multiple custom field required, pass validation if field is empty

by Vincent   Last Updated January 09, 2018 15:07 PM

I've build a custom field with several values. I've to make this field required. But I want to pass the validation if at least one field is filled and the last one is empty.

But my problem is Drupal warn me that the last (empty) field is required. I've thought that the hook_field_is_empty() solved the problem, but, even if return true, the form cannot be validated.

Many thanks for your help.

Implementation :

function MYMODULE_field_widget_form(...) {
    $element['address']+=[
      ...
      '#required' => $instance['required'],
    ];
    ...
}

function MYMODULE_field_is_empty($item, $field) {
    if (empty($item['address']) && empty($item['other'])) {
        return TRUE ;
    }
    return false ;
}
Tags : 7


Related Questions