In the manual I see a code snippet for custom validation in a case
when the application needs to validate that a username doesn't already
exist in the database.
This validation is written inline in the controller. Shouldn't custom
validation methods be handled on the model as well?
For example...
The controller would look like...
<?php
class UsersController extends AppController
{
function create()
{
// Check to see if form data has been submitted
if (!empty($this->data['User']))
{
//See if a user with that username exists
$user = $this->User->validateUsername($this->data['User']
['username']);
//Try to save as normal, shouldn't work if the field was
invalidated.
if($this->User->save($this->data))
{
$this->redirect('/users/index/saved');
}
else
{
$this->render();
}
}
}
}
?>
Then in the model it'd look something like...
<?php
class User extends AppModel
{
var $name = 'User';
var $validate = array(
'username' => '/[a-z0-9\_\-]{3,}$/i',
'password' => VALID_NOT_EMPTY,
'email' => VALID_EMAIL,
'born' => VALID_NUMBER
);
function validateUsername($username)
{
$user = $this->User->findByUsername($username);
if (!empty($user['User']['username']))
{
return $this->User->invalidate('username_unique'); //
populates tagErrorMsg('User/username_unique')
}
}
}
?>
The reason I ask is because the example that's in the manual runs into
a bit of a usability problem.
When a user has an empty field for username an error is returned that
$username_unique is not declared.
When a user has a username that is not unique but an invalid email is
entered, the username is not validated until AFTER the email has been
successfully validated.
So, someone could potentially fill in all the correct information
except an email. Think they just need to fix an email and fix it.
The page is THEN reloaded saying the username is not valid. For a lot
of users they will think that their last attempt was submitted and
that they are not stuck in some kind of registration loop where they
couldn't get the username they think they rightfully should have had.
Any ideas on how to get around this? I searched and didn't see
anything for 1.1. It looks like model based validation is addressed
in 1.2? Thanks in advance.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---