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 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备