2013-02-01 2 views
4

CodeIgniters 폼 유효성 검사 라이브러리를 사용하려고하는데 유효성 검사 규칙이 작동하는 것처럼 보이지만 validation_errors()를 호출 할 때마다 빈 문자열이 나타납니다.CodeIgniter 폼 유효성 검사에서 유효성 검사 오류가 발생하지 않습니다.

다음은 코드 단편입니다.

$base_rules = 'required|trim'; 

$this->_validation_rules = array(
     array(
      'field' => 'name', 
      'label' => 'name', 
      'rules' => $base_rules . '|alpha_numeric|min_length[5]|max_length[30]' 
     ), 
     array(
      'field' => 'price', 
      'label' => 'Price', 
      'rules' => $base_rules . '|decimal' 
     ), 
     array(
      'field' => 'duration', 
      'label' => 'Duration', 
      'rules' => $base_rules . '|integer' 
     ), 
     array(
      'field' => 'booking', 
      'label' => 'Booking', 
      'rules' => $base_rules . '|integer' 
     ) 
    ); 
    $this->form_validation->set_rules($this->_validation_rules); 

    if ($this->form_validation->run()) { 
     // do stuff 
    }else{ 
     // prints an empty string 
     var_dump(validation_errors()); 
     exit; 
    } 

왜 이런 경우인지, 그리고 오류를 어떻게받을 수 있습니까? 양식의 상단에

답변

5

넣어 VALIDATION_ERRORS(), 그리고)

if ($this->form_validation->run()) { 
     // do stuff 
    }else{ 
     $this->load->view('myform'); //display your form again 
    } 

VALIDATION_ERRORS (에 코드를 변경 만하여보기에 poplated됩니다.

+5

그래도 고마워, 한 가지 질문 - 플래시 Data 또는 다른 저장소에 유효성 검사 오류를 저장할 수 없기 때문에 다른 곳으로 리디렉션 할 수 있습니까? 건배 – AaronDS

+0

@ 존 AJAX 요청을 보내면 어떻게 될까요? 물론, View보다는 Controller에서 오류를 가져와야합니다. 그래서 컨트롤러에 오류가 발생하는 해결책이 있습니까? controller에서'validation_errors()'가 작동하지 않습니다. – muaaz

1

넣어 당신이 당신의 입력에 따라 넣을 수있는 형태의 게시물을 한 경우에

echo validation_errors('<div>', '</div>'); 

다음,이

else{ 
    $this->load->view('myform'); //display your form again 
} 
+0

기여에 감사드립니다. – AaronDS

1

완료하려면 존의 대답에 다른 변경보기 파일 형태의이 코드 상단 :

<?php echo form_error('input-name'); ?> 

하지만 아약스 요청을하고 있다면validation_errors()를 사용할 수 있습니다. 여기

exemple :

//Important to turn that off if it's on 
    $this->output->enable_profiler(false); 

    $this->output->set_status_header('500'); 
    $this->output->set_content_type('application/json'); 

    echo json_encode(array(
     'error_msg' => validation_errors(), 
    )); 

그리고 클라이언트 측에이 같은 응답 사용할 수 있습니다

error:function(data) { 
    $("your-error-input-selector").html('').append(data.responseJSON.msg); 
} 

희망 나는 1 년 늦었 경우에도 도움을.

피씨. 영어로 부러진 죄송합니다.

+0

Not working ...... validation_errors()는 항상 빈 문자열을 반환합니다. – muaaz

+0

양식 검증을 추가 했습니까? 검증을 실행 했습니까? 당신의 코드를 살펴볼 수 있습니까? 나는 너를 돕기 위해 기꺼이보다 더 행복 할 것이다! – Gabtheviking

관련 문제