You are here

function conditional_fields_custom_required_field in Conditional Fields 6.2

Unset the #required property and set a #conditional_fields_required property for custom validation.

See also

conditional_fields_set_required_field()

2 calls to conditional_fields_custom_required_field()
conditional_fields_form_alter in ./conditional_fields.module
Implementation of hook_form_alter().
conditional_fields_node_after_build in ./conditional_fields.module
Main tasks:

File

./conditional_fields.module, line 1341
Content fields and groups visibility based on the values of user defined 'trigger' fields.

Code

function conditional_fields_custom_required_field(&$field, &$field_info = NULL) {
  if (isset($field['#required']) && $field['#required']) {
    unset($field['#required']);
    $field['#conditional_fields_required'] = TRUE;

    // Some modules like FileField use the field info property to check if the
    // field is required, so change it.
    if ($field_info) {
      $field_info['required'] = '0';
    }

    // Required radio buttons without a default value need custom validation,
    // otherwise _form_validate will think that an invalid choice is selected
    // when the field is submitted with no value.
    if ($field['#type'] == 'radios' && !$field['#default_value'] && isset($field['#needs_validation']) && $field['#needs_validation']) {
      unset($field['#needs_validation']);
      $field['#element_validate'] = array_merge(array(
        'conditional_fields_required_radios_validate',
      ), (array) $field['#validate']);
    }
  }
  foreach (element_children($field) as $child) {
    conditional_fields_custom_required_field($field[$child]);
  }
}