free hit counter

Validate Form Using Model Without Table In Cakephp 2

Validate Form Using Model Without Table In Cakephp (eg- Contact Us form )

 

Form Validation is an essential task in any online application, its also an incredibly dull, tedious and repetitive task but with CakePHP you can cut down the time and effort you spend validating your form data. In a standard PHP application you will normally display the form for user input, try to validate the data, if errors exist redisplay the form showing the errors whilst keeping the data that was already submitted and go through the process again until the data successfully validates.

Create a Model “User” and copy paste this code

  1. <?php  
  2. App::uses(‘Model’‘Model’);  
  3. class User extends AppModel {  
  4.     var $useTable = false;  
  5.   
  6. var $_schema = array(  
  7.     ‘name’   =>array(‘type’=>‘string’‘length’=>100),   
  8.     ‘address’ =>array(‘type’=>‘string’‘length’=>255),   
  9.     ‘phone’ =>array(‘type’=>‘string’‘length’=>20),  
  10.     ‘subject’  =>array(‘type’=>‘string’‘length’=>255),  
  11.     ‘message’  =>array(‘type’=>‘text’)  
  12. );  
  13.     var $validate = array(  
  14.         ‘name’ => array(  
  15.             ‘rule’ => ‘notEmpty’,  
  16.             ‘message’ => ‘required field’  
  17.         ),  
  18.         ‘address’ => array(  
  19.             ‘rule’ => ‘notEmpty’,  
  20.             ‘message’ => ‘required field’  
  21.         ),  
  22. ‘phone’ => array(  
  23.         ‘rule’ => array(‘phone’, null, ‘us’),  
  24.         ‘message’ => ‘Please enter a valid phone eg. 5556668888’,  
  25.     ),  
  26.         ‘subject’ => array(  
  27.             ‘rule’ => ‘notEmpty’,  
  28.             ‘message’ => ‘required field’  
  29.         ),  
  30.         ‘message’ => array(  
  31.             ‘rule’ => ‘notEmpty’,  
  32.             ‘message’ => ‘required field’  
  33.         ),  
  34.     );  
  35. }  

 

For View

  1. <?php  
  2.   
  3. echo $this->Form->create(‘User’,array(‘novalidate’ => true));  
  4.   
  5. echo $this->Form->input(‘name’);  
  6. echo $this->Form->input(‘address’);  
  7. echo $this->Form->input(‘phone’);   
  8. echo $this->Form->input(‘subject’);   
  9. echo $this->Form->input(‘message’);        
  10.   
  11. echo $this->Form->end(‘Add’);?>  

Action for Controller

  1. public function index()  
  2. {  
  3.     if(!emptyempty($this->data))  
  4.     {  
  5.         //pr($this->data);die;   
  6.         if(isset($this->request->data[‘User’]))  
  7.         {  
  8.         $this->User->set($this->request->data[‘User’]);  
  9.         }  
  10.         if($this->User->validates())  
  11.         {}  
  12.         else {}  
  13.     }  
  14. }  

Now Run your code  🙂

Or Download View, model and action here 

2 thoughts on “Validate Form Using Model Without Table In Cakephp

  1. Reply Oscar Jul 26,2014 7:02 am

    I see a lot of interesting articles on your page. You have to spend a lot of time writing, i know
    how to save you a lot of time, there is a tool that creates readable, google friendly posts in couple
    of seconds, just type in google – k2 unlimited content

  2. Reply Dexter Aug 27,2014 3:23 pm

    I read a lot of interesting posts here. Probably you spend a lot
    of time writing, i know how to save you a lot
    of work, there is an online tool that creates high quality, google
    friendly posts in minutes, just search in google – laranitas free content source

Leave a Reply to Dexter Cancel Reply