I'm using Drupal8's inline entity form and is having issue with writing custom validation. I found few links and wasn't helpful at all. Does anyone know how to handle this?


2 Answers 2


You have to use inline form alter for custom validation in module shown below

 * {@inheritdoc}
 * Implements hook_form_alter().
function module_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {

  // Added custom validation 
  if ($entity_form['#bundle'] == 'bundle_name') {
    $entity_form['#element_validate'][] = '_custom_validate_function';

And implement that validation like below.

 * Custom validation.
function _custom_validate_function(&$form, FormStateInterface $form_state) {

 $triggering_element = $form_state->getTriggeringElement();
 $values = $form_state->getValues();
 // or you can use $form_state->getValue('field_name')
 // Validate your functionality

 // Set Error for form
 $form_state->setError($form, t("message"));
 // Note: $name may be unknown in $form_state and
 // $form_state->setErrorByName($name, $message) may suppress the error message.
 // hence use above $form_state->setError()

Set Error on inline entity forms is improved by specifying the actual element like this

$form_state->setError($form['field_name']['widget'], t("message"));

The above will successfully display the message inline.

Your Answer

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

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