我已经针对我的问题搜索了互联网和Stack Overflow,但似乎没有任何效果。这就是为什么我要创造这个问题。
我在CakePHP中创建了一个表单,但是一旦我单击提交按钮并且验证中有错误,我只会在视图中看到错误$this->Flash->error(__('Not all fields are filled in.'));
。
所有输入的数据都已删除,我没有看到输入字段中的验证错误。
如果我不正确,保存功能应该在视图中生成错误。
验证失败的原因是什么?
这是我的CustomerModel:
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' => ''
)
);
}
我的customerController:
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>
答案 0 :(得分:2)
如果发生验证错误,请勿重定向。通过编写
检查数据是否有效if($this->Model->validates()){
//save
}else{
// set error in flash and do not redirect, let the view rendered. It will show error
}