Page 1 of 1

prevent from being saved if an option is selected on radio button field and the next field is left blank

Posted: 2021-05-26 20:02
by fgazza
Hi to all.
I need to prevent the record from being saved if the "other, please specify" option is selected in a radio button field and the next field named "other" is left blank.

In this case an error message "unable to save the record because you selected the OTHER option but did not specify in the next field.

Can anyone suggest the code?

Thank you!!!

Fabiano

Re: prevent from being saved if an option is selected on radio button field and the next field is left blank

Posted: 2021-06-04 05:03
by jsetzer
Seems you are looking for conditional validation: field2 is required IF field1 does not contain a certain value.

Alternative 1 (PHP)

Unset "required", re-generate app, then validate $data on serverside (PHP) in _before_insert and _before_update. Depending on the validation result, return TRUE or FALSE . Backdraw: if validation fails on insert and you return FALSE, user will be redirected and there may be data-loss.

Alternative 2 (Javascript)

Create a javascript function named TABLENAME_validateData(). Get the values of your fields by using Javascript/JQuery. Validate your fields' values. if validation fails, return false, otherwise return true. You can also highlight errors by adding Bootstrap's "has-error" class on the closest .form-group of the field which is causing validation errors.

(Done this few years ago but not tested with latest version)