angular-schema-form
angular-schema-form copied to clipboard
validation of conditionally showed input
Hi, this is my form:
[
{
"key": "showinput",
"type": "checkbox"
},
{
"key": "seconds",
"type": "number",
"condition": "model.showinput"
}
]
and this is my schema:
{
"type": "object",
"properties": {
"showinput": {
"type": "boolean",
"title": "Show integer input",
"default": true
},
"seconds": {
"type": "integer",
"title": "Seconds",
"default": 60,
"minimum": 1
}
},
"required": [
"seconds"
]
}
What I do:
- Remove any data from "Seconds" input (the form becomes invalid).
- Uncheck the "Show Integer Input" checkbox (the form is valid again).
- Check the "Show Integer Input" checkbox back.
And now the form is still valid! I need it invalid obviously, how can I fix that?
Thanks)
Is it the default value that is re-entered or something, it the "seconds" empty if you click it a third time? And what are the actual model values at that point?
There is no value in "seconds" input, its empty after removing. I reproduce it on example page (http://schemaform.io/examples/bootstrap-example.html) and after removing "seconds" value, model is next and it doesn't change on check/uncheck the checkbox:
{
"showinput": true
}
It should re-validate in that situation. I would say this is a bug.
@haluz schema form only validates the form fields, not the entire model against the entire schema. This is by design so that you can have partial forms for a schema. This also means that when you hide something with condition
it is removed from the validation.
But what you can do is use https://github.com/geraintluff/tv4 directly to validate your model before submit.
(EDIT: my other idea didn't work)
Well, the moment the field reappears, it is a form field again?
Yes, its form field.
Sorry, I was referring to @davidlgj:s reply, I was unclear: @davidlgj : What I am saying is that when it is unhidden again it should be re-added to the validation, which doesn't seem to happen.
@nicklasb oh. that sounds broken. I got to test that
Any update on this bug?
@oskanberg there's two ways this can be done.
When a submit is triggered or the schemaFormValidate event is triggered, the form will again display the error. You can see here when you follow the original instructions and then click submit http://localhost/asf/examples/example.html#/99678122dfeb31c38454edae6d0ac826
Alternatively you can also update the destroyStrategy to retain
and set allowInvalid: true
in ngModelOptions so any numeric value is retained to trigger the error again. Angular doesn't trigger the validation on null though, only on valid invalid values.
If there was a PR to trigger the validate on a valid condition displaying a field again when validateOnRender is true, I'd accept it. Otherwise it will have to wait for other higher priorities for my own limited time.
any update on this bug ? I am facing the same issue in our application . Need a work around to fix this issue.
@angular360 did the work arounds above not help your case?