0

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.'
        )
    );
}
?>
tereško
  • 58,060
  • 25
  • 98
  • 150
drewwyatt
  • 5,989
  • 15
  • 60
  • 106
  • From within the controller, I'd take a look at the $this->Contact->validationErrors property. Be sure to call $this->Contact->set($this->data); (or ->save(...)) prior to inspecting the validation errors or else you'll just get an empty array each time. – Duncan Apr 07 '12 at 17:37

1 Answers1

1

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

mark
  • 21,691
  • 3
  • 49
  • 71
  • No, `ContactsController` is correct. Controller names should be plural. – JJJ Apr 07 '12 at 17:24
  • I am not talking about what the conventions are (I am pretty aware of them). I also didnt tell you that you MUST do that. I merely suggested to you that it would be wise to do so (as outlined in the blog) but either way you still didnt show us the relevant information. – mark Apr 07 '12 at 17:38
  • I believe that you thought Juhana was me - since you said " either way you still didnt show us the relevant information." Juhana is not the OP. I actually showed you everything I had, I am about to read over the link you posted, but I only started using Cake yesterday, and was under the impression that I could validate before moving forward with the controller. I'll take a look and let you know if it works. Thanks for all of your help! – drewwyatt Apr 07 '12 at 23:24
  • I realized the mistake too late (coudnt edit the comment). I did edit my answer though (the part about the empty index action). You need to manually trigger validation here. And please provide the exact cakephp version on your questions (in this case probably 2.1). this will help to give the appropriate answer. – mark Apr 07 '12 at 23:29
  • Totally worked. Thanks for being patient and holding my hand. – drewwyatt Apr 07 '12 at 23:46
  • Awww guys that was beautiful. – Sandy Jul 17 '13 at 20:25