cakephp : apply validation on form using model but doesn’t have actually table exist in database. | Techbirds

Posted on: May 25, 2014 /

Categories: PHP / Author Name: Anand Kumar

Problem : model which doesn’t actually have a table.
for example validating a form on site e.g contact form

Solution :

class ContactForm extends AppModel { var $name = ‘contactForm’; var $useTable = false; var $_schema = array( ‘name’ => array(‘type’=>’string’, ‘length’=>100), ’email’ => array(‘type’=>’string’, ‘length’=>255), ‘details’ => array(‘type’=>’text’) ); var $validate = array( ‘name’ => array( ‘rule’=>array(‘minLength’, 1), ‘message’=>’Name is required’ ), ’email’ => array( ‘rule’=>’email’, ‘message’=>’Must be a valid email address’ ), ‘details’ => array( ‘rule’=>array(‘minLength’, 1), ‘message’=>’Feedback is required’ ) ); }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

class ContactForm extends AppModel {

    var $name = ‘contactForm’;

    var $useTable = false;

    var $_schema = array(

        ‘name’        =>    array(‘type’=>’string’, ‘length’=>100),

        ’email’        =>    array(‘type’=>’string’, ‘length’=>255),

        ‘details’    =>    array(‘type’=>’text’)

    );

    var $validate = array(

        ‘name’ => array(

            ‘rule’=>array(‘minLength’, 1),

            ‘message’=>’Name is required’ ),

        ’email’ => array(

            ‘rule’=>’email’,

            ‘message’=>’Must be a valid email address’ ),

        ‘details’ => array(

            ‘rule’=>array(‘minLength’, 1),

            ‘message’=>’Feedback is required’ )

    );

}

Note: just use var $useTable = false;

415 total views, 1 views today

Share this Onfacebook-1691412twitter-5432123linkedin-2428497google-7951260