In 1.2 you can use Model::isUnique() method.
var $validate = array(
'username' => array(
'Username must be between 6 and 255 characters long.'
=> array(
'rule' => array('between', 6, 255),
'required' => true,
),
'Name must be unique.' => array(
'rule' => array('isUnique', 'name'),
'on' => 'create'
),
),
'profile' => array(
'Profile must be unique.' => array(
'rule' => array('isUnique', 'profile'),
'on' => 'create'
),
),
);
On Apr 20, 6:15 pm, simonb <[EMAIL PROTECTED]> wrote:
> Assuming you are using 1.2
> I have created this snippet in the model. Could be but in app_model if
> it is required.
>
> //checks to see if the given fieldname is unique with the model
> function checkUnique($data, $fieldName) {
>
> $valid = false;
> $this->recursive = -1;
> if(isset($fieldName) && $this->hasField($fieldName))
> {
> $valid = $this->isUnique(array($fieldName => $data));
> }
> return $valid;
> }
>
> the validate array within the model can be as follows. Notice the
> multiple validation rules for email.
> The on=> 'create' only validates this rule on an insert into the
> database.
> The message will automagically appear in the view. No need for the
> tagErrorMsg. just ensure you are using the html Helper.
>
> var $validate = array(
> //password must be at least 6 characters and consist of
> only
> alphanumeric
> 'password' => array(
> 'rule' => array('custom',
> '/[a-z0-9]{6,}$/i'),
> 'message' => 'Only letters and numbers,
> min 6 characters'
> ),
> //email address must exist and must be a unique
> value within the
> Customers.email field.
> 'email' => array(
> 'email_valid' => array(
> 'rule' => 'email',
> 'message' => 'Valid email address required',
> ),
>
> 'email_unique' => array(
> 'rule' => array('checkUnique', 'email'),
> 'message' => 'Email address already used.',
> 'on' => 'create'
> )
>
> ),//end email
>
> On Apr 20, 4:21 pm, Chez17 <[EMAIL PROTECTED]> wrote:
>
>
>
> > I am having issues making sure that a field is unique. I am beginner
> > so please go easy on me. Right now, I am trying to create a simple
> > login system where the user name name and profile name have to be
> > unique. I have been cutting and pasting some stuff together and I
> > can't seem to get this to work out. Here is a sample of my 'create'
> > view:
>
> > <label for="profile">Profile Name:</label>
> > <?php echo $html->password('User/profile', array('size' => 20)); ?>
> > <?php echo $html->tagErrorMsg('User/profile', 'You need to enter a
> > profile name.') ?>
>
> > Now, here is my validation function:
>
> > var $validate = array(
> > 'username' => VALID_NOT_EMPTY,
> > 'password' => VALID_NOT_EMPTY,
> > 'profile' => VALID_NOT_EMPTY
> > );
> > When I tried use custom rules and try to make the validate array check
> > for uniqueness, my message gets lost. It will keep displaying the
> > 'You need to enter a profile name' error message, not the one I
> > specify in the validate array. So now I am trying to do some custom
> > validation, but I don't understand the find() function. I couldn't
> > find one concrete example of how to use it. I am too much of a
> > beginner to understand the API that they put out.
>
> > I would like to know how to solve my problem both ways. How can I get
> > the message from the validate function to my view? Also, how can I
> > write a custom find() statement so I can check if a profile name is
> > already in the database?
>
> > Thanks for your time,
> > Da
>
> ve
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" 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
-~----------~----~----~----~------~----~------~--~---