0
votes

I am trying to build my first CakePHP application and I am having some trouble with the form validation not showing when I try to add to the database. On submitting the form I get the flash message (as shown in the controller below), but the individual validation does not show.

Model:

class Client extends AppModel {

public $validate = array (

'businessName' => array (
    'mustNotBeBlank'=>array(
        'rule'=>'notEmpty',
        'message'=>'Must not be empty'
                ),
    'mustBeUnique'=>array(
        'rule'=>'isUnique',
        'message'=>'Name already registered'
                ),
    'maxLength50'=>array (
        'rule'=>array('maxLength', 50),
    'message'=>'Exceeds 50 Characters'
                )
    ),

'address1' => array (
    'mustNotBeBlank'=>array(
        'rule'=>'notEmpty',
        'message'=>'Must not be empty'
                ),
    'maxLength50'=>array (
        'rule'=>array('maxLength', 50),
    'message'=>'Exceeds 50 Characters'
                )
    ),

'address2' => array (
    'mustNotBeBlank'=>array(
        'rule'=>'notEmpty',
        'message'=>'Must not be empty'
                ),
    'maxLength50'=>array (
        'rule'=>array('maxLength', 50),
    'message'=>'Exceeds 50 Characters'
                )
    ),
'address3' => array (
    'mustNotBeBlank'=>array(
        'rule'=>'notEmpty',
        'message'=>'Must not be empty'
                ),
    'maxLength50'=>array (
        'rule'=>array('maxLength', 50),
    'message'=>'Exceeds 50 Characters'
                )
    ),

'postCode' => array (
    'mustNotBeBlank'=>array(
        'rule'=>'notEmpty',
        'message'=>'Must not be empty'
                ),
    'postCode' => array(
            'rule'=>array('postal', null, 'uk'),
            'message'=>'Please enter a valid postcode'
                    )
    ),

'telephone1' => array (
    'mustNotBeBlank'=>array(
        'rule'=>'notEmpty',
        'message'=>'Must not be empty'
                ),
    'maxLength11'=>array (
        'rule'=>array('maxLength', 11),
    'message'=>'Exceeds phone number length'
                ),
    'mustBeNumber'=>array(
            'rule' =>'numeric',
            'message' => 'Must be a number'
                    )
    ),

'telephone2' => array (
    'maxLength11'=>array (
        'rule'=>array('maxLength', 11),
    'message'=>'Exceeds phone number length'
                ),
    'mustBeNumber'=>array(
            'rule' => 'numeric',
            'message' => 'Must be a number'
                    )

    ),

'email' => array (
        'rule'    => array('email', true),
        'message' => 'Please supply a valid email address.'
),

    'domain' => array (
        'rule' => 'url',
        'message' => 'Please supply a valid email address.'
)
);

}

View (html removed for simplicity):

echo $this->form->create();
echo $this->Form->input('businessName', array('label' => 'Business Name'));
echo $this->Form->input('address1', array('label' => 'Address'));
echo $this->Form->input('address2', array('label' => ''));
echo $this->Form->input('address3', array('label' => ''));
echo $this->Form->input('postCode', array('label' => 'Postcode'));
echo $this->Form->input('telephone1', array('label' => 'Landline'));
echo $this->Form->input('telephone2', array('label' => 'Mobile'));
echo $this->Form->input('email', array('label' => 'Email'));
echo $this->Form->input('domain', array('label' => 'Domain'));

$options = array(
    'label' => 'Add',
    'div' => array(
        'class' => 'btn btn-primary',
    )
);
echo $this->Form->end($options);

Add action in controller:

public function add() {
    if ($this->request->is('post')) {
        $this->Client->create();
        if ($this->Client->save($this->request->data)) {
            $this->Session->setFlash('Client has been saved.');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('Unable to add your post.');
        }
    }
}

Thanks once again for any help.

1
Have a look here first for you database names. book.cakephp.org/2.0/en/getting-started/… - Jelmer

1 Answers

7
votes

Your form creation tag is

echo $this->form->create();

but should be

echo $this->Form->create();

Notice the upper case F. Just tried both locally and the lowercase ->form fails to report error messages.