How do you perform Form validation without a model in Cakephp?

后端 未结 6 961
Happy的楠姐
Happy的楠姐 2021-02-13 00:06

I need to perform some validation. I don\'t have the model in the application.

Does anyone know how to do the validation without a model? Can you show me using a small s

6条回答
  •  北荒
    北荒 (楼主)
    2021-02-13 00:30

    You can perform validation of form data in CakePHP without having to create a model.php file. There are many times when I need to do this, and storing model.php files that do nothing more then validation is a poor usage of the model design pattern.

    Another problem with CakePHP is that sometimes validation rules are common across multiple models. It would be nice to move validation out of the model, much in the way behaviors are to their own subfolder. That way we can re-use them or use them without a model.

    Another problem with validation is that it's dependent upon the model alias. If you have a model called "Email" without a table to perform validation, then the posted form must also use "Email". If the form uses a alias different from the controller, then you have to set the action. A lot of extra steps just to do validation. You can't re-use that model again if your form uses a different model.

    So here is my alternative approach.

    In your controller's action that receives the posted form data. You can create a default CakePHP model, add some validation rules and then use that model for validation.

    An example action might look like this;

    function edit()
    {
       $model = ClassRegistry::init(array('class'=>'Email','table'=>false,'type'=>'Model'));
       if(!empty($this->data))
       {
           $model->validate = array(
               'subject'=>array(
                   'rule'=>'notEmpty',
                   'required'=>true
               ),
               'message'=>array(
                   'rule'=>'notEmpty',
                   'required'=>true
               )
           );
           if($model->save($this->data))
           {
               // validation was successful, but no data was actually saved
           }
       }
    }
    

    The key here is the creation of an automatic model by CakePHP.

           $model = ClassRegistry::init(array('class'=>'Email','table'=>false,'type'=>'Model'));
    

    The above attempts to find a model by Email in the applications model folder. When it is not found CakePHP will auto-create an in memory model for that class. Since we set the value of 'table' to false, then this should tell CakePHP that this model doesn't use a table.

    This works as long as there really isn't a email.php file in the applications model folder. Once this model is created in memory. It's accessible from the built in Form help. That means validation errors will be passed correctly to the view.

    Here is an example view file.

    Form->create('Email',array('action'=>array('controller'=>'mycontroller','action'=>'edit'))); ?>
    Form->input('subject'); ?>
    Form->input('message',array('type'=>'textarea')); ?>
    Form->submit(); ?>
    

    The view will now render the validation errors from the Email model using the Form helper. This is because CakePHP class registry has saved a copy of the EMail auto model in memory that the Form helper will access.

    If you want to use custom validation rules, then you will have to put the callback methods in the app_model.php file.

    These methods tested in CakePHP 1.3

提交回复
热议问题