0

I have created a form which I need to validate using model and controller .Here is my form

index.ctp

    <?php echo $this->Form->create('Contact',array('url'=>array('controller'=>'contacts','action'=>'add'))); 

 echo $this->Form->text('name');

Model : Contact.php

class Contact extends AppModel
{
        var $name = 'Contact';
        var $useTable = false;

       public $validate = array(
        'name' => array(
            'alphaNumeric' => array(
                'rule'     => 'alphaNumeric',
                'required' => false,
                'message'  => 'Letters and numbers only'
            ),
            'between' => array(
                'rule'    => array('between', 5, 15),
                'message' => 'Between 5 to 15 characters'
            )
        )
    );
} 

Controller : ContactsController.php

public function add()
    {
         $this->Contact->validates();

            $this->request->data['Country']['country_name']=$this->request->data['Contact']['country'];

            $this->Country->saveall($this->request->data);

            $this->redirect('/Contacts/index/');

    }

I am trying to do the validation by googling but it seems difficult to me so if anyone could describe the process it would be a great help .My cakephp version is 2.3.8. I just need to validate this name field , as when I click in submit it will show this message in the form.

4
  • Your question is pretty unclear. Also please always mention your exact CakePHP version and tag your question accordingly! That being said: book.cakephp.org/2.0/en/models/data-validation/… Commented Oct 10, 2014 at 9:04
  • $this->Contact->validates(); returns a boolean true or false depending on valid or onvalid data, And also use $this->Contact->set($this->request->data); before $this->Contact->validates(); Commented Oct 10, 2014 at 9:09
  • I have edited my question also when I put $this->Contact->validates() after form submit its showing me this error Fatal error: Call to a member function validates() on a non-object in /opt/lampp/htdocs/projects/cake/cakephp/app/Controller/ContactsController.php on line 74 .. please let me know if you need any more inputs from me Commented Oct 10, 2014 at 9:14
  • You need to be sure if your Contact.php is loaded in controller , it seems Controller is ignoring the corresponding model. Commented Oct 10, 2014 at 13:00

1 Answer 1

1

Your controller code should be like this The process of validation in CakePHP is like

1) as you have defined validation rules in CakePHP model public `$validates = array();`

2) when ever you do a save on particular model directly or through any association 
a callback method beforeValidate for that model gets called to validate the data which is being saved. 

3) once the data is validated then beforeSave callback is called after this save method is called. 

4) we can also validate the form input fields in controller using $this->Model->validates() but then while saving we have to disable the beforeValidate callback by doing 

$this->Model->save($data,array('validate'=>false));

Otherwise you will end validating the same data twice

your controller code should be somewhat like this.

public function add() {
        // here we are checking that the request is post method
        if ($this->request->is('post')) {
               $this->request->data['Country']['country_name']
                               = $this->request->data['Contact']['country'];
                // here we are saving data
            if ($this->Contact->saveAll($this->request->data)) {

                //here we are setting a flash message for user
                $this->Session->setFlash('your record has been added','success');

                $this->redirect(array('controller'=>'contacts','action' => 'index'));
            } else {
                //here we are setting a flash message for user for error if input are not          
                //validated as expected
                $this->Session->setFlash('sorry we could add your record','error');
            }

        }

    }

For more information you can always refer to http://book.cakephp.org/2.0/en/models/callback-methods.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.