angular-schema-form icon indicating copy to clipboard operation
angular-schema-form copied to clipboard

validation of conditionally showed input

Open haluz opened this issue 9 years ago • 12 comments

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:

  1. Remove any data from "Seconds" input (the form becomes invalid).
  2. Uncheck the "Show Integer Input" checkbox (the form is valid again).
  3. 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)

haluz avatar Jun 05 '15 11:06 haluz

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?

nicklasb avatar Jun 11 '15 12:06 nicklasb

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
}

haluz avatar Jun 12 '15 09:06 haluz

It should re-validate in that situation. I would say this is a bug.

nicklasb avatar Jun 12 '15 09:06 nicklasb

@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)

davidlgj avatar Jun 12 '15 09:06 davidlgj

Well, the moment the field reappears, it is a form field again?

nicklasb avatar Jun 12 '15 09:06 nicklasb

Yes, its form field.

haluz avatar Jun 12 '15 09:06 haluz

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 avatar Jun 12 '15 09:06 nicklasb

@nicklasb oh. that sounds broken. I got to test that

davidlgj avatar Jun 12 '15 11:06 davidlgj

Any update on this bug?

oskanberg avatar Feb 28 '17 11:02 oskanberg

@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.

Anthropic avatar Feb 28 '17 14:02 Anthropic

any update on this bug ? I am facing the same issue in our application . Need a work around to fix this issue.

angular360 avatar Nov 27 '17 03:11 angular360

@angular360 did the work arounds above not help your case?

Anthropic avatar Nov 28 '17 12:11 Anthropic