> I a username field and a check to see if it is empty, so my
> html->tagErrorMsg message says "You must fill out username" when they
> don't fill it out, but what if I want to check if username already
> exists? I check to see if that username is in the database, and use
> invalidate() to flag the error message, but now I would want it to say
> "Username already exists". How would I do this?
in your view you need another tagErrorMsg in adddtion to the standard
error msg (I assume you have)
<?php echo $html->tagErrorMsg('User/username', 'username is missing or
invalid'); ?>
<?php echo $html->tagErrorMsg('User/username_unique', 'Sorry, this
username is already in use'); ?>
then in your User model
function beforeValidate()
{
// check username is unique
if ($this->findByUsername($this->data[$this->name]['username']))
{
$this->invalidate('username_unique');
}
return true;
}
I also use this technique to check for a unique email address. You may
wish to check to see what method (of the controller) is in use,
because if you're doing an edit, you don't want to check for a unique
username or email address (unless of course they have choosen a
differnent username or email address to their existing data). I've
done this by adding an extra hidden var to the view, but there may be
a much more elegant way (perhaps using $this->action that's available
in the controller).
hth
jon
--
jon bennett
t: +44 (0) 1225 341 039 w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---