dongtan5555 2014-10-10 08:54
浏览 48
已采纳

如何使用模型和控制器验证cakephp中的表单字段

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.

  • 写回答

1条回答 默认 最新

  • dongshipang8094 2014-10-10 10:19
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题