[英]Validation in Two Form On Same Page In CakePHP
我是CakePhp的学习者。 我正在单页上开发登录和注册功能。我使用的是名为“用户”的相同模型。但是在验证两个表单时,当我提交“注册表单”时,在注册和登录表单中都显示相同的错误消息。 这是代码
UsersController
class UsersController extends AppController {
public $layout = 'test';
public $components = array('Security');
public function index(){
if($this->request->is('Post')){
if($this->request->data['User']['email']){
$this->User->set('creation_date',date('Y-m-d H:i:s'));
$this->request->data['User']['password'] = Security::hash($this->request->data['User']['password'], 'md5', true);
if($this->User->save($this->request->data)){
//$this->Session->setFlash('Your data is Entered');
$this->Session->write('username', $this->request->data['User']['user_name']);
$this->flash('You are signed Up', 'dashboard');
//$this->redirect(array('action'=>'dashboard'));
}
else{
$this->Session->setFlash('Data is Not Save');
}
}
else {
$username = $this->request->data['User']['user_name'];
$password = Security::hash($this->request->data['User']['password'], 'md5', true);
$query = $this->User->find('all',array('conditions'=>array('User.user_name'=>$username,
'User.password'=>$password)));
if (sizeof($query)!=0){
$this->Session->write('username', $query[0]['User']['user_name']);
$this->redirect('dashboard');
}
else {
$this->Session->setFlash("User is Not Valid");
}
}
}
}
}
索引视图
<div id="loginForm">
<?php
echo $this->form -> create('', array('url'=>array('controller'=>'Users','action'=>'index')));?>
<table cellpadding="10">
<?php
echo '<tr><td>'.$this->form -> input('user_name', array('placeholder'=>'UserName')).'</td>';
echo '<td>'.$this->form -> input('password', array('id'=>'password','placeholder'=>'Password')).'</td>';
echo '<td>'.$this->form -> end('Login').'</td></tr>';
?>
</table>
</div>
<center>
<h1>Create Account</h1>
<div id="signupForm">
<table cellpadding="5">
<?php
echo $this->form -> create('', array('url'=>array('controller'=>'Users','action'=>'index'),'inputDefaults'=>array('label'=>FALSE)));?>
<tr>
<td>User Name:-
<?php echo $this->form -> input('user_name', array('placeholder'=>'UserName'));?></td>
<td> Email:-
<?php echo $this->form -> input('email', array('placeholder'=>'Email'));?></td>
</tr>
<tr>
<td>Mobile No:-
<?php echo $this->form -> input('mobile_no', array('placeholder'=>'Mobile')).'<br>';?></td>
<td>Password:-
<?php echo $this->form -> input('password', array('placeholder'=>'password')).'<br>';?></td>
</tr>
<tr><td colspan="2"><center><?php echo $this->form -> end('Sign Up');?></center></td></tr>
</table>
这是Scren Shot 我进行了很多搜索,但是我找不到任何有用的解决方案。请告诉我该怎么办。
谢谢。
SO中有类似的问题,没有一个起作用吗? 其中尤其我记得是这样的(因为我回答的话)。 你有尝试过吗?
答案总结:
由于表单验证使用模型名称输入错误,并且两个表单的模型名称相同,因此您需要一种变通方法来执行所需的操作。 因此,您需要更改表单名称(如果只是一个,请将其分开),在控制器操作中修复模型处理,并在呈现之前更改名称,以正确验证错误。 一切都在那里解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.