0
votes

I'm programming a application using cakephp but the Method 'MyModel'->validates(array('fieldList) => array('myfield')) is not working. The related validation rule in my model is ignored.

My Controller function is as follows:

function _processaddbasic(){
$this->Caught->set($this->data);
if ($this->Caught-validates(array('fieldList' => array('type_id')))){
   return true;
}
return false;
}

My Modelpart is as follows:

public $validate = array(
'type_id'=> array(
            'rule' => 'notEmpty',
            'required' =>true,
            'allowEmpty' => false,
)
)

After executing the code with invalid (empty) data, the related part in my controller returns always true. So, where i'm wrong?

2
I figured out, that the 'required' tag of the type_id field inside the HTML code is missing. How can I ensure that the tag 'requried' is available? - user2610983

2 Answers

0
votes

A few suggestions:

Suggestion 1

It looks like you have a typo in your code.

if ($this->Caught-validates(array('fieldList' => array('type_id')))){

should be

if ($this->Caught->validates(array('fieldList' => array('type_id')))){

(it's missing a '>')

Suggestion 2

If you use CakePhp 2.x it may be better to have

$this->Caught->set($this->request->data);

instead of

$this->Caught->set($this->data);

Suggestion 3

Your model may be "dirty" with data from other calls in your program. To reset it before adding data to it you'll have to call create; see docs;

$this->Caught->create();

Suggestion 4

Check your Model class name and file name and make sure they are singular and that they match and that there are no typos.

0
votes