Cakephp:验证错误不会出现在数据验证 [英] Cakephp: Validation Errors not appearing in data validation

查看:121
本文介绍了Cakephp:验证错误不会出现在数据验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用cakephp模型实现表单验证。这是我的代码段...

I am trying to implement form validation using cakephp models. Here are my code snippets...

模型

// 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' => 'notEmpty',
        'required' => true,
        'message' => 'Name is required'
    ),
    'email' => array(
        'emailFormat' => array(
            'rule' => 'notEmpty',
            'required' => true,
            'message' => 'Email is required'
        ),
        'emailNeeded' => array(
            'rule' => array('email', true),
            'required' => 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 );
            if ( $this->Enquiry->validates() ) {
            // .....
            } else {
                 $this->set("errors", $this->Enquiry->invalidFields());
            }
        }
    }
}

查看....

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

我的问题:提交时,表单验证发生, > validates方法返回false,但是验证错误永远不会显示。我已经检查了invalidFields()返回的数组,所有在模型中设置的错误消息都存在,但它们没有显示....

My Problem: On submitting, the form validation occurs, the Model->validates method returns false, but the validation errors never get displayed. I have checked the array returned by invalidFields(), all the error messages I set in the model are there, but they are not getting displayed....

我做错了?

回答

推荐答案

基本上,如果验证查询,公司字段应该使用以下名称:data [Inquiry] [name]而不是data [Node] [name]

Basically if you validating Enquiry, the firm fields should with following names: data[Enquiry][name] instead of data[Node][name].

如果您更改

<?php echo $form->create("Node", array("action" => "ask")); ?>

<?php echo $form->create("Enquiry", array("action" => "ask")); ?>

错误应该出现在核心。

如果您的表单在Node和Inquiry字段之间有混合,那么只需将模型名称放在字段名称前面,如下所示:

If your form have mix between Node and Enquiry fields, then just put the name of the Model in front of the field name like this:

<?php echo $form->input("Enquiry.email", array(....));?>

这篇关于Cakephp:验证错误不会出现在数据验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆