Check for required information in multiple fields based on the state of one field
Last updated on
30 April 2025
<?php
/**
* In this example, the form asks how a client prefers to be contacted.
* Whatever they choose is then checked to be sure the matching
* field has the information needed to respond in the preferred manner.
*/
// Get form values
$phone = $form_values['submitted_tree']['phone'];
$fax= $form_values['submitted_tree']['fax'];
$email = $form_values['submitted_tree']['email'];
$preferred = $form_values['submitted_tree']['preferred_contact_method'];
// If Preferred is set to Phone, check that they entered a phone number.
if ($phone == "" && $preferred == 'Phone') {
form_set_error('submitted][phone', t('If you really prefer to be contacted by phone, please include a phone number.'));
}
// If Preferred is set to Fax, check that they entered a fax number.
if ($fax== "" && $preferred == 'Fax') {
form_set_error('submitted][fax', t('If you really prefer to be contacted by fax, please include a fax number.'));
}
// If Preferred is set to Email, check that they entered an email address.
if ($email == "" && $preferred == 'Email') {
form_set_error('submitted][email', t('If you really prefer to be contacted by email, please include an email address.'));
}
?>
Help improve this page
Page status: Not set
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion