Description
The Problem
I have create user form which along with other fields contains password and passwordRepeat fields.
Validation should be triggered on blur and the goal is to get "Passwords should match" message near the passwordRepeat field.
Investigation
The first idea was to add custom keyword in JsonSchema which will compare values. Something like this:
{
type: "object",
required: ['password', 'confirmPassword'],
properties: {
password: {
type: "string"
},
confirmPassword: {
type: "string",
equalTo: "password"
}
}
}
equalTo should be registered in tv4 on initialization stage.
However later I realized that angular-schema-form splits original schema by field so each field knows only about own piece of schema which makes impossible to reffer other fields.
Solution
To be honest I didn't find really good solution here. I just forked angular-schema-form repository and added optional callback function in form definition which will be invoked after default validation in case when tv4 decided that value is valid.
Here is usage example from my project:
{
key: 'confirmPassword',
title: 'Confirm password',
type: "password",
validationMessage: validationMessagesBuilder.build('Confirm password', null, {
'notMatch': "Passwords do not match"
}),
afterValidated: function (value, fieldModel, formModel, schema) {
if (formModel.password != value) {
return 'notMatch';
}
}
}
It should return error code if there is an error and undefined otherwise.
So...
Does anyone have any better ideas? Maybe someone solved similar issue before... Maybe I missed something...
Anyway if you think that this approach is useful and correct I can prepare pull request.
Chears,
Dmitry