I'm currently writing regex rules to validate a field and I want to display a different error message depending on what the user did wrong, so I'm creating multiple regex rules. However currently the way this module works is it matches one rule at a time and displays that error message, it'd be great if it could match multiple rules at once and display all the error messages so the user doesn't fix the one error message they receive, submit the form and then get another error message for the same field.

Comments

g089h515r806’s picture

You could use php validator to solve this issue.

SeanBannister’s picture

Awesome, for sure.

Decided to do it the proper way in a module, read the guide at http://drupal.org/node/1553646

And just to start out I put this code into a custom module (haven't got the domain validation working yet). But its not showing up as a new rule in the interface. I know I'm doing something obviously wrong here, was just wondering if you could point me in the right direction.

$plugin = array(
  'label' => t('Domain name'),
  'description' => t("Verifies that user-entered data is a valid email address."),
  'handler' => array(
    'class' => 'custom_domain_validator',
  ),
);

class custom_domain_validator extends field_validation_validator {

  /**
   * Validate field.
   */
  public function validate() {
    if ($this->value != '' && (!valid_email_address($this->value))) {
      $this->set_error();
    }
  }
}
g089h515r806’s picture

Just copy a validator, rename it, change the logic of validate.

if you want to put it into a custom module, you need implement hook_ctools_plugin_directory:

/**
 * Implementation of hook_ctools_plugin_directory().
 */
function field_validation_extras_ctools_plugin_directory($module, $plugin) {
  if ($module == 'field_validation' && $plugin == 'validator') {
    return 'plugins/' . $plugin;
  }
}

Then you could put your validator file at the firectory that you specified.

SeanBannister’s picture

Thanks so much for your help, I started to work on this and almost got it working however then I realised I would also need to write integration with Clientside Validation and was a little stuck on time so just simplified my validation rule into one.

I do however think this would be really handy having the ability to output the result of multiple rules at once. Not only because then I wouldn't have to write Clientside Validation as well :)

I've also filled an issue at http://drupal.org/node/1804924 as Clientside Validation has a bug dealing with multiple validation rules at once.

g089h515r806’s picture

We set several error messages to one form element, but Drupal only display the first(or last) one.