Hi,

Been playing with your module and trying to add clientside validation to the user registration form like so:

function customizations_form_alter(&$form, &$form_state, $form_id) {
switch($form_id) {
		case 'user_register':
			// add clientside validation
			$form['mail']['#rules'] = array(
				'email'
			);
			$form['pass']['#rules'] = array(
				array(
					'rule'  => 'regexp[/^[a-zA-Z0-9]{6,}$/]',
					'error' => 'test'
				)
			);
		break;
}

But whenever I try adding a regexp rule, no matter what the regex is it throws an error;

warning: json_decode() expects parameter 1 to be string, array given in clientside_validation.module on line 120.
warning: Invalid argument supplied for foreach() in clientside_validation.module on line 123

What am I doing wrong? Or is this a bug?

Cheers,
Bartezz

PS. I've already applied the patch commas_in_regex-1425268-3.patch from the fapi_validation issue queue to solve issues with commas in regexes...

Comments

jelle_s’s picture

Version: 6.x-1.9 » 6.x-1.x-dev
Status: Active » Fixed

There was indeed a small bug in the way we handled regex validations. This should be fixed in the latest dev version.
There's also a mistake in your code. The $form['pass'] field is of the type 'password_confirm'. When the form is built it is split up in two seperate fields ('Password' and 'Confirm password'). FAPI Validation rules must be defined on the fields themselves.

So here's the correct code you should use with our latest dev version:

function customizations_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id) {
      case 'user_register':
        $form['#after_build'][] = '_customizations_add_rules';
        // add clientside validation
        $form['mail']['#rules'] = array(
          'email'
        );
        
      break;
  }
}

function _customizations_add_rules($form) {
  $form['pass']['pass1']['#rules'] = $form['pass']['pass2']['#rules'] = array(
    array(
      'rule'  => 'regexp[/^[a-zA-Z0-9]{6,}$/]',
      'error' => 'test'
    )
  );
  return $form;
}
bartezz’s picture

Hi Jelle,

Thanx for your reply will try and post back if dev doesn't solve the issue for me.

Also thanx for your pointer, yet I'm not using password_confirm :) I only use a single password field.

/**
 * Implementation of hook_form_user_register_alter().
 */
function customizations_form_user_register_alter(&$form, &$form_state) {
  $form['pass']['#type'] = 'password';
	$form['pass']['#title'] = t('Password');
	$form['pass']['#description'] = t('It is recommended to choose a password that contains at least six characters. It should include numbers, punctuation, and both upper and lowercase letters.');
}
bartezz’s picture

Status: Fixed » Active

Hi Jelle,

Well the error mentioned above is gone after using dev... but I have a new issue now;

$form['pass']['#rules'] = array(
				array(
					'rule'  => "regexp[/^.*(?=.{6,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W]).*$/]",
					'error' => 'test'
				)
			);

Error given;

warning: preg_match() [function.preg-match]: No ending delimiter '/' found in clientside_validation.module on line 125.

I tested the regex and it should be good?

When I dsm($param); in the _clientside_validation_ajax_call() { function of your module I get this;

... (Object) stdClass
expressions (Array, 1 element)
0 (String, 1 characters ) /
messages (Array, 1 element)
0 (String, 4 characters ) test

Is this a bug?

Cheers

bartezz’s picture

Status: Active » Needs review
StatusFileSize
new668 bytes

Ok found the bug!

In clientside_validation_fapi.module on line #108 it says $params[0] this sould simply be $params as $params is a string and not an array.

 if ($callback == 'fapi_validation_rule_regexp') {
    $expressions['fapi_validation_rule_regexp'] = $params;
    $type = 'regexp';
}

With $params[0] only the first character of the string is returned resulting in the error mentioned before. Patch attached!

Cheers

bartezz’s picture

Ok, I think the error in my case is because I'm using jquery update module with jquery version set to 1.7, also read this post;
http://drupal.org/node/1067290#comment-5597442

Cheers

jelle_s’s picture

Status: Needs review » Fixed

Should be fixed in the latest stable version (6.x-1.30)

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

Extended with PS