When I export my field validation rules to features, I end up with this error.

Warning: Invalid argument supplied for foreach() in element_children() (line 6288 of /Users/jucallme/Sites/systemseed/concern/7.x-1.x/build/includes/common.inc

that is called from clientside_validation_field_validation.module [line] => 30

Here is the export of one of the rules.

/**
 * Implements hook_default_field_validation_rule().
 */
function MY_FEATURE_MODULE_default_field_validation_rule() {
  $export = array();

  $rule = new stdClass();
  $rule->disabled = FALSE; /* Edit this to true to make a default rule disabled initially */
  $rule->api_version = 2;
  $rule->rulename = 'First name can only contain alphabetic characters.';
  $rule->name = 'first_name_alpha_billing';
  $rule->field_name = 'field_cw_profile_firstname';
  $rule->col = 'value';
  $rule->entity_type = 'commerce_customer_profile';
  $rule->bundle = 'billing';
  $rule->validator = 'field_validation_regex_validator';
  $rule->settings = array(
    'data' => '^[a-zA-Z]*$',
    'errors' => 0,
  );
  $rule->error_message = 'First name can only contain alphabetic characters.';
  $export['first_name_alpha_billing'] = $rule;

  return $export;
}

Comments

attiks’s picture

This sounds like a problem with field validation? Or goes the problem wawy if you disable clientside_validation?

roam2345’s picture

Status: Active » Closed (won't fix)

opened ticket against field_validation http://drupal.org/node/1778002

roam2345’s picture

Status: Closed (won't fix) » Active

I have narrowed it down to this line.

/**
 * Regular form.
 */
function clientside_validation_form_after_build(&$form, &$form_state) {
  static $js_rules = array();

  clientside_validation_settings_current_form($form['#clientside_validation_settings']);
  drupal_alter("clientside_validation_form", $form, $form_state, $js_rules);         // <----- this is causing the error.

  if (!empty($js_rules)) {
    $settings = array();
    _clientside_validation_add_general_settings($settings, $form);
    _clientside_validation_add_special_rules($js_rules, $settings, $form);
    clientside_validation_add_js_settings($settings);
  }

  return $form;
}
mrfelton’s picture

I think the problem may be a problem with commerce, where the fields lay deeper within the form array (within individual checkout panes), and not at the top level of the form as with more standard entity forms. On line 23 of clientside_validation_field_validation.module, you call $element = &$element[$rule['rule']['field_name']];:

/**
 * Implements hook_clientside_validation_form_alter().
 */
function clientside_validation_field_validation_clientside_validation_form_alter(&$form, &$form_state, &$js_rules) {
  $field_rules = array();
  clientside_validation_field_validation_find_rules($form, $field_rules);
  foreach ($field_rules as $rule) {
    $element = &$form;
    // Field validation 1.x
    if (function_exists('field_validation_get_bundle_rules')) {
      foreach ($rule['field']['#parents'] as $parent) {
        $element = &$element[$parent];
      }
      clientside_validation_field_validation_after_build_recurse($form['#id'], $element, $form_state, $rule['rule'], $js_rules);
    }
    // Field validation 2.x
    else {
      $element = &$element[$rule['rule']['field_name']];
      $field = field_info_field($rule['rule']['field_name']);
      $languages = field_available_languages($rule['rule']['entity_type'], $field);
mrfelton’s picture

Status: Active » Needs work
StatusFileSize
new1.56 KB

Attached patch sort of fixes the problem, in that the errors go away, and validation continues to work normally on standard entity forms. But now, in the case of commerce, the validations seem messed up (wrong element validating against wrong rules).

attiks’s picture

Assigned: Unassigned » jelle_s
qasimzee’s picture

subscribing

roam2345’s picture

Here is a patch that correctly traverses the form and finds the elements (it now does not matter where they hide).

roam2345’s picture

Status: Needs work » Needs review

change the status on this.

roam2345’s picture

fixed small issue with patch moved validation calls inside path check.

jelle_s’s picture

Status: Needs review » Needs work

After applying this patch I get a fatal error going to a form with (admittedly quite a lot of) field validation rules:

Fatal error: Maximum function nesting level of '500' reached, aborting! in /sites/all/modules/clientside_validation/clientside_validation_field_validation/clientside_validation_field_validation.module on line 51
roam2345’s picture

Now limiting the recursive search to 3 levels of the array, assume 3 should be sufficient.

roam2345’s picture

Status: Needs work » Needs review

status change

cellar door’s picture

This worked for me with an issue I was having with Profile2reg and Field Validation. I had a text field validated w/ regex and it was causing this error but after this patch it worked. Patch didn't apply cleanly but that's probably because it's against dev. Either way it's functioning now.

Thanks!

mrfelton’s picture

Updated to work with latest codebase

attiks’s picture

Status: Needs review » Fixed

Committed to latest dev version, thanks

jelle_s’s picture

Assigned: jelle_s » Unassigned

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

more context