Reputation: 6027
I have only recently started using CakePHP and have been unable to get validation to work in a contact app that I have created for the sole purpose of testing forms. Once I setup the $validate
array in the modal, the asterisks showed up on the form, however, I still am not getting the validation messages when I submit the form. Here is my code:
/app/View/Contacts/index.ctp
<h1>Contact Form</h1>
<?php
echo $this->Form->create('Contact');
echo $this->Form->input('name');
echo $this->Form->input('age');
echo $this->Form->end('Submit This Form!!!');
?>
/app/Controller/ContactsController.php
<?php
class ContactsController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
}
}
?>
/app/Model/Contact.php
<?php
class Contact extends AppModel {
var $useTable = false;
public $validate = array(
'name' => array(
'rule' => 'notEmpty',
'message' => 'Cannot leave this field blank.'
),
'age' => array(
'rule' => 'notEmpty',
'message' => 'Cannot leave this field blank.'
)
);
}
?>
Upvotes: 0
Views: 3302
Reputation: 21743
Your mistake is probable in the code you DIDN'T show us: How you process your form data in the controller. You probably forgot to set the data to the model.
Take a look on this tutorial here: http://www.dereuromark.de/2011/12/15/tools-plugin-part-2-contact-form/
My guess looking at your empty (!) index action. You are not doing anything at all. How should that trigger the validation?
I refer to the above link. It shows you how you can validate such a form using set() and validates(). Basically it boils down to:
if ($this->request->is('post') || $this->request->is('put')) {
$this->ContactForm->set($this->request->data);
if ($this->ContactForm->validates()) {
}
}
PS: and since "contacts" is not really a word here and doesnt mean what is supposed to mean I suggest you rename your controller to "ContactController". which also makes the url /contact
Upvotes: 1