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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失