Closed jods4 closed 8 years ago
Can I suggest that we don't spin off this thread just yet? We desperately need to get the core functionality nailed down, and if there is a generic requirement for a feature then we need to incorporate or support that in the core system, so discussing options or solutions outside of that will fragment the process of finding a solution. Just a friendly suggestion...
@jods4 thanks for the example, will give it try and keep you posted
@jods4 @jdanyow i managed to add the map validator made a slight change to the code, however i faced a couple of issues
const validator = new Validator();
validate.validators.map = function(map: Map<string, any>) {
const errors = []
map.forEach((v, k) => {
console.log(v, validator.validateObject(v));
if (validator.validateObject(v).length > 0) errors.push(k);
});
// Unfortunately details are lost, but Aurelia's controller will evaluate and display them on the binding as well.
// This is only really useful for your `validate()` call when saving.
return errors.length > 0 ? `${errors.join(',')} has invalid values` : null;
}
function mapRule(config) {
return new ValidationRule('map', config);
}
export function validatemap(targetOrConfig?, key?, descriptor?) {
return base(targetOrConfig, key, descriptor, mapRule);
}
& validate
to the property <ui-language languages.bind="model.languages & validate" select.trigger="changeLanguage($event)" add.trigger="addLanguage($event)" delete.trigger="removeLanguage($event)">Language</ui-language>
description
{rule: ValidationRule, message: "Null can't be blank", object: HotelLanguage, propertyName: null}
it wouldn't validate the map object unless i add & validate to the property
Right... :disappointed: This is a limitation of the current aurelia-validation
API. Only properties linked to UI bindings are validated. I think @jdanyow wants to add a .validate(object)
API to make sure every property of the object gets validated.
i keep getting this error even if the object was valid, it doesn't seem to pick the property name
Really not sure about this one, sorry. You'll have to debug it to find out where it goes wrong :frowning:
closing- we have a new release that supports some or possibly all of this stuff. please open an issue in aurelia-validation for anything that's missing
This is a spin-off of aurelia/validation#252 to answer @adarshpastakia who asked how to validate a
Hotel
that has a Map ofHotelLanguage
.I haven't actually built the code, but I would use something like this:
@jdanyow Writing this code has shown me a few things: it is cumbersome to write your own validators. Suggestions:
getRules(object)
.ValidationRule
. In the code above I had to do a little dance aroundValidator
because the stored rules are not in the nativevalidatejs
format. Is there any reason not to store plain constraints? It would make things easier and this module is specific tovalidatejs
anyway, it's not generic.base
something else. Or maybe we could not exportbase
, because with the changes suggested above I think the following code is decent: