angular-schema-form: More complicated validation scenarios seems to be not supported
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
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 19 (8 by maintainers)
Hi @corvis - that’s a really great question. It’s generally against the goals of this project to change the json-schema spec (which is actually an ietf standard). However, the ability you want is part of the v5
$data
proposal forconstant
.You would do something like:
I’m just about 100% sure that TV4 doesn’t support this yet (although others like jjv do), so json-schema-form can’t support this right now. However, I just wanted to mention this as the “correct” way to do this.
I believe there are some other workarounds provided by the form declaration that could allow you to add this functionality, but I’ll defer to those more familiar with them. Either way, if you’re using a server-side validator that supports
constant
and$data
, you can declare it like above without breaking the client side.