开发者

Model validation problem in Cakephp

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" => 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" )
        )
    );
}

Controller action

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

    var $name = "Nodes";
  开发者_StackOverflow社区  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") )
            ) ) {
            // .....
            }
        }
    }
}

View....

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

My Problem: On submitting, the form never validates. The validate function returns true everytime, even if I do not enter anything in the form.

What am I doing wrong?

Regards


As you have only two validation rules there is no sense to list those two fields to be validated in the validates(). Try this way:

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

Your validation array should be (follow syntax consistency):

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


I think that your passing incorrect data into validates. Having set $this->data I would just call

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜