要使用KnockoutJS处理验证,我一直在使用Knockout-Validation扩展带有验证规则的可观察项:
var myObj = ko.observable('').extend({ required: true });
这使得在单个字段级别上创建表单和处理验证变得简单:
<div>
<input type="text" data-bind="value: someValue"/>
<p data-bind="validationMessage: someValue"></p>
<div>
复杂对象级别:
//want to know if all of your ViewModel's properties are valid?
var myViewModel = ko.validatedObservable({
property1: ko.observable().extend({ required: true }),
property2: ko.observable().extend({ max: 10 })
});
console.log(myViewModel.isValid());
如何扩展带有子属性的Mobx Observable进行验证,以实现相似的目的?