0
我正在學習CakePHP3.3,並且在保存之前遇到了嘗試驗證表單的問題。驗證CakePHP3.3中不是「添加」或「編輯」的表單
我在'src/Template/Users'中創建了一個名爲'register.ctp'的新表單,並在'src/Controller/UsersController'中添加了一個名爲'register'的動作。
我想在保存之前驗證表單提交,但無法弄清楚如何使這項工作。
FWIW,預保存驗證對'添加'和'編輯'表單完美適用,但我認爲這在CakePHP3中默認發生。
有沒有辦法讓這些相同的驗證規則適用於「註冊」表單?僅供參考,「註冊」行爲實際上是更新先前爲匿名用戶創建的現有用戶記錄。
這裏的控制器動作:
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
class UsersController extends AppController
{
//<snipped methods>
public function register()
{
if($this->request->session()->read('Auth.User'))
{
$id = $this->request->session()->read('Auth.User.id');
if ($this->request->is(['patch', 'post', 'put']))
{
$user = $this->Users->get($id, [
'contain' => []
]);
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user))
{
$this->Flash->success(__('Your free trial period has started.'));
return $this->redirect("/Home/index");
}
else
{
$this->Flash->error(__('We were unable to start your trial. Please, try again.'));
}
}
}
else
{
$this->Flash->error(__('Your demo session has expired. Please start again.'));
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
}
這裏的UsersTable模型的驗證規則:
<?php
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class UsersTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('users');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
}
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->email('email')
->requirePresence('email')
->notEmpty('An email address is required.');
$validator
->requirePresence('password')
->notEmpty('A password is required.');
$validator
->requirePresence('firstname')
->notEmpty('firstname')
->add('firstname', 'minlength',['rule' => ['minlength', 1]]);
$validator
->requirePresence('lastname')
->notEmpty('lastname')
->add('lastname', 'minlength',['rule' => ['minlength', 1]]);
$validator
->integer('status')
->requirePresence('status', 'create')
->notEmpty('status');
$validator
->add('role', 'inList', [
'rule' => ['inlist', ['admin','author','subscriber']],
'message' => 'Please enter a valid role.'
]);
$validator
->requirePresence('referer', 'create')
->allowEmpty('referer');
$validator
->integer('plan_id')
->requirePresence('plan_id', 'create')
->notEmpty('plan_id');
return $validator;
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['email']));
return $rules;
}
}
而這裏的register.ctp形式:
<div class="users form large-12 columns ">
<?= $this->Form->create() ?>
<fieldset>
<legend><?= __('Sign Up For Your No-Risk Free Trial!') ?></legend>
<?= $this->Form->input('firstname'); ?>
<?= $this->Form->input('lastname'); ?>
<?= $this->Form->input('email'); ?>
<?= $this->Form->input('password'); ?>
</fieldset>
<?= $this->Form->button(__('Start My Free Trial Now')) ?>
<?= $this->Form->end() ?>
</div>
任何幫助非常感謝!
我試過這個,但仍然有同樣的問題......當我提交沒有值的表單時,錯誤只會被保存而不是保存之前。此外,所有必填字段在'添加'和'編輯'表單中都以'*'表示 - 即使所有人都共享相同的UsersController – Arnold
,您也可能忘記添加$用戶在表單中創建** = $this-> Form-> create($ user)?> **在您的ctp文件中 – tarikul05
嘗試了這一點太多了,它會引發錯誤...「注意(8):未定義變量:user [APP/Template/Users/register.ctp,第3行]「......顯然我錯過了一些基本的東西,我似乎無法看到它。 – Arnold