【问题标题】:CakePHP 2.x validating formCakePHP 2.x 验证表单
【发布时间】:2013-12-30 10:31:23
【问题描述】:

我是 CakePHP 的新手,我已经按照他们 www 上的教程进行操作。现在我正在制作简单的应用程序并且我停留在验证表单中,我阅读了有关验证表单here 的信息。

控制器:

class DevicesController extends AppController {
   public $helpers = array('Html', 'Form', 'Session');
   public $components = array('Session');

   public function add()
   {
     if ($this->request->is('post')) {
        $this->Device->create();
        $this->Device->set($this->request->data);

        if ($this->Device->validates()) {
                if ($this->Device->save($this->request->data)) {
                    $this->Session->setFlash(__('feedback'));
                    return $this->redirect(array('action' => 'index'));
                } else {
                // didn't validate logic
                $this->Session->setFlash($this->Device->validationErrors);
                }
        }

    $this->Session->setFlash(__('feedback'));
    }
}

型号:

class Device extends AppModel {
public $validate = array(
    'user' => array(
        'alphaNumeric' => array(
            'rule'     => 'alphaNumeric',
            'required' => true,
            'message'  => 'Alphabets and numbers only'
        ),
        'between' => array(
            'rule'    => array('between', 5, 15),
            'message' => 'Between 5 to 15 characters'
        )
    ),
    'manufacturer' => array(
        'rule'       => 'date',
        'message'    => 'Enter a valid date',
        'allowEmpty' => true
    ),
    'mac' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'Nick name is required.'
        )
    )
);}

查看:

echo $this->Form->create('Device');
echo $this->Form->input('user', array('label' => 'Urzytkownik'));
echo $this->Form->input('mac', array('label' => 'MAC'));
echo $this->Form->input('manufacturer', array('label' => 'Producent'));

即使我不会填写表格,它也成功通过了验证过程。 我在这里做错了什么?

【问题讨论】:

    标签: php validation cakephp cakephp-2.0


    【解决方案1】:

    这里的方法是错误的..

    一旦验证失败,您将在 validates 的 else 块中收到验证错误。但是您在 validates 块本身中访问它是不正确的。

    应该是这样的。

    if ($this->Device->validates()) {
        // successful validation
    } else {
        // you'll get the validation errors here
    }
    

    现在结果代码应该是

    if ($this->Device->validates()) {
        if ($this->Device->save($this->request->data, false)) { // setting 2nd param to false thus no need to validate again.
            $this->Session->setFlash(__('feedback'));
            return $this->redirect(array('action' => 'index'));
        } 
    } else {
        // didn't validate logic
        $this->Session->setFlash($this->Device->validationErrors);
    }
    

    【讨论】:

    • 好的,我找到了答案,我在模型中的文件有“s”:Devices insted of Device,通过删除 s 一切正常
    • 很高兴,它帮助了你:)
    【解决方案2】:

    使用

    if ($this->Device->save($this->request->data)) {
    

    在已经验证之后没有意义。

    在一个简单的调用中一起完成:

    if ($this->request->is('post')) {
        $this->Device->create();
        if ($this->Device->save($this->request->data)) {
            $this->Session->setFlash(__('feedback'));
            return $this->redirect(array('action' => 'index'));
        }
        // didn't validate logic - note that validationErrors is an array!
        $this->Session->setFlash($this->Device->validationErrors);
    }
    

    或在这种情况下正确使用保存:

    $this->Device->save(null, false)
    

    (避免二次验证)。

    注意:第一个是更清洁的选项。

    【讨论】:

      【解决方案3】:

      试试这个代码....

      if ($this->Device->validates()) {
          if ($this->Device->save($this->request->data)) {
              $this->Session->setFlash(__('feedback'));
              return $this->redirect(array('action' => 'index'));
          } else {
              // didn't validate logic
              $this->Session->setFlash($this->Device->validationErrors);
          }
      } else {       
          $errors = $this->Device->validationErrors;
      }
      

      参考:CakePHP validation not working

      【讨论】:

        【解决方案4】:
        **Try the code.....**
        
        <?php
        if ($this->request->is('POST')) {
                        $this->Device->create();
                               if ($this->Device->save($this->data)) {
        $this->Session->setFlash(__('Data has been save.'));
        }
        }
         ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-21
          • 1970-01-01
          • 2012-08-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多