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' => '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