2013-04-16 2 views
0

cakephp에 user의 first_name과 last_name을 추가하는 양식이 있습니다. 여기 UserController에 대한 (add.ctp)케이크 PHP 폼 유효성 검사가 작동하지 않습니다

<?php 
echo $this->Form->create('User'); 
echo $this->Form->input('first_name',array('label'=>'First Name')); 
echo $this->Form->input('last_name',array('label'=>'Last Name'));   
echo $this->Form->end('Add User'); 
?> 

코드 (UsersController.php) 사용자 모델 (UserModel.php)

<?php 
    class UserModel extends AppModel{ 
    public $validate = array(
    'first_name' => array(
      'rule' => 'notEmpty', 
      'message' => 'first name should not be empty.' 
    ), 
    'last_name' => array(
     'rule' => 'notEmpty', 
     'message' => 'last name should not be empty.' 
    ) 
    ); 
} 
?> 

에 대한

<?php 
    public function add(){ 
      if($this->request->is('post')){ 
       $addData = $this->request->data; 
       $this->User->create();  
       if($this->User->save($addData)){ 
        $this->Session->setFlash('User has been added successfully'); 
        $this->redirect(array('action'=>'index'));    
       }    
      } 
    }  
?> 

보기 코드 코드를보기위한

코드입니다 이것은 내가 사용하고있는 코드이다. , 나는 케이크 책을 보았고 여러 가지 다른 규칙을 사용했지만 어떤 형태의 검증도 유효하지 않습니다. 이유가 될 수있는 일을 좀 도와 주실 수 있습니까? 미리 감사드립니다.

답변

5

모델 파일 이름이 잘못되었습니다. User.php이 아님 UserModel.php

0

모델 이름은 사용자 여야합니다 (테이블 이름과 컨트롤러 이름은 사용자 임). 그래서 모델 파일이 시도 (User.php)

<?php 
App::uses('AppModel', 'Model'); 
class User extends AppModel{ 
public $validate = array(
'first_name' => array(
     'rule' => 'notEmpty', 
     'message' => 'first name should not be empty.' 
), 
'last_name' => array(
    'rule' => 'notEmpty', 
    'message' => 'last name should not be empty.' 
) 

);
}

1

당신이 UserModel.php

대신 사용자로 MySQL에 테이블 이름을 사용하는 경우 user.php에 파일 이름을 변경하고 클래스 이름이어야합니다 바랍니다 같은

<?php 
    class User extends AppModel{ 

    var $name = 'User'; 

    public $validate = array(
    'first_name' => array(
      'rule' => 'notEmpty', 
      'message' => 'first name should not be empty.' 
    ), 
    'last_name' => array(
     'rule' => 'notEmpty', 
     'message' => 'last name should not be empty.' 
    ) 
    ); 
} 
?> 
아래
관련 문제