【问题标题】:Model validation problem in CakephpCakephp 中的模型验证问题
【发布时间】:2010-06-30 07:43:38
【问题描述】:

我正在尝试使用 cakephp 模型实现表单验证。这是我的代码 sn-ps...

型号

// File: /app/models/enquiry.php
class Enquiry extends AppModel {
    var $name = "Enquiry";
    var $useTable = false;
    var $_schema = array(
        "name"      => array( "type" => "string", "length" => 100 ), 
        "phone"     => array( "type" => "string", "length" => 30 ), 
        "email"     => array( "type" => "string", "length" => 255 )
    );
    var $validate = array(
        "name"  => array(
            "rule" => array( "minLength" => 1 ),
            "message" => "Name is required"
        ),
        "email" => array(
            "emailFormat" => array( "rule" => "notEmpty", "last" => true, "message" => "Email is required" ),
            "emailNeeded" => array( "rule" => array( "email", true ), "message" => "Must be a valid email address" )
        )
    );
}

控制器动作

// /app/controllers/nodes_controller.php
class NodesController extends AppController {

    var $name = "Nodes";
    var $uses = array( "Enquiry" );
    function enquire() {
        if ( $this->data ) {
        $this->Enquiry->set( $this->data );
        $this->Enquiry->set( "data", $this->data );
        if ( $this->Enquiry->validates(
                array( "fieldList" => array("name", "email") )
            ) ) {
            // .....
            }
        }
    }
}

查看....

// /app/views/nodes/enquire.ctp
<?php echo $form->create("Node", array("action" => "ask")); ?>
<?php echo $form->input( "name", array( "label" => "Name" ) ); ?>
<?php echo $form->input( "email", array( "label" => "Email" ) ); ?>
<?php echo $form->input( "phone", array( "label" => "Phone" ) ); ?>
<?php echo $form->end("Send");?>

我的问题:提交时,表单永远不会验证。 validate 函数每次都返回 true,即使我没有在表单中输入任何内容。

我做错了什么?

问候

【问题讨论】:

    标签: cakephp validation models


    【解决方案1】:

    由于您只有两个验证规则,因此在validates() 中列出要验证的这两个字段是没有意义的。试试这个方法:

    function enquire(){
        if($this->data){
            $this->Enquiry->set( this->data);
            if($this->Enquiry->validates()){
                // it validated logic
            }else{
                // didn't validate logic
            }
        }
    }
    

    您的验证数组应该是(遵循语法一致性):

    var $validate = array(
        'name' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'Name is required'
            )
        ),
        'email' => array(
            'emailFormat' => array(
                'rule' => 'notEmpty',
                'message' => 'Email is required'
            ),
            'emailNeeded' => array(
                'rule' => array('email', true),
                'message' => 'Must be a valid email address'
            )
        )
    );
    

    【讨论】:

    • 这确实有效。现在我看到 $this->Model->validates() 返回 false,但我仍然没有看到出现的错误消息。我需要在模型中使用 invalidate() 方法吗?
    • 我刚刚检查了 invalidFields 数组,它在那里显示了无效字段。但是错误消息没有出现在表单中......
    • 我在这里得到了验证错误问题的答案stackoverflow.com/questions/3154805/…
    【解决方案2】:

    我认为您将不正确的数据传递给验证。设置了$this-&gt;data 我会打电话

    $this->Model->validates();
    // or
    $this->Model->validates($this->data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      相关资源
      最近更新 更多