【问题标题】:how to validate form field in cakephp using model and controller如何使用模型和控制器验证 cakephp 中的表单字段
【发布时间】:2014-10-10 08:54:43
【问题描述】:

我创建了一个表单,我需要使用模型和控制器进行验证。这是我的表单

index.ctp

    <?php echo $this->Form->create('Contact',array('url'=>array('controller'=>'contacts','action'=>'add'))); 

 echo $this->Form->text('name');

型号: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'
            )
        )
    );
} 

控制器: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/');

    }

我正在尝试通过谷歌搜索进行验证,但对我来说似乎很难,所以如果有人能描述这个过程,那将是一个很大的帮助。我的 cakephp 版本是 2.3.8。我只需要验证此名称字段,因为当我单击提交时,它会在表单中显示此消息。

【问题讨论】:

  • 你的问题很不清楚。另外,请始终提及您的确切 CakePHP 版本并相应地标记您的问题!话虽这么说:book.cakephp.org/2.0/en/models/data-validation/…
  • $this->Contact->validates();根据有效或有效数据返回布尔值 true 或 false,并且还使用 $this->Contact->set($this->request->data);之前 $this->Contact->validates();
  • 我也编辑了我的问题,当我在表单提交后将 $this->Contact->validates() 显示给我这个错误致命错误:调用非成员函数 validates()第 74 行 /opt/lampp/htdocs/projects/cake/cakephp/app/Controller/ContactsController.php 中的对象 .. 如果您需要我的更多输入,请告诉我
  • 你需要确定你的Contact.php是否加载到controller中,好像Controller忽略了对应的model。

标签: php cakephp cakephp-2.3


【解决方案1】:

你的控制器代码应该是这样的 CakePHP 中的验证过程是这样的

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));

否则你将结束对相同数据的两次验证

您的控制器代码应该有点像这样。

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');
            }

        }

    }

更多信息您可以随时参考http://book.cakephp.org/2.0/en/models/callback-methods.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多