2014-03-06 3 views
0

비슷한 질문이 많지만 그 중 누구도 실제로 나를 도와줍니다. HTML5 양식 유효성 검사 메시지를 트리거 것 같다 나는 데이터베이스에 컴퓨터를 추가하는 형태가CakePHP - 모델 유효성 검사가 작동하지 않습니다.

"모델을 입력하십시오"해야한다 모델 검증 메시지의 대신 "이 필드에 입력하십시오." 여기

내 형태 : 여기
echo $this->Form->create('Computer'); 
echo $this->Form->input('Computer.model', array('label' => 'Model')); 
echo $this->Form->input('Computer.memory', array('label' => 'memory')); 
echo $this->Form->input('Computer.hdd', array('label' => 'hdd')); 
echo $this->Form->input('Computer.price', array('label' => 'price'));  
echo $this->Form->end('Save Computer'); 

인덱스와 전체 컨트롤러 코드이며, 여기에 모델

<?php 
class Computer extends AppModel { 


public $validate = array(

    'model' => array(
     'Please enter model name'=> array(
      'rule'=>'notEmpty', 
      'message'=>'Please enter model' 
     ) 
    ) 

); 
} 

?> 

나는 다른 형태에서 읽기의 행동

<?php 
class ComputersController extends AppController { 

    public $helpers = array('Html', 'Form', 'Session'); 
    public $components = array('Session'); 


    public function beforeFilter() { 
     parent::beforeFilter(); 
     $this->Auth->allow('add'); 
    } 
    public function index() { 


     $this->set('computers', $this->Computer->find('all')); 

    } 

    public function add() { 

     if ($this->request->is('post')) { 
      if (!empty($this->request->data)) { 

       $this->Computer->save($this->request->data); 
       $this->Session->setFlash(__('Your Computer has been saved, or so it seems.....')); 
       return $this->redirect(array('action' => 'index')); 
      } 
      $this->Session->setFlash(__('Not sure why we got here 1.')); 
     } else { 
      $this->Session->setFlash(__('By right, this should be the index page')); 


     } 

    } 

} 
?> 

를 추가 모델 저장 기능을 실행하면 모델 유효성 검사가 자동으로 트리거됩니다. 모델 검증이 작동하도록하려면 어떻게해야합니까?

감사 케빈

거짓
+0

참고로 이동 : <입력 이름 = "데이터 [컴퓨터] [모델]"최대 길이 = "10"유형 = "텍스트"ID = "ComputerModel 같은 모델 필드가 기본값 "required ="required "> 모델 확인 규칙 'notEmpty'가 있기 때문에"필수 "로 표시되는 문제가 발견되었습니다. 이것은 위대합니다. 그러나 여전히 모델 유효성 검사 메시지를 표시하지는 않지만 대신 HTML5 메시지를 표시합니다. – aDvo

+2

Cake는 브라우저에서 처리하는'required = "required"'만 추가합니다. 요소를 검사하고 '필수'속성을 수동으로 제거하면 서버에 도달하게되고 모델에서 메시지를 받게됩니다. Cake는 브라우저 확인 메시지를 변경하지 않습니다. 이것은 수동으로 변경할 수 있습니다 (http://stackoverflow.com/questions/5272433/html5-form-required-attribute-set-custom-validation-message) – cornelb

+0

이와 비슷한 http://stackoverflow.com/a/21094082/ 2776508 – arilia

답변

0

$this->{Model}->save() 반환 유효성 검사가 실패 할 경우,하지만 귀하의 경우에는 후 저장 기능을 플래시 메시지와 함께 리디렉션하고 있습니다. 먼저 양식이 완벽하게 저장되는지 확인하십시오. 완벽하게 저장 한 다음 목록 페이지로 리디렉션하면 view 파일에 유효성 검사 메시지를 볼 수있는 플래시 메시지가 표시됩니다.

if ($this->Computer->save($this->request->data)) { 
    $this->Session->setFlash(__('Your Computer has been saved, or so it seems.....')); 
    return $this->redirect(array('action' => 'index')); 
} else { 
    $this->Session->setFlash(__('Unable to save form')); 
} 

참고 : 그냥보기 파일이 당신을 도와줍니다

희망을

$this->Form->inputDefaults(array(
    'required' => false 
)); 

을 HTML 유효성 검사를하지 않으려면.

+0

고마워,이게 내가 찾던 해답이야. 다른 대답은 여전히 ​​도움이되었습니다. 모델 저장 호출에 실패하여 인덱스 페이지로 리디렉션하면 모델 오류 메시지가 표시됩니다. 그것은 단지 명백하지 않았습니다 ... – aDvo

1

말하자면, 모델에 notEmpty 유효성 검사가있는 경우 CakePHP는 required="required"을 입력 속성에 추가합니다. 이 값은 브라우저에서 처리하므로 빈 값을 제출하려고하면 기본 Please enter this field 메시지가 표시됩니다. 장점은 브라우저를 다른 언어로 사용하는 경우 메시지가 해당 언어로 표시된다는 것입니다.

메시지를 변경하려면 this question과 같은 해결책을 시도해 볼 수 있습니다. 당신이 클라이언트 측 메시지를 제거하려는 경우, 당신은 novalidate

echo $this->Form->create('Computer', array('novalidate' => 'novalidate')); 

이 방법을 사용하여 해제 할 수 있습니다

(이 원하지 무엇 아마도)에서 HTML5는 속성이 무시됩니다 필요하고 모델로부터 메시지를받습니다.

Cake가 클라이언트 측에서 서버 측 값을 사용하도록 지시하는 방법이 있는지 확실하지 않습니다.FormHelper 옵션에

+0

'클라이언트 측에서 서버 측 값을 사용하도록 Cake에게 알리는 방법이 확실하지 않습니다.'는 (ajax에 의한 유효성 검사, 아니오). +1. – AD7six

관련 문제