Is there a way or feature to add a custom validation to the form upon submitting the "variation"? I've tried #element_validate but it doesn't seem to work.

Comments

fulat2k’s picture

Kinda got this working. But it doesn't highlight a specific form element. Still figuring that out :(

function my_form_alter(&$form, &$form_state, $form_id) {  

  if ($form_id == 'product_display_node_form') {
    $form['field_products'][LANGUAGE_NONE]['form']['#element_validate'][] = 'my_product_display_validation';
  }
}

function my_product_display_validation($element, &$form_state) {

  if (isset($form_state['values']['field_products'][LANGUAGE_NONE]['form']['commerce_price'][LANGUAGE_NONE][0]['amount'])) {
    $val = $form_state['values']['field_products'][LANGUAGE_NONE]['form']['commerce_price'][LANGUAGE_NONE][0]['amount'];

    if ((int) $val > (int) $product_price_limit) {
      form_error($element, t('Invalid price for product.  Please try a lower value.'));
    }
  }
}
bojanz’s picture

Category: feature » support
Status: Active » Fixed

Something like #1 is the right way to do it.

Instead of the generic form alter, I would use hook_inline_entity_form_entity_form_alter() that is documented in inline_entity_form.api.php

jonloh’s picture

Yes, after looking up and down, hook_inline_entity_form_entity_form_alter() is the right way to go. Thanks bojanz!

mrharolda’s picture

Is it possible to add a validator to the inline entity(/node) form instead of a single field? The inline documentation states "The entity form is first validated by its controller." but I can't seem to be able to extend/hook into that?

bojanz’s picture

Just add an #element_validate to the complete $entity_form that hook_inline_entity_form_entity_form_alter() gives you.
That key can be on any level of the form.

That's how IEF does it for itself as well.

mrharolda’s picture

@bojanz: Confirmed! tnx!

Too bad the form values are tucked away like this:

$value = $form_state['values'][$entity_reference_field_name][LANGUAGE_NONE]['form'][$field_name][LANGUAGE_NONE][$delta]['value'];

A reference to it makes it a bit more readable:

$entity_form = &$form_state['values'][$entity_reference_field_name][LANGUAGE_NONE]['form'];

$value = $entity_form[$field_name][LANGUAGE_NONE][$delta]['value'];
bojanz’s picture

Actually:

$entity_values = drupal_array_get_nested_value($form_state['values'], $entity_form['#parents']);

gives you the relevant values for that $entity_form.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

aaronbauman’s picture

Issue summary: View changes
Status: Closed (fixed) » Active

This method only works if the IEF "save" button is used.
The element validation doesn't fire if the parent form is saved before submitting the IEF.

Is there a method that works for both, rather than having to implement two separate validations?

aaronbauman’s picture

Status: Active » Closed (fixed)

sorry, i'm mistaken.
#element_validate should be sufficient.