gpt4 book ai didi

validation - 验证错误未显示CakePHP

转载 作者:行者123 更新时间:2023-12-03 08:12:52 24 4
gpt4 key购买 nike

我正在尝试显示CakePHP(Newbie)的验证错误,但我被卡住了。我收到此错误“分隔符不能为字母数字或反斜杠”。不知道是否遵守逻辑,我从头开始。

什么都没有显示。这是我的代码:

用户模型

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&#039;inscrire</button>
</form>
</div>

最佳答案

//型号

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;
}
}

// Controller
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>

关于validation - 验证错误未显示CakePHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31094730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com