2012-03-26 4 views
0

이이 질문에 함수 add_newStaff에서CodeIgniter 폼 유효성 검사에서 코드 중복을 방지하는 방법은 무엇입니까?

function newStaff() 
{ 
    $data = array(); 

    $data['departmentList'] = $this->department_model->list_department(); 
    $data['branchList'] = $this->branch_model->list_branch(); 
    $data['companyList'] = $this->company_model->list_company(); 

    $this->load->view('staff/newstaff', $data); 

} 

function add_newStaff() 
{ 
    //when user submit the form, it will call this function 
    //if form validation false 

    if ($this->validation->run() == FALSE) 
    { 
     $data = array(); 

     $data['departmentList'] = $this->department_model->list_department(); 
     $data['branchList'] = $this->branch_model->list_branch(); 
     $data['companyList'] = $this->company_model->list_company(); 

     $this->load->view('staff/newstaff', $data); 
    } 
    else 
    { 
     //submit data into DB 
    } 

} 

(), 내가 양식 유효성 검사가 false를 반환하면 데이터베이스에서 모든 데이터를 다시로드 할 필요에 대한 직원 컨트롤러 기능의 샘플입니다. 두 개의 코드 사본을 유지해야하기 때문에 문제가 될 수 있습니다. 이 문제를 방지하기 위해 사용할 수있는 팁이 있습니까?

감사합니다.

답변

1

하면 다음과

function newStaff() 
    { 
     $data = $this->_getData(); 

     $this->load->view('staff/newstaff', $data); 

    } 

    function add_newStaff() 
    { 
     //when user submit the form, it will call this function 
     //if form validation false 

     if ($this->validation->run() == FALSE) 
     { 
      $data = $this->_getData(); 

      $this->load->view('staff/newstaff', $data); 
     } 
     else 
     { 
      //submit data into DB 
     } 

    } 

    private function _getData() 
    { 
     $data = array(); 

     $data['departmentList'] = $this->department_model->list_department(); 
     $data['branchList'] = $this->branch_model->list_branch(); 
     $data['companyList'] = $this->company_model->list_company(); 

     return $data; 
    } 
+0

와우 감사합니다. 나는이 방법을 전에는 생각할 수 없다. – cyberfly

1

일에서 당신을 방지 뭐죠은 다른 방법으로 당신은 당신의 형태는 당신이 다음과 같은과 초기 형태의 요청에 대해 사용하는 것과 동일한 서비스를 가리 키도록에 제출 동작을 변경합니다. 또한 폼에 제출 된 값을 유지하려는 경우 페이지로드간에 POST 값을 유지해야합니다.

function newStaff() 
{ 
    // validation rules 

    if ($this->validation->run() == TRUE) 
    { 
     //submit data into DB 
    } 
    else 
    { 
     $data = array(); 
     $data['departmentList'] = $this->department_model->list_department(); 
     $data['branchList'] = $this->branch_model->list_branch(); 
     $data['companyList'] = $this->company_model->list_company(); 

     $this->load->view('staff/newstaff', $data); 
    } 
} 
+0

감사합니다, 당신의 대답은 나를 위해 일할 수있는 아이디어를 제공합니다. – cyberfly

관련 문제