简体   繁体   English

CakePHP $ this-> Plan-> validates()从非模型形式进行验证

[英]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). 我现在一天中的大部分时间都在工作,我无法在我的一生中使它正常工作(好吧,我可以使它工作1/2,但不能完全正确地工作)。

Basically, I am trying to use Validation on a search form 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 : ( ?? 当它为空白时,将显示NO错误,并且当我输入字母时,它似乎可以工作:(??

Does anyone know a trick to this odd behavior? 有人知道这种奇怪行为的窍门吗?

Try using the 'notEmpty' rule instead of the required/allowEmpty stuff. 尝试使用“ notEmpty”规则而不是必需/ allowEmpty的东西。

'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? 首先,当惯例说分数应小于小数时,为什么还要使用“ ApplicantAge”字段?

to answer your question the best way to do validation like that is http://book.cakephp.org/view/410/Validating-Data-from-the-Controller 要回答您的问题,最好的验证方法就是http://book.cakephp.org/view/410/Validating-Data-from-the-Controller

the other option is to do $this->Model->save($data, array('validate' => 'only')); 另一个选择是执行$ 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. 但是您对validate => only array的建议似乎可以解决问题。 This is how I got it working: 这就是我的工作方式:

plans_controller.php 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) plan.php(模型)

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. 现在,如果在ApplicateAge字段中未输入任何数据,则显示正确的消息。 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. 我几乎不知道仅在save()上的validate =>实际上仍在将数据保存到我的计划表中。

I was able to get it working using set(). 我能够使用set()使它工作。 Here is the code that completely solved the problem: 这是完全解决问题的代码:

plans_controller.php Plans_controller.php

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

plan.php (model): plan.php(模型):

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'),
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM