1. If this filed is mandatory and unlimited , leave it empty, the form error won`t show the filed name.
2. If this field has multiple children we leave the first to empty and click save, error will occur.

Please see the attachments.

CommentFileSizeAuthor
field2.png5.29 KBavenhe
field.png6.73 KBavenhe
error.png2.55 KBavenhe
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

avenhe’s picture

Status: Active » Needs review

My custom module here to fix these two issue

/**
 * Implementation of hook_form_alter()
 * For mandatory unlimited entity-reference field , the cron module set only the first field to be mandatory
 * Now we use this function to add a validate fucntion to those field.
 * First we set the first field un-required and check in the validate function for all the field
 */
function alu_features_content_type_form_alter(&$form, $form_state, $form_id){
  // Set the entityreference type in field #attributes
  $type = 'field-type-entityreference';
  $boolen = false;
  foreach ($form as $id => $list){
    if (is_array($list)){
      // Not all the field has those filed so we must check
      if (isset($list["#attributes"]["class"]) && !empty($list["#attributes"]["class"])){
        // To check this field type is entityreference or not
        if (in_array($type,$list["#attributes"]["class"])){
          // If this field is required then we add a validate function in $form["actions"]["submit"]
          if ($list["und"]["#required"] && isset($list['und']['add_more'])){
            // Set the first field required to false
            $form[$id]["und"][0]["target_id"]['#required'] = false;
            // Add vaildate function
            $boolen = true;
          }
        }
      }
    }
  }
  // Add vaildate function
  if ($boolen){
    $form["actions"]["submit"]['#validate'][] = 'alu_features_content_type_field_spotlights_validation' ;
  }
}

/**
 * Implementation of hook_form_validation()
 */
function alu_features_content_type_field_spotlights_validation($form, &$form_state){
  // Get the fields from form post
  $field_type = $form_state['field'];
  // To check fields is entityreference or not
  foreach ($field_type as $name => $field){
    // If the type is entityreference and required is ture
    if ($field['und']['field']['type'] == 'entityreference' && $field['und']['instance']['required'] == 1){
      $field_check = $form_state['values'][$name];
      $boolen = false ;
      // Expect the singl line field
      if (isset($field_check['und']['add_more'])){
        foreach ( $field_check as $fields ){
          foreach ($fields as $array => $id){
            if (is_numeric($array)){
              // Check whether or not it is empty or not
              if (isset($id['target_id']) && !empty($id['target_id'])){
                $boolen = true;
              }
            }
          }
        }
        // Sent error messege if the unlimited field is all empty
        if (!$boolen){
          // Use the label for this field instead of using the empty title
          $label = $field['und']['instance']['label'];
          form_error($form[$name],t('%name is required!',array('%name' => $label)));
        }
      }
    }
  }
}

amitaibu’s picture

Priority: Major » Normal
Status: Needs review » Active

Please roll a patch.

Samvel’s picture

Guys, problems not only for entityreference, but for all types all fields (for multiple fields specially removed title in core, it is normal). It must be patch for core.