views:

26

answers:

1

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

+1  A: 

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(....));?>
Nik
and we have found the answer :) Thanks man.
ShiVik