c# - Fluent Validation - Stop validating all other validations when a specific validation fails -
i'm using fluent validation server side validation. i've created set of rules validated. these rules individual functions in validator.
public samplevalidator() { validate_authorisation(); validatetitle_notempty(); validategender_notempty(); validatefirstname_regex(); validatefirstname_notempty(); validatesurname_notempty(); validatesurname_regex(); validatemobilephone_regex(); } private void validate_authorisation() { rulefor(model=> model) .must(model=> isuserauthorised(username)) .withname("authorisation check"); } private void validatetitle_notempty() { rulefor(model=> model) .must(title=> !string.isnullorempty(title)) .withname("title"); } private void validategender_notempty() { rulefor(model=> model) .must(gender=> !string.isnullorempty(gender)) .withname("gender"); }.... , others
now, want stop validating other validations when authorisation validation fails. don't want use cascademode.stoponfirstfailure because checks first validation failure , stop validating others. there way return service (from validator called) when authorisation validation fails.
if change validate_authorisation
method following:
private irulebuilderoptions<model, string> validate_authorisation() { rulefor(model=> model) .must(model=> isuserauthorised(username)) .withname("authorisation check"); }
you can following using dependentrules
extension method:
validate_authorisation().dependentrules(rules => { validatetitle_notempty(); validategender_notempty(); validatefirstname_regex(); validatefirstname_notempty(); validatesurname_notempty(); validatesurname_regex(); validatemobilephone_regex(); });
Comments
Post a Comment