我正在尝试用CakePHP(新手)显示验证错误,但我被卡住了。我收到此错误“分隔符不能是字母数字或反斜杠”。不知道逻辑是否得到尊重,我是从头开始的。
没有显示任何内容。这是我的代码:
用户模型
class User extends AppModel {
public $validate = array(
'nom' => array(
'message' => 'Saisie obligatoire',
'required' => true
),
'prenom' => array(
'message' => 'Saisie obligatoire',
'required' => true
),
'date_naissance' => array(
'rule' => array('date','dmy'),
'message' => 'Veuillez respecter le format de la date (jour/mois/année)',
'allowEmpty' => true
),
'email' => array(
'rule' => 'email',
'message' => 'Veuillez introduire une adresse mail valide',
'required' => true
),
'password' => array(
'rule' => 'password',
'message' => 'Un mot de passe est requis'
)
);
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
将功能添加到UsersController
public function add() {
if ($this->request->is('post')) {
$this->User->set($this->request->data);
if ($this->User->validates()) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Auth->login($this->User);
return $this->redirect('/index');
}
}
} else {
return $this->User->validationErrors;
}
}
add.ctp
<?= $this->element('navbar');?>
<div class="formcontainer">
<div class="page-header">
<h1>Rejoignez-nous</h1>
</div>
<form action="/users/add" id="UserAddForm" method="post" accept-charset="utf-8">
<div style="display:none;">
<input type="hidden" name="_method" value="POST"/>
</div>
<div class="form-group input text">
<label for="UserNom">Nom:</label>
<input name="data[User][nom]" maxlength="20" type="text" id="UserNom" class="form-control" placeholder="requis">
</div>
<div class="form-group input text">
<label for="UserPrenom">Prénom:</label>
<input name="data[User][prenom]" maxlength="20" type="text" id="UserPrenom" class="form-control" placeholder="requis">
</div>
<div class="form-group input text">
<label for="UserDateNaissance">Date de naissance:</label>
<input name="data[User][date_naissance]" maxlength="20" type="text" id="UserDateNaissance" class="form-control">
</div>
<div class="form-group input email">
<label for="UserEmail">Email:</label>
<input name="data[User][email]" maxlength="100" type="email" id="UserEmail" class="form-control" placeholder="requis"/>
</div>
<div class="form-group input password">
<label for="UserPassword">Mot de passe:</label>
<input type="password" name="data[User][password]" class="form-control" id="UserPassword" placeholder="requis">
</div>
<button type="submit" class="btn btn-default bSub">M'inscrire</button>
</form>
</div>
答案 0 :(得分:1)
//型号
class User extends AppModel {
public $validate = array(
'password' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Un mot de passe est requis'
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'email' => array(
'email' => array(
'rule' => array('email'),
'message' => 'Veuillez introduire une adresse mail valide',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'date_naissance' => array(
'date' => array(
'rule' => array('date'),
'message' => 'Veuillez respecter le format de la date (jour/mois/année)',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'prenom' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Saisie obligatoire',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'nom' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Saisie obligatoire',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
//控制器
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
// add.ctp
<div class="users form">
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php
echo $this->Form->input('password');
echo $this->Form->input('email');
echo $this->Form->input('date_naissance');
echo $this->Form->input('prenom');
echo $this->Form->input('nom');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?></li>
</ul>
</div>
答案 1 :(得分:0)
您的验证应该是这样的(您必须为字段添加规则,并且cakephp中没有内置的 passowrd 规则。)
public $validate = array(
'nom' => array(
'rule' => 'notEmpty', //add rule here
'message' => 'Saisie obligatoire',
'required' => true
),
'prenom' => array(
'rule' => 'notEmpty', //add rule here
'message' => 'Saisie obligatoire',
'required' => true
),
'date_naissance' => array(
'rule' => array('date', 'dmy'),
'message' => 'Veuillez respecter le format de la date (jour/mois/année)',
'allowEmpty' => true
),
'email' => array(
'rule' => 'email',
'message' => 'Veuillez introduire une adresse mail valide',
'required' => true
),
'password' => array(
'rule' => 'notEmpty', //there is no inbuilt validation rule with name *password*
'message' => 'Un mot de passe est requis'
)
);
如果您想显示错误消息,那么您应该使用Form Helper来创建表单输入,如
echo $this->Form->input("User.nom", array("class"=>"form-control", "placeholder"=>"requis", 'label'=>false));
或者您可以使用Form helper
的isFieldError方法显示消息if ($this->Form->isFieldError('nom')) {
echo $this->Form->error('nom');
}