json-schema-form / angular-schema-form

Generate forms from a JSON schema, with AngularJS!
https://json-schema-form.github.io/angular-schema-form
MIT License
2.47k stars 653 forks source link

Conditional Required in Schema form #939

Open sonikritika569 opened 6 years ago

sonikritika569 commented 6 years ago

I have to handle a scenario where I have some fields in my form if any of them is filled other all fields become required. Is there any way by which we can mark conditional Required in such scenario?

yourarj commented 6 years ago

Namaste @sonikritika569 , Kindly check the contributing.md file before submitting a bug. This seems some logical problem not an issue in angular-schema-form. Kindly check stackoverflow for more relevant problems and solutions.

Anthropic commented 6 years ago

@sonikritika569 you can do this using the calculate add-on to set a value which you then add to the condition on all fields which you want to show to make them show.

brunodymios commented 6 years ago

@sonikritika569 try to check the example "Hack: Conditional required" in the examples page http://schemaform.io/examples/bootstrap-example.html. Basically, for each conditionally required field you have two versions of it, with the same key. It may be very useful to support a "requiredIf" key in the schema that accepts a condition-like formula to better manage these situations that are very common. This require an add-on.

@Anthropic do you think it would be easier to edit the current "required" schema key in order to support an array of objects? In each of them we would specify the key of the field and a condition that make it required.

Thank You

Anthropic commented 6 years ago

@brunodymios the required property of the schema is defined by the json-schema standard. I am looking at adding in a rule processing capability in future that can masse-update required, visible and disabled states for fields based on rules. but it could be a long time coming.

brunodymios commented 6 years ago

@Anthropic Thank You. I did some research after writing and I noticed that indeed in the official specification of the standard there is no way to specify such validations. It could be a nice proposal for a future release of the standard, because they are very commonly requested. Extending your library this way could result in inconsistencies when used with other json schema validator (e.g. for server-side validations).