모델에서 $ validate 배열을 사용하여 '이 필드는 비워 둘 수 없습니다'또는 '이름이 null이 아닙니다'와 같은 사용자 지정 메시지를 표시하려고합니다. 두 개의 컨트롤러, 주 및 사용자가 있습니다.
주 컨트롤러의 색인 파일에는 로그인 및 등록보기가 있습니다. login 및 register 함수의 action 부분은 user_controller에 있습니다. 로그인 및 등록 기능이 유효성을 확인하면 주 컨트롤러의 홈 페이지로 리디렉션됩니다. 그렇지 않으면 인덱스 페이지 자체에 남아 있습니다.
유효성 검사 메시지가 색인 페이지 자체에 표시되기를 원합니다. 그러나 이러한 메시지는 로그인 및 등록을위한 별도의보기 파일이있는 경우에만 표시됩니다. 즉, /views/forms/register.ctp 및 /views/forms/login.ctp가 있습니다.
해당 기능에 대한 별도의보기 파일없이 이러한 유효성 검사 메시지를 표시 할 수있는 방법이 있습니까? 나는 아래에 내 코드를 주었다. 누군가 나를 안내 해줘.
모델 클래스 :
<?php
class User extends AppModel {
var $name = 'User';
var $components=array('Auth');
var $validate = array(
'name' => array(
'rule' => 'notEmpty',
'message' =>'Name cannot be null.'
),
'password' => array(
'rule' => 'notEmpty'
),
'email_id' => array(
'rule' => 'notEmpty'
)
);
function registerUser($data)
{
if (!empty($data))
{
$this->data['User']['name']=$data['User']['name'];
$this->data['User']['email_id']=$data['User']['email_id'];
$this->data['User']['password']=$data['User']['password'];
$existingUsers= $this->find('all');
foreach($existingUsers as $existingUser):
if($this->data['User']['email_id']==$existingUser['User']['email_id']){
return 0;
}
else{
$this->save($this->data);
$this->data['User']['id']= $this->find('all',array('fields' => array('User.id'),
'order' => 'User.id DESC'
));
$userId=$this->data['User']['id'][0]['User']['id'];
return $userId;
}
endforeach;
}
}
function loginUser($data)
{
$this->data['User']['email_id']=$data['User']['email_id'];
$this->data['User']['password']=$data['User']['password'];
$login=$this->find('all');
foreach($login as $form):
if($this->data['User']['email_id']==$form['User']['email_id'] && $this->data['User']['password']==$form['User']['password'])
{
$this->data['User']['id']= $this->find('all',array('fields' => array('User.id'),
'conditions'=>array('User.email_id'=> $this->data['User']['email_id'],'User.password'=>$this->data['User']['password'])
));
$userId=$this->data['User']['id'][0]['User']['id'];
return $userId;
}
endforeach;
}
}
?>
컨트롤러 클래스 :
<?php
class UsersController extends AppController
{
var $name = 'Users';
var $uses=array('Form','User','Attribute','Result');
var $helpers=array('Html','Ajax','Javascript','Form');
function register()
{
$this->Session->write('userId',$this->User->registerUser($this->data));
$this->User->data=$this->data;
if (!$this->User->validates())
{
$this->Session->setFlash('Please enter valid inputs');
$this->redirect('/main');
return;
}
if($this->Session->read('userId')==0){
$this->Session->setFlash('You are already a registerd member.Log in your account');
$this->redirect('/main');
}
else{
$this->Session->setFlash('User account created');
$this->redirect('/main/home');
}
}
function login()
{
//$userId=$this->User->loginUser($this->data);
$this->Session->write('userId',$this->User->loginUser($this->data));
$this->User->data=$this->data;
if (!$this->User->validates())
{
$this->Session->setFlash('Please enter valid inputs');
$this->redirect('/main');
return;
}
if($this->Session->read('userId')>0){
$this->Session->setFlash('Login Successful');
$this->redirect('/main/home');
break;
}
else{
$this->Session->setFlash('Username and password do not match.');
$this->redirect('/main');
}
}
}
?>
보기 틀 :
<!-- File: /views/main/index.ctp-->
<div id="register">
<h3>Register</h3>
<?php
echo $form->create('User',array('action'=>'register'));
echo $form->input('name');
echo $form->input('email_id');
echo $form->input('password');
echo $form->end('Register');
?>
</div>
<div id="login">
<h3>Login</h3>
<?php
echo $form->create('User',array('action'=>'login'));
echo $form->input('email_id');
echo $form->input('password');
echo $form->end('Login');
?>
</div>
그래, 내가 말한대로 변경을하겠다.하지만 특정 유효성 검사 오류를 원한다. 로그인 페이지에 표시됩니다. – Angeline
내가 말했듯이, 같은 페이지에 두 번 기본적으로 동일한 양식이있는 경우 자동 오류 메시지 생성과 관련된 문제가 발생합니다. 또한 로그인 양식에는 불필요하다고 반복해서 말씀 드리겠습니다. 그런데, 양식을 분리한다면 여기에 설명 된 방법을 사용할 수 있습니다 : http://book.cakephp.org/view/410/Validating-Data-from-the-Controller – deceze