views:

33

answers:

2

Hello all

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";
    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

+1  A: 

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);
DavidYell
+1  A: 

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'
        )
    )
);
bancer
This does work. Now I am seeing the $this->Model->validates() returning false, but I still don't see the error messages appearing. Do I need to use the invalidate() method inside my model?
ShiVik
I just checked the invalidFields array and it is displaying the invalid fields there. But the error messages are not appearing in form....
ShiVik
That is strange. Try `$form->error('email');`
bancer
$form->error('email'); does nothing noticeable.
ShiVik
I got the answer to validation error problem over herehttp://stackoverflow.com/questions/3154805/cakephp-validation-errors-not-appearing-in-data-validation
ShiVik