When I save the information from a registration form I have some validation rules on the username and email fields (I’ve pasted the rules below). Validation is called automatically with the saveAll() function.
The problem is that the isUnique rule on the username field doesn’t work at all (doesn’t return any error). The other rules for this field work just fine, and the unique validation for the email field also works. I can’t seem to figure out why this is happening, when the two rules are basically the same.
var $validate = array(
'username' => array(
'isUnique' => array (
'rule' => 'isUnique',
'message' => 'This username already exists.'),
'custom' => array (
'rule' => array('custom', '/^[A-Za-z0-9,\.-_]*$/i'),
'message' => 'The username can only contain letters, numbers, _, - and .'),
'minLength' => array(
'rule' => VALID_NOT_EMPTY,
'message' => 'You must fill in the username.')
),
'email' => array(
'isUnique' => array (
'rule' => 'isUnique',
'message' => 'This email address already exists in our database.'),
'valid' => array (
'rule' => array('email', false),
'message' => 'Invalid email.'),
'minLength' => array(
'rule' => VALID_NOT_EMPTY,
'message' => 'You must fill in the email address.')
)
);
The “isUnique” rule looks for repeating values in other rows. Which will create a problem if the “isUnique” test is also the primary key. So, based on your rules/model above, if you look at the SQL dump when the rule is run, you will likely see something like
This obviously returns 0 or “unique” in the test. Fix would be to set the primary key equal to something other than the field you are testing the rule on. When you do that, the SQL dump should show something like
This will return the >0 when the field is not unique and the desired functionality will be restored.