PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 22, 2022

[FIXED] how to validate form field in cakephp using model and controller

 April 22, 2022     cakephp, cakephp-2.3, php     No comments   

Issue

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.


Solution

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



Answered By - justrohu
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing