开发者

CakePHP $this->Plan->validates() Validation from non-model form

I've been at this most of the day now, and I cannot get this working for the life of me (well I can get it 1/2 working but not fully correctly).

Basically, I am trying to use Validation on a search 开发者_StackOverflowform field like so:

if(isset($search['ApplicantAge']) && !empty($search['ApplicantAge'])) {
        if ($this->Plan->validates()) { 
        $ApplicantAge = $search['ApplicantAge'];
        }
    } 

And here is my model code:

...

'ApplicantAge' => array(
'required' => true,
'allowEmpty' => false,
'rule' => 'numeric',
'message' => 'A valid Age is required. Please enter a valid Age.'),

...

The validation is working BUT when I enter a number (numeric), it displays my error! And when it's blank NO error displays, and when I enter letters it seems to work : ( ??

Does anyone know a trick to this odd behavior?


Try using the 'notEmpty' rule instead of the required/allowEmpty stuff.

'ApplicantAge' => array(
'applicant-age-numeric'=> array(
    'rule' => 'numeric',
    'message' => 'A valid Age is required. Please enter a valid Age.'
    ),
'applicant-age-not-empty'=> array(
    'rule' => 'notEmpty',
    'message' => 'This field cannot be left blank'
    )
)


firstly why are you using the field 'ApplicantAge' when the conventions say its should be lower case under scored?

to answer your question the best way to do validation like that is http://book.cakephp.org/view/410/Validating-Data-from-the-Controller

the other option is to do $this->Model->save($data, array('validate' => 'only'));


The manual did not assist me at all : (

But your suggestion on the validate => only array seems to have done the trick. This is how I got it working:

plans_controller.php

if (isset($search['ApplicantAge'])) {
        $this->Plan->save($search, array('validate' => 'only'));
        if ($this->Plan->validates($this->data)) {
            $ApplicantAge = $search['ApplicantAge'];
        }
    }

plan.php (model)

var $validate = array(
    'ApplicantAge'   => array(
        'applicant-age-numeric'   => array(
            'rule'    => 'numeric',
            'message' => 'A valid Age is required. Please enter a valid Age.'),
        'applicant-age-not-empty' => array(
            'rule'    => 'notEmpty',
            'message' => 'This field cannot be left blank'),
    ),

Now, if no data is entered in the ApplicateAge field, the proper message is displayed. And if a non-numeric is entered, the correct message is also displayed.

This was a lot more challenging than I thought it would be!


For the record, I'll make a correction to my earlier accepted post. Little did I know the validate => only on the save() was actually still saving data to my plans table.

I was able to get it working using set(). Here is the code that completely solved the problem:

plans_controller.php

if (isset($search['ApplicantAge'])) {
        $this->Plan->set($this->data);
        if ($this->Plan->validates()) {
            $ApplicantAge = $search['ApplicantAge'];
        }
    }

plan.php (model):

var $validate = array(
    'ApplicantAge'   => array(
        'applicant-age-numeric'   => array(
            'rule'    => 'numeric',
            'message' => 'A valid Age is required. Please enter a valid Age.'),
        'applicant-age-not-empty' => array(
            'rule'    => 'notEmpty',
            'message' => 'This field cannot be left blank'),
    )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜