【问题标题】:Cake PHP, avoid and detect empty fields and duplicate primary keyCake PHP,避免和检测空字段和重复主键
【发布时间】:2015-02-28 04:28:03
【问题描述】:

在 Controller 类中,当字段为空或用户尝试添加具有重复主键的新行时,我无法捕捉到。 这是我的模型验证数组:

public $validate = array(
    'username' => array(
        'notEmpty' => array(
            'rule' => 'notEmpty',
            'message' => 'username empty',
            'required' => true
        ),
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'message' => 'not alphanumeric'
        ),
        'maxLength' => array(
            'rule' => array('maxLength', 50),
            'message' => 'too long'
        ),
        'minLength' => array(
            'rule' =>array('minLength', 1),
            'message' => 'too short'
        )
    ));

这是我在控制器中的添加操作:

if ($this->request->is('post')) {
        $this->Admin->set($this->request->data);
        $this->Admin->create();
            // it validated logic
            if ($this->Admin->save($this->request->data)) {
                $this->Session->setFlash(__('The admin has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $errors = $this->Admin->validationErrors;
                debug($errors);
                $this->Session->setFlash(__('The admin could not be saved. Please, try again.'));
            }
}

但是,

  • 如果我将字段留空,则不会显示我的自定义消息,但会显示空字段的默认 cakephp 消息

  • 如果我尝试将重复元素添加到我的数据库中(因此我违反了主键),也不会显示错误消息,并且我将自动重定向到控制器的 index() 操作(默认索引操作显示数据库元素)

【问题讨论】:

    标签: php mysql cakephp


    【解决方案1】:

    为避免重复主键,请将 id 设置为数据库表中的唯一键。

    如果我将字段留空,则不会显示我的自定义消息,但会显示空字段的默认 cakephp 消息

    这是显示验证消息的正确方式。

    如果我尝试将重复元素添加到我的数据库中(因此我违反了主键),也不会显示错误消息,并且我将自动重定向到控制器的 index() 操作(显示数据库的默认索引操作元素)。

    如果提交的数据包含字段 id 并且该值存在于数据库中,您的帖子将被更新。

    我觉得这段话是多余的:

    $this->Admin->set($this->request->data);
    

    更新:

    如果用户插入重复记录,我会显示一条消息。我不想显示默认的蛋糕 php 消息,但我的自定义消息声明为规则。

    public function add(){
    if ($this->request->is('post')) {
            // 
            if(isset($this->request->data['ModelName']['id'])){
               $id = $this->ModelName->findById($this->request->data['ModelName']['id']);
               if($id){
                  // my custom message here
               }
            }
            //
            $this->Admin->create();
                // it validated logic
                if ($this->Admin->save($this->request->data)) {
                    $this->Session->setFlash(__('The admin has been saved.'));
                    return $this->redirect(array('action' => 'index'));
                } else {
                    $errors = $this->Admin->validationErrors;
                    debug($errors);
                    $this->Session->setFlash(__('The admin could not be saved. Please, try again.'));
                }
    }
    }
    

    【讨论】:

    • Id 在我的数据库中被声明为主键,如果用户插入重复记录,我会显示一条消息。我不想显示默认的蛋糕 php 消息,但我的自定义消息声明为规则。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 2021-04-05
    • 2021-09-13
    • 2012-05-08
    相关资源
    最近更新 更多