I manage this via the $validate variable in the model and a custom
function in AppModel, e.g.:

var $validate = array(
        'name' => array(
                'name' => array(
                        'rule' => 'notEmpty',
                        'message' => 'Please enter a name for this widget.',
                        'required' => true,
                        'last' => true
                        ),
                'unique' => array(
                        'rule' => array('checkUnique', array('name', 
'client_id')),
                        'message' => 'This widget already exists.'
                )
        )
);

You'll notice that in the part that checks for uniqueness this example
checks across two fields (effectively against a multi-field unique
index). You can check against a single field by just using:

'rule' => array('checkUnique', array('name'))

Then, you'll need to put the checkUnique function into your AppModel:

function checkUnique($data, $fields) {
        if (!is_array($fields)) {
                $fields = array($fields);
        }

        foreach($fields as $key) {
                $tmp[$key] = $this->data[$this->name][$key];
        }

        return $this->isUnique($tmp, false);
}

Hope this helps.

On Nov 20, 8:54 am, jiru <[email protected]> wrote:
>  I have a dropdown list in my form.
> How can i give it a validation criteria such that, if i try to enter
> data from it to database it should check for uniqueness.
> If already an entry is there in the database for it, it should display
> a message.
> We can do it for a text area using validation in the model.How it can
> be done for dropdown list?

--

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=.


Reply via email to