Hi,
Trying to validate 2 elements in an inline entity form that is an entity reference field. Inline and host entities are both nodes.
This code adds a validate function and it works as expected.

function wcava_validate_file_attachment_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
  if ($entity_form['#bundle'] == 'file') {
    $entity_form['#element_validate'][] = 'wcava_validate_file_attachment_inline_form_validate';
  }
}

This validates the 2 fields correctly.

function wcava_validate_file_attachment_inline_form_validate($entity_form, &$form_state) {
  if (!(empty($form_state['values']['field_file_attachments'][LANGUAGE_NONE]['form']['field_file_attachment'][LANGUAGE_NONE][0]['fid'])
  xor empty($form_state['values']['field_file_attachments'][LANGUAGE_NONE]['form']['field_embed_google_doc'][LANGUAGE_NONE][0]['value']))) {
    form_error($entity_form, t('Please attach a file OR embed a google doc. You can\'t do both on the same file attachment.'));
  }
}

As you can see from the attached image, this does work, but confusingly highlights the inline node's title field as being invalid, which it isn't.

Is there a better way to validate these fields so I can set errors on the fields at fault, rather than on the $entity_form?

CommentFileSizeAuthor
Capture.PNG66.75 KBmshepherd
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mshepherd’s picture

For the sake of completeness, I've just modified the 2nd of these snippets so we don't skip validation if the referenced node is edited inline:

function wcava_validate_file_attachment_inline_form_validate($entity_form, &$form_state) {
  if ($entity_form['#op'] == 'edit') {
    $a = $entity_form['#parents'][0];
    $b = $entity_form['#parents'][1];
    $c = $entity_form['#parents'][2];
    $d = $entity_form['#parents'][3];
    $e = $entity_form['#parents'][4];
    $file_attachment_value = $form_state['values'][$a][$b][$c][$d][$e]['field_file_attachment'][LANGUAGE_NONE][0]['fid'];
    $file_embed_value = $form_state['values'][$a][$b][$c][$d][$e]['field_embed_google_doc'][LANGUAGE_NONE][0]['value'];
  } else {
    $file_attachment_value = $form_state['values']['field_file_attachments'][LANGUAGE_NONE]['form']['field_file_attachment'][LANGUAGE_NONE][0]['fid'];
    $file_embed_value = $form_state['values']['field_file_attachments'][LANGUAGE_NONE]['form']['field_embed_google_doc'][LANGUAGE_NONE][0]['value'];
  }
  if ( !(empty($file_attachment_value) xor empty($file_embed_value)) ) {
    form_error($entity_form, t('Please attach a file OR embed a google doc. You can\'t do both on the same file attachment.'));
  }
}

This wasn't intended to change the behaviour described above though.

mstef’s picture

Looks like hook_node_validate() gets ignored..

dcam’s picture

Status: Active » Closed (outdated)

I'm closing old IEF support issues. Please feel free to re-open the issue if you still require assistance.