Clare Barrington
Clare Barrington

Reputation: 1155

New Aurelia Validation ! equals(expectedValue)

The old way:

this.validator = this.validation.on(this)
    .ensure('baseContent.RedirectFrom')
        .isNotEmpty()
    .ensure('baseContent.RedirectTo')
        .isNotEmpty()
        .isNotEqualTo(() => { return this.baseContent.RedirectFrom }, 'Redirect from');
});

isNotEquals doesn't seem to exist in the new validation there is a equals(expectedValue)

I have considered doing a custom validation element however struggled as it passed through 'RedirectFrom' as undefined.

ValidationRules.customRule('notEqualTo',
        (value, obj, otherValue) => {
            return value === null || value === undefined || value === '' || otherValue === null || otherValue === undefined || otherValue === ''||  value === otherValue;
        }, 
        "must not match");

.ensure('RedirectTo').required().satisfiesRule('notEqualTo', this.baseContent.RedirectFrom)
.ensure('RedirectTo').required().satisfiesRule('notEqualTo', this.RedirectFrom)
.ensure('RedirectTo').required().satisfiesRule('notEqualTo', RedirectFrom)

Anyone see anything I'm missing?

Upvotes: 1

Views: 222

Answers (1)

Clare Barrington
Clare Barrington

Reputation: 1155

obj[propertyName]

CustomValidation.js

ValidationRules.customRule('mustNotMatchValue',
            (value, obj, valueToNotMatch) => {
                return value === null || value === undefined || value === '' || obj[valueToNotMatch] === null || obj[valueToNotMatch] === undefined || obj[valueToNotMatch] === '' ||  value !== obj[valueToNotMatch];
            }, 
            "must not match ${$config.valueToNotMatch}", 
            (valueToNotMatch) => ({ valueToNotMatch }));

baseContent.js

.ensure('RedirectTo').required().satisfiesRule('mustNotMatchValue', 'RedirectFrom');

Upvotes: 1

Related Questions