I'm using the money module to handle currency data. A money field consists of a textbox for the numeric value and a select list for choosing the correct currency.

The money module includes the following validation to ensure that a numeric value has been entered when a currency has been selected:

<?php
if (!is_numeric($item['amount']) && $item['currency']) {
  $errors[$field['field_name']][$langcode][$delta][] = array(
    'error' => 'money_amount',
    'message' => t('%name: a valid amount is required when a currency is specified.', array('%name' => $instance['label'])),
  );
}
?>

However, when a money field is required, the "currency" part is automatically populated. This means that if a required money field is also an untriggered dependent, the above validation always fails because "currency" is populated but "amount" is not.

Conditional field doesn't seem to remove this field validation error, so the form can't be submitted.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

morbiD’s picture

As far as I can tell, this is down to execution order of validation hooks and callbacks.

The order of execution that I'm seeing is:

  1. money_field_widget_validate
  2. conditional_fields_dependent_validate
  3. money_field_validate
  4. conditional_fields_form_validate

So money's implemention of hook_field_validate() is executing after conditional_fields' validation callback, meaning the callback can't catch the validation error.

Not sure what the solution is yet.

morbiD’s picture

Title: Validation error with untriggered dependency (Money field module) » Untriggered dependency validation errors not removed if generated by hook_field_validate() implementation

I don't see how this can be fixed within the current system of detecting validation errors in a #element_validate callback and then removing them later in a #validate callback.

The problem is twofold:

  1. Implementations of hook_field_validate() seem to execute after #element_validate callbacks, so conditional_fields_dependent_validate() can't detect any validation errors generated by hooks.
  2. conditional_fields_form_validate() relies on $form_state['conditional_fields_untriggered_dependents'][$index]['errors'] being populated by conditional_fields_dependent_validate(), but there is no way to access $form_state after conditional_fields_dependent_validate() has executed.

Therefore, it seems some detection logic must be added to conditional_fields_form_validate(), rather than just skipping the dependent field when $form_state['conditional_fields_untriggered_dependents'][$index]['errors'] is empty.

morbiD’s picture

Status: Active » Needs review
FileSize
2.37 KB

Well, I wrote a patch which attempts to check if there are any validation errors for an untriggered dependency that haven't already been detected by conditional_fields_dependent_validate(), then adds them to the field's array of errors.

I tested very briefly in order to resolve the money field issue I described in the first post, but there could be consequences that I've missed for other use cases.

morbiD’s picture

Last patch could cause "Invalid argument" PHP errors. Here's a new one.

morbiD’s picture

Issue summary: View changes

Correction

peterpoe’s picture

Issue summary: View changes
Status: Needs review » Fixed

This was fixed by some other commit.

Status: Fixed » Closed (fixed)

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

stefan.korn’s picture

Could you explain which commit fixed this issue? I am still experiencing this issue with fields that make use of hook_field_validate (i. e. eMail Field or number field) using latest 7.x dev version. So I assume this is not fixed currently?

The patch from #4 is also not working.