我是Cake PHP的新手,我需要验证表单。我运行应用程序并正常提交,即使字段为空。
我想使用 UsersTables.php
中使用的默认规则验证它们如果有人帮助我,我将不胜感激。
register.ctp :
<br>
<div class="index large-4 medium-4 large-offset-4 medium-offset-4 columns">
<div class="panel">
<h2 class="text-center">Sign Up</h2>
<?= $this->Form->create(); ?>
<?php
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('password');
?>
<?= $this->Form->submit('Sign Up', array('class' => 'button')); ?>
<?= $this->Form->end(); ?>
</div>
</div>
我想在 UsersTable.php 中使用以下验证码:
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->requirePresence('nome', 'create')
->notEmpty('nome');
$validator
->email('email')
->requirePresence('email', 'create')
->notEmpty('email');
$validator
->requirePresence('password', 'create')
->notEmpty('password');
return $validator;
}
我已经导入了Cake \ Validation \ Validator;我想理解为什么上面的验证代码与'add'形式和其他形式一起工作。
UsersController.php :
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Validation\Validator;
/**
* Users Controller
*
* @property \App\Model\Table\UsersTable $Users
*/
class UsersController extends AppController
{
/**
* Index method
*
* @return \Cake\Network\Response|null
*/
public function index()
{
$users = $this->paginate($this->Users);
$this->set(compact('users'));
$this->set('_serialize', ['users']);
}
/**
* View method
*
* @param string|null $id User id.
* @return \Cake\Network\Response|null
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$user = $this->Users->get($id, [
'contain' => []
]);
$this->set('user', $user);
$this->set('_serialize', ['user']);
}
/**
* Add method
*
* @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
/**
* Edit method
*
* @param string|null $id User id.
* @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$user = $this->Users->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
/**
* Delete method
*
* @param string|null $id User id.
* @return \Cake\Network\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$user = $this->Users->get($id);
if ($this->Users->delete($user)) {
$this->Flash->success(__('The user has been deleted.'));
} else {
$this->Flash->error(__('The user could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
public function register()
{
$user = $this->Users->newEntity();
if($this->request->is('post'))
{
$user = $this->Users->patchEntity($user, $this->request->data);
if($this->Users->save($user))
{
$this->Flash->success('Cadastro efetuado com sucesso');
return $this->redirect(['action' => 'login']);
}
else
{
$this->Flash->error('Erro no cadastro');
}
$this->set('user',$user);
$this->set('_serialize', ['user']);
}
}
public function login()
{
if($this->request->is('post'))
{
$user = $this->Auth->identify();
if($user)
{
$this->Auth->setUser($user);
return $this->redirect(['controller' => 'comentario', 'action' => 'add']);
}
// Erro no Login
$this->Flash->error('Erro de autenticação');
}
}
public function alterar()
{
$id = $this->Auth->user('id');
$user = $this->Users->get($id);
if($this->request->is(['patch', 'post', 'put']))
{
$user = $this->Users->patchEntity($user, $this->request->data);
if($this->Users->save($user))
{
$this->Flash->success('Seus dados foram alterados');
}
else
{
$this->Flash->error('Erro na alteração dos dados');
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
public function logout()
{
$this->Flash->success('Sessão encerrada');
return $this->redirect($this->Auth->logout());
}
public function beforeFilter(Event $event)
{
$this->Auth->allow(['register']);
}
}
答案 0 :(得分:0)
<br>
<div class="index large-4 medium-4 large-offset-4 medium-offset-4 columns">
<div class="panel">
<h2 class="text-center">Sign Up</h2>
<?= $this->Form->create(); ?>
<?php
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('password');
?>
<?= $this->Form->submit('Sign Up', array('class' => 'button')); ?>
<?= $this->Form->end(); ?>
</div>
</div>
用
替换此代码<br>
<div class="index large-4 medium-4 large-offset-4 medium-offset-4 columns">
<div class="panel">
<h2 class="text-center">Sign Up</h2>
<?= $this->Form->create($user); ?>
<?php
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('password');
?>
<?= $this->Form->submit('Sign Up', array('class' => 'button')); ?>
<?= $this->Form->end(); ?>
</div>
</div>
在控制器的注册方法中添加它
public function register()
{
$user = $this->Users->newEntity();
if($this->request->is('post'))
{
$user = $this->Users->patchEntity($user, $this->request->data);
if($this->Users->save($user))
{
$this->Flash->success('Cadastro efetuado com sucesso');
return $this->redirect(['action' => 'login']);
}
else
{
$this->Flash->error('Erro no cadastro');
}
}
$this->set('user',$user);
}