Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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?
Comment | File | Size | Author |
---|---|---|---|
Capture.PNG | 66.75 KB | mshepherd |
Comments
Comment #1
mshepherd CreditAttribution: mshepherd commentedFor 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:
This wasn't intended to change the behaviour described above though.
Comment #2
mstef CreditAttribution: mstef commentedLooks like hook_node_validate() gets ignored..
Comment #3
dcam CreditAttribution: dcam as a volunteer commentedI'm closing old IEF support issues. Please feel free to re-open the issue if you still require assistance.