Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drive Validations based on values returned from REST controller. #521

Open
atreya-yelishetti opened this issue Apr 9, 2019 · 2 comments

Comments

@atreya-yelishetti
Copy link

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
Copy link
Author

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);
}

@m-gallesio
Copy link

m-gallesio commented Apr 23, 2019

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants