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