react-jsonschema-form-conditionals icon indicating copy to clipboard operation
react-jsonschema-form-conditionals copied to clipboard

Form sends onChange before conditional re-validation applied

Open mrclay opened this issue 6 years ago • 3 comments

We have lots of required conditionally displayed questions and the Form's onChange is firing too early, sending us the set of errors before the revalidation of the new schema has been completed.

Our current workaround is to have several of our widgets (ones which affect others) call onChange a second time after a short delay. After the delay the Form internally has the correct set of errors considering all rules.

mrclay avatar Mar 30 '18 17:03 mrclay

@mrclay Can you provide an example of a form and change that is happening. Thanks in advance.

mavarazy avatar May 22 '18 19:05 mavarazy

Faced the same issue. Here is my example:

schema: {
	"type": "object",
	"required": [
	  "field2"
	],
	"properties": {
	  "checkbox": {
	    "type": "boolean",
	    "default": false
	  },
	  "field1": {
	    "type": "string"
	  },
	  "field2": {
	    "type": "string"
	  }
	}
}
rules: [{
	"conditions": {
	 "checkbox": {
	    "is": true
	 }
	},
	"event": [
	  {
	    "type": "remove",
	    "params": {
	      "field": [
	        "field2"
	      ]
	    }
	  },
	  {
	    "type": "require",
	    "params": {
	      "field": [
	        "field1"
	      ]
	    }
	  }
	]
}]

When you submitting form you will get required field2 in errors After applying checkbox onChange event still handling error in field2 which is not exist on form. The only way to overrite errors is to submit the form but in real app there are cases that prevent me from using it.

i-Living avatar May 21 '19 17:05 i-Living

I'm sorry I've not been able to provide an example form. They're all large and use many custom widgets/fields, and to be fair it's quite possible our problems lie there.

mrclay avatar May 21 '19 18:05 mrclay