Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Some time ago, I posted a helper function that extend observable properties to add validation based on the breeze validators. Using that helper you can achieve the red color on the invalid inputs:</p> <p><a href="https://stackoverflow.com/questions/14316454/translate-breeze-validation-messages">Translate breeze validation messages</a></p> <p>I can't help you with the second question, I know that you can customize the validation message but I think that with the default validators you can't set a friendly name to show on the message.</p> <p>Update:</p> <p>The first that I do is to create a helper module that exposes the function (updated version):</p> <pre><code>define([], function () { "use strict"; var foreignKeyInvalidValue = 0; function addValidationRules(entity) { var entityType = entity.entityType, i, property, propertyName, propertyObject, validators, u, validator, nValidator; if (entityType) { for (i = 0; i &lt; entityType.dataProperties.length; i += 1) { property = entityType.dataProperties[i]; propertyName = property.name; propertyObject = entity[propertyName]; validators = []; for (u = 0; u &lt; property.validators.length; u += 1) { validator = property.validators[u]; nValidator = { propertyName: propertyName, validator: function (val) { var error = this.innerValidator.validate(val, { displayName: this.propertyName }); this.message = error ? error.errorMessage : ""; return error === null; }, message: "", innerValidator: validator }; validators.push(nValidator); } propertyObject.extend({ validation: validators }); } for (i = 0; i &lt; entityType.foreignKeyProperties.length; i += 1) { property = entityType.foreignKeyProperties[i]; propertyName = property.name; propertyObject = entity[propertyName]; validators = []; for (u = 0; u &lt; property.validators.length; u += 1) { validator = property.validators[u]; nValidator = { propertyName: propertyName, validator: function (val) { var error = this.innerValidator.validate(val, { displayName: this.propertyName }); this.message = error ? error.errorMessage : ""; return error === null; }, message: "", innerValidator: validator }; validators.push(nValidator); } propertyObject.extend({ validation: validators }); if (!property.isNullable) { //Bussiness Rule: 0 is not allowed for required foreign keys propertyObject.extend({ notEqual: foreignKeyInvalidValue }); } } } } return { addValidationRules: addValidationRules }; }); </code></pre> <p>Then, I'm defining an initializer for each breeze entity type ( <a href="http://www.breezejs.com/documentation/extending-entities" rel="nofollow noreferrer">http://www.breezejs.com/documentation/extending-entities</a> ). Example:</p> <pre><code>define(['app/validatorHelper', 'knockout'], function (vHelper, ko) { "use strict"; var constructor = function () { }, initializer = function indicadorInitializer(entity) { vHelper.addValidationRules(entity); }; return { constructor: constructor, initializer: initializer }; }); </code></pre> <p>And finally, somewhere ( I'm doing it on an init function inside my dataservice module ), I'm registering the initializer ( <a href="http://www.breezejs.com/documentation/extending-entities" rel="nofollow noreferrer">http://www.breezejs.com/documentation/extending-entities</a> ) :</p> <pre><code>//store comes from: manager = breezeconfig.createManager(), // store = manager.metadataStore, store.registerEntityTypeCtor("Palanca", domain.palanca.constructor, domain.palanca.initializer); </code></pre> <p>I'm doing all of this before fetching the metadata.</p> <p>I hope that this would help you.</p> <p>Update 2:</p> <p>I've found the problem, your version of knockout validation is not the last one.</p> <p>Inside the downloaded file from: <a href="http://ericmbarnard.github.com/Knockout-Validation/" rel="nofollow noreferrer">http://ericmbarnard.github.com/Knockout-Validation/</a> the line: 349 is:</p> <pre><code>exports.rules[ruleName] = ruleObj; </code></pre> <p>Inside your file, the equivalent line ( function addAnonymousRule ) is:</p> <pre><code>ko.validation.rules[ruleName] = { validator: ruleObj.validator, message: ruleObj.message || 'Error' }; </code></pre> <p>I think that with the lastest version should work.</p> <p>Update 4:</p> <p>This is the code to save:</p> <pre><code>vmAddPalanca.prototype.saveChangesCmd = ko.asyncCommand({ execute: function (palanca, complete) { var validationErrors = ko.validation.group(palanca); if (validationErrors().length === 0) { dataservice.saveChanges() .then(saveChangesSuccess) .fail(saveChangesFail) .fin(complete); } else { validationErrors.showAllMessages(true); toastr.error("Debe corregir los errores antes de poder guardar"); complete(); } }, canExecute: function (isExecuting) { return !isExecuting &amp;&amp; dataservice.hasChanges(); } }); </code></pre>
 

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