Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not sure why people would want to use ko.validation - it just replicates the processing breeze's client side is doing anyway. And given the breeze developers hints that validation will get even more power soon, why bother.</p> <p>So I started with Thiago Oliveira's great work. But I wanted to have the bare minimum of markup. By assuming the use of bootstrap classes &amp; defaulting the validation property name from the previous element I could simplify most markup additions to:</p> <pre><code>&lt;span class="help-inline" data-bind="breezeValidation: null"&gt;&lt;/span&gt; </code></pre> <p>Win! </p> <p>My ko.bindingHandler:</p> <pre><code>//Highlight field in red &amp; show first validation message // //Outputs first validation message for 'propertyName' or if null: previous controls value binding //Needs ancestor with 'control-group' class to set class 'error' for Bootstrap error display // //Example: //&lt;td class="control-group"&gt; // &lt;input class="input-block-level text-right" data-bind="value: id" /&gt; // &lt;span class="help-inline" data-bind="breezeValidation: null"&gt;&lt;/span&gt; //&lt;/td&gt; // //Does not and cannot validate keys that already exist in cache. knockout write calls breeze which throws uncaught error ko.bindingHandlers.breezeValidation = { init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { // This will be called when the binding is first applied to an element // Set up any initial state, event handlers, etc. here var $msgElement = $(element); var entity = viewModel; var propName = valueAccessor(); if (propName === null) { // $element.prev().data("bind") = "value: itemType" var prevBinds = $msgElement.prev().data("bind"); if (!prevBinds) { $msgElement.text("Could not find prev elements binding value."); return; } var bindPhrases = prevBinds.split(/,/); for (var i = 0, j = bindPhrases.length; i &lt; j; i++) { var bindPhrase = bindPhrases[i]; if (utility.stringStartsWith(bindPhrase, 'value: ')) { propName = bindPhrase.substr(7); break; } } } if (!propName) { $msgElement.text("Could not find this or prev elements binding value."); return; } //var $groupElement = $msgElement.parent(); var $groupElement = $msgElement.closest(".control-group"); if (!$groupElement.hasClass("control-group")) { $msgElement.text("Could not find parent with 'control-group' class."); return; } onValidationChange(); //fire immediately (especially for added) //... and anytime validationErrors are changed fire onValidationChnange entity.entityAspect.validationErrorsChanged.subscribe(onValidationChange); element.onchange = function () { //Should never have updates pushed from validation msgElement $msgElement.text("readonly error"); }; function onValidationChange() { var errors = entity.entityAspect.getValidationErrors(propName); var message = ""; if (errors.length &gt; 0) { message = errors[0].errorMessage; } if (message) { $groupElement.addClass('error'); } else { $groupElement.removeClass('error'); } $msgElement.text(message); } } //Not interested in changes to valueAccessor - it is only the fieldName. //update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { }; </code></pre> <p>Example view simple implicit property usage:</p> <pre><code>&lt;div class="control-group"&gt; &lt;label class="control-label" for="editStatusNote"&gt;Status note:&lt;/label&gt; &lt;div class="controls"&gt; &lt;input id="editStatusNote" type="text" data-bind="value: statusNote" /&gt; &lt;span class="help-inline" data-bind="breezeValidation: null"&gt;&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>Example view explicit property usage:</p> <pre><code>&lt;div class="control-group"&gt; &lt;label class="control-label" for="editAmount"&gt;Amount:&lt;/label&gt; &lt;div class="controls"&gt; &lt;div class="input-prepend"&gt; &lt;span class="add-on"&gt;$&lt;/span&gt; &lt;input id="editAmount" class="input-small" type="text" data-bind="value: amount" /&gt; &lt;/div&gt; &lt;span class="help-inline" data-bind="breezeValidation: 'amount'"&gt;&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; </code></pre>
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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