Cakephp: Validation Errors not appearing in data validation

给你一囗甜甜゛ 提交于 2019-12-13 03:41:21

问题


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

Model

// 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'
        )
    )
    );
}

Controller action

// /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());
            }
        }
    }
}

View....

// /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");?>

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....

What am I doing wrong?

Regards


回答1:


For me the problem is in your form tag.

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

If you change

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

to

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

The errors should appear corectly.

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(....));?>


来源:https://stackoverflow.com/questions/3154805/cakephp-validation-errors-not-appearing-in-data-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!