I have an image field and a checkbox in my node edit form. I want the image field to be required when the checkbox is not checked. And I want the image field to not be required when the checkbox is checked.
How can I do this?
For image field use #states, this will be making a field required based on the checkbox clicked or not. Check here for more information
'#states' => array(
'required' => array(
:input[name="NAME OF THE FIELD"]' => array('value' => 'VALUE OF FIELD'),
),
),
#states
to make a field required is possible, and it's easy. But it requires JS, and validation will be done in the browser. If that works for you, because your whole site won't work without JS, cool. Alternatively you'd implement an#element_validate
callback and do the validation in PHP. Depends a bit on how important the form and the requirement is.