0

Is it possible to alter the validation function when using inline_entity_form ?

Here is an example module that I'm using where I use devel to validate if I can hook into drupals and IEF validate functions (as pointed out by Shawn Conn).

function mymodule_inline_entity_form_entity_form_alter(&$entity_form, &$form_state)
{
  $entity_form['#element_validate'] = array('mymodule_inline_entity_form_entity_form_validate');
}


function mymodule_inline_entity_form_entity_form_validate(&$entity_form, &$form_state)
{
  // No effect
  form_set_error('field_price', '!Validate');
  form_set_error('field_bucket_list', '!Validate');
}

I have one content type which has a title and a decimal field called field_price and a second type which has the IEF field (called field_bucket_list).

2 Answers 2

0

Yes, IEF calls drupal_alter() after upon building a form. This enables any custom module to invoke hook_inline_entity_form_entity_form_alter() to alter it.

/**
 * Implements hook_inline_entity_form_entity_form_alter().
 */
function MYMODULE_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
  // Replace validation callback.
  $entity_form['#element_validate'] = array(
    'MYMODULE_inline_entity_form_entity_form_validate'
  );
}

function MYMODULE_inline_entity_form_entity_form_validate(&$entity_form, &$form_state) {
  // @TODO: Add extra validation in front of IEF validation handler.
  inline_entity_form_entity_form_validate($entity_form, $form_state);
}
2
  • Thank you very much for you answer. It seems that I can hook into the alter function but not into the validate function. I'm using Devel to validate if the hooks work but I get no output when submitting new nodes. I can see that the internal validate functions are run within drupal since it rejects text if I use a decemal field. So I tried to use hook_node_validate but I cant get form_set_error to work there -_- . Any thoughts?
    – Fever
    Aug 19, 2016 at 18:44
  • Saw my mistake, edited my initial post
    – Fever
    Aug 19, 2016 at 22:46
1

At last I got it to work. Here is the final code where the error was that I did not fully understand how to use form_set_error to point at the field I wanted to validate.

function mymodule_inline_entity_form_entity_form_alter(&$entity_form, &$form_state)
{
  $entity_form['#element_validate'] = array('mymodule_inline_entity_form_entity_form_validate');
}


function mymodule_inline_entity_form_entity_form_validate(&$entity_form, &$form_state)
{
  form_set_error('field_bucket_list][und][form][field_price', '!Validate');
  inline_entity_form_entity_form_validate($entity_form, $form_state);
}

The field I wanted to validate is field_price and is located as values->field_bucket_list->und->form->field_price in $form_state, which I got from using Devel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.