Setting "required" based on a dropDownList option


I’ve got a dropDownList where I can choose three options.

I would set another field as required when one of the dropDownList option is selected.

How can I do it?


On the front-end, you should handle this in JavaScript (maybe jQuery).

On the back-end, first, you should assign some meaningful values to each element of the dropdown list. Then when user submits the form, you can use the value of the dropdown list as a "validation scenario" for a model.

For example, when "name" option is chose, "name" textfield should be filled, or when "last name" option is chosen, "last name" should be visible and is required. This should be done in Javascript.

On the back-end, then, you accept the value of dropbox. The below is a psudocode:

$option = $_POST['MyModel']['my_option'];

$model = new MyModel($option);

$model->attributes = $_POST['MyModel'];

if ($model->validate())