【问题标题】:Validation errormessages not being displayed in the view with CakePHP 2.xCakePHP 2.x 的视图中未显示验证错误消息
【发布时间】:2016-06-19 09:14:49
【问题描述】:

我已经在互联网和 Stack Overflow 上搜索了我的问题,但似乎没有任何效果。这就是我提出这个问题的原因。

我在 CakePHP 中创建了一个表单,但是当我单击提交按钮并且验证中出现错误时,我只看到错误 $this->Flash->error(__('Not all fields are filled in.')); 在视图中创建。 所有输入的数据都已被删除,我没有看到输入字段中的验证错误。

如果我不正确,保存功能应该会在视图中生成错误。

验证失败的原因是什么?

这是我的客户模型:

class Respondent extends AppModel {
    public $validate = array(
    'customer_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'user_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'accept_invitation' => array(
        'notBlank' => array(
            'rule' => array('notBlank'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            'required' => true,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'gender' => array(
        'notBlank' => array(
            'rule' => array('notBlank'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'first_name' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'required' => true,
            'message' => 'Letters and numbers only'
        ),
        'notBlank' => array(
            'rule' => array('notBlank'),
            'message' => ': No first name',
            'allowEmpty' => false,
            'required' => true,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'middle_name' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'required' => false,
            'message' => 'Letters and numbers only'
        ),
        'notBlank' => array(
            'rule' => array('notBlank'),
            //'message' => ': custom message',
            'allowEmpty' => true,
            'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'last_name' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'required' => true,
            'message' => 'Letters and numbers only'
        ),
        'notBlank' => array(
            'rule' => array('notBlank'),
            'message' => ': : No last name',
            'allowEmpty' => false,
            'required' => true,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
);
}

// The Associations below have been created with all possible keys, those that are not needed can be removed


public $hasOne = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);
}

我的客户控制器:

public function register() {

    $userid = $this->Session->read('id');
    $user = $this->findByUserId($userid);
    debug($user);

    if(isset($user['Customer']['is_logged']) && $user['Customer']['is_logged']=== true ) {
        if ($user['Customer']['accept_invitation'] === 0) {
            $this->redirect(array('controller' => 'customers','action' => 'notaccepted'));
        } else {
            $this->redirect(array('controller' => 'customers','action' => 'accepted'));
        }
    }
    //set var
    $this->set(compact('userid'));

}

public function save() {
    //save data from user in db
    if ($this->request->is('post')) {
        $this->Customer->create();

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

            if ($this->data['Customer']['accept_invitation'] == 0 ) {

                $this->redirect(array('action' => 'no'));
            } else {

                $this->redirect(array('action' => 'yes'));
            }
            $this->Flash->success(__('The customer has been saved.'));
        } else {
            $this->Flash->error(__('Not all fields are filled in.'));

            $this->redirect(array('action' => 'register'));

        }
    }

}

我的 register.ctp 视图:

<div class="container">
<?php debug ($_SESSION); ?>

<?php print $this->Form->create('Customer', array('url' => 'save'));?>
<div class="col-md-12 ml15">
    <?php
    $useroptions = [
        '1' => 'Yes',
        '0' => 'No' ];
    $attributes = array(
        'legend' => false,
        'label' => array(
            'class' => 'p10'
        )
    );
    echo $this->Form->radio('accept_invitation', $useroptions, $attributes);
    ?>
</div>
<div  id="registerForm">
    <div class="col-md-6">
        <h2>Persoonlijke gegevens</h2>
        <?php echo $this->Form->hidden('user_id', array('value' =>  $userid )); ?>
        <?php echo $this->Form->hidden('is_logged', array('value' =>  1)); ?>
        <div class="row">
            <div class="col-md-12 ml15">
                <?php
                $options = array(
                    'm' => 'Mister',
                    'f' => 'Miss'
                );
                $attributes = array(
                    'legend' => false,
                    'label' => array(
                        'class' => 'p10'
                    )
                );
                echo $this->Form->radio('gender', $options, $attributes);
                ?>
            </div>
        </div>
        <?php echo $this->Form->input('first_name', array(
                'div' => 'col-md-12 form-group',
                'label' => false,
                'class' => 'form-control',
                'placeholder' => 'First Name *'
            )
        );
        ?>
        <?php echo $this->Form->input('middle_name', array(
                'div' => 'col-md-12 form-group',
                'label' => false,
                'class' => 'form-control',
                'placeholder' => 'Middle Name',
                'length' => 8,
                'required' => false
            )
        );
        ?>
        <?php echo $this->Form->input('last_name', array(
                'div' => 'col-md-12 form-group',
                'label' => false,
                'class' => 'form-control',
                'placeholder' => 'Last Name *'
            )
        );
    </div>
    <div class="col-md-6">
        <?php echo $this->Form->end('Send'); ?>
    </div>
</div>

【问题讨论】:

    标签: php forms validation cakephp


    【解决方案1】:

    如果发生验证错误,请勿重定向。通过写入检查数据是否有效

    if($this->Model->validates()){
         //save
    }else{
         // set error in flash and do not redirect, let the view rendered. It will show error
    }
    

    【讨论】:

    • 感谢您的回复。有效。我认为可以使用自定义公共函数来处理发布请求,但你是对的。我只需要将保存函数中的逻辑放在寄存器函数中。现在它就像一个魅力。该死的,我觉得菜鸟:D Thx 再次为我指出正确的方向!
    猜你喜欢
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多