Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The validation plugin only gets applied in your bindings only if by the time when the binding is parsed by Knockout your properties are validate. </p> <p>In different words: you cannot add validation to a property <strong>after</strong> the property was bound on the UI.</p> <p>In your example you are using an empty object in <code>self.person = ko.observable({});</code> as a default value, so when Knockout executes the <code>data-bind='value: person().name'</code> expression you don't have a <code>name</code> property so the validation won't work even if you later add the <code>name</code> property to your object.</p> <p>In your example you can solve this with changing your <code>Model</code> constructor to include the validation rules:</p> <pre><code>function Model() { this.name = ko.observable().extend({required: true}); this.email = ko.observable().extend({required: true}); } </code></pre> <p>And use an empty <code>Model</code> object as the default person:</p> <pre><code>self.person = ko.observable(new Model()); </code></pre> <p>And when calling <code>Load</code> don't replace the <code>person</code> object but update its properties:</p> <pre><code>self.load = function() { // Set person data self.person().name('Long'); self.person().email('John'); } </code></pre> <p><strong>Demo <a href="http://jsfiddle.net/2Um56/">JSFiddle</a>.</strong></p> <p>Note: Knockout does not always handles well if you replace whole object like <code>self.person(new Model());</code> so it is anyway a better practice to only update the properties and not throw away the whole object.</p> <p>A different solution would be to use the <code>with</code> binding because inside the <code>with</code> binding KO will reevaluate the bindings if the bound property changes.</p> <p>So change your view: </p> <pre><code>&lt;!-- ko with: person --&gt; &lt;input type='text' data-bind='value: name' /&gt; &lt;input type='text' data-bind='value: email' /&gt; &lt;!-- /ko --&gt; </code></pre> <p>In this case you need to use <code>null</code> as the default <code>person</code>:</p> <pre><code>self.person = ko.observable(); </code></pre> <p>And in your <code>Load</code> you need to add the validation <strong>before</strong> assigning your <code>person</code> property so by the time KO applies the bindings your properties have the validation:</p> <pre><code>self.load = function() { var model = new Model() model.name.extend({required: true}); model.email.extend({required: true}); self.person(model); // Set person data self.person().name('Long'); self.person().email('John'); } </code></pre> <p><strong>Demo <a href="http://jsfiddle.net/5sV5B/">JSFiddle</a>.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload