validation icon indicating copy to clipboard operation
validation copied to clipboard

Drive Validations based on values returned from REST controller.

Open atreya-yelishetti opened this issue 5 years ago • 2 comments

Code Sample:

ValidationRules
.ensure('id').displayName('ID').required().then().

Current behavior:

If code is bundled with webpacks, and later on we decided id is not a required field, we need to make code changes by removing .required() on a property and rebundle with webpack and deploy it

Expected/desired behavior:

Consider I have a REST CONTROLLER which says if that is required or not. Based on a value of a key I would like to dynamically set id as required or not required.

if the controller returns as below json, I would like id to be required: id:{ required:true; }

if the controller returns as below json, I would like id to be not required: id:{ required:false; }

I've already implemented a service which does that for me, Can I know how to achieve this on UI with Aurelia-Validation framework.?

atreya-yelishetti avatar Apr 09 '19 19:04 atreya-yelishetti

Something like this:

//someother validations written here like as shown

ValidationRules .ensure('cid').displayName('Company ID').required().then().matches(/^[a-zA-Z]{1}/).withMessage('${$displayName} should always start with alphabet').then().matches(/^[a-zA-Z]{1}[a-zA-Z0-9]*$/).withMessage('Spaces and Special characters are not allowed.').then().minLength(this.companyCidMin).maxLength(this.companyCidMax).on(this.selectedItem);

//then the following:

if(this.companyNameRequired){ ValidationRules .ensure('cid').displayName('Company ID').required().on(this.selectedItem); }

atreya-yelishetti avatar Apr 09 '19 22:04 atreya-yelishetti

Something like this should work:

let partialRules = ValidationRules.ensure(property).required();

if (conditionFromBackend) {
partialRules = partialRules.ensure(property).satisfies(otherCondition);
}

partialRules.on(this.selectedItem);

Rule definition has a fluent interface, so every step can be saved into a variable. Note also that on can be applied to an object only once; doing it multiple times will just overwrite the current rules.

Alternatively, a simple .when(condition) could be enough (see the Conditional validation section at https://aurelia.io/docs/plugins/validation#defining-rules).

m-gallesio avatar Apr 23 '19 19:04 m-gallesio