2014-12-30 3 views
0

내 응용 프로그램에서 사용자는 products.here를 추가하기 위해 자신의 필드 목록을 만들 수 있습니다 내 양식입니다.codigniter 유효성 검사에서 복제 된 필드의 유효성을 검사하는 방법은 무엇입니까?

<div class="row productsdiv"> 
    <input type="text" name="product[]" class="product form-control" <?php echo set_value('product[]'); ?>'> 
    <?php echo form_error('product[]'); ?> 
    <input type="hidden" name="product_id[]" <?php set_value("");?>> 
    <input type="text" name="price[]" class="price form-control"<?php echo set_value("price[]");?> placeholder='<?php echo lang('placeholder_price'); ?>'> 
    <?php echo form_error('price[]'); ?> 
    <input type="text" name="qty[]" class="quantity form-control"<?php echo set_value("qty[]");?>'> 
    <?php echo form_error('qty[]'); ?> 
    </span><span class="total">0</span> 
    <textarea name="description[]" cols="40" rows="3" class="footerinput form-control"></textarea> 
    <?php echo form_error('description[]'); ?> 
</div><!--Product row end --> 

위 div 제품은 버튼을 더 사용하여 복제됩니다. 내 JQuery와

$('#addmore').on('click', function() {  
    var cloneRow = $('.productsdiv:first').clone(); 
    cloneRow.find("input").val("").end(); 
    cloneRow.find(".total").text("").end(); 
    cloneRow.find(".total").after('<button type="button" class="close lebel-danger" data-dismiss="alert">&times;</button>'); 
    cloneRow.find(".footerinput").css({"display":"none"}); 
    cloneRow.appendTo('#products'); 

내 컨트롤러 :

if($_POST){ 
$this->form_validation->set_rules('customer_id', $this->lang->line('validation_name_label'), 'required'); 
    $this->form_validation->set_rules('date',$this->lang->line('validation_date_label'), 'required'); 
    $this->form_validation->set_rules('product[]', $this->lang->line('validation_product_label'), 'required'); 
    $this->form_validation->set_rules('price[]',$this->lang->line('validation_price_label'), 'required|is_numeric'); 
    $this->form_validation->set_rules('qty[]',$this->lang->line('validation_quantity_label'), 'required|is_numeric'); 

//remaing logic here after..... 

내 문제는 검증이 실패했을 경우, 나는이 때 새로 추가 된 양식 필드 페이지를 다시로드를 잃을 것입니다. 양식 오류를 존중받는 각 양식 필드로 되돌려 보내고 양식 유효성 검사가 실패 할 때 모든 작업을 계속 수행하려면 어떻게해야합니까? 내가 분명하지 않은 경우 미안합니다. 필요한 경우 추가 정보를위한 설명을 작성하십시오.

+0

를 보라? –

+0

아니, 최대한 많이 추가 할 수 있습니다. 또는 좋지 않은 경우 다른 방법을 제안하십시오. 재생을위한 감사합니다 –

+0

어떤 종류의 유효성 검사가 필요합니까? 특정 필드 아래에 오류 메시지를 표시 하시겠습니까? 아니면 폼의 맨 위 또는 맨 아래에 오류를 표시하고 싶습니까? – ss56

답변

0

각 필드에 대한 오류를 표시하는 클라이언트 측 라이브러리를 jquery_validator 사용할 수 있습니다

http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/

그리고 다른 측면에서 (서버 측) 당신이 할 수있는 모든 글로벌 검증입니다 :

$this->form_validation->set_rules('product[]', 'lang:product', 'trim|required'); 
+0

여전히 잃어버린 필드가 복제 될 수 있습니다. –

+0

$ ('form.formValidate'). validationEngine(); 귀하의 입력란에 클래스 속성을 추가하십시오 :

+0

ID가없는 필드의 유효성을 검사하는 방법.이 경우에는 컨트롤러가 어떻게 표시 될지 궁금합니다 .. –

0

양식을보다 역동적으로 만들면 다음과 같은 의미가됩니다. 뭔가 같이 :

$countProducts = 1; 

if ($this->input->post("submit", TRUE)) { 
    $countProducts = $this->input->post("product", TRUE); 
} 

for ($i = 0; $i < $countProducts; $i++){ 

echo "<div class='row productsdiv'>"; 

echo "<input type='text' name='product[" . $i . "]' class='product form-control'" . set_value("product[" . $i . "]"); 

// echo the other items 

echo "</div>"; 
} 

양식 작성 및 검증을 더 쉽게하려면, 여분의 필드 증가 제한이 없습니다 양식 세대 도서관 http://frankmichel.com/formgenlib/user_guide/installation/install.html

+0

. 필드 'name ='product [ ". $ i."] ''에 대한 새 이름을 정의 할 때 –

+0

동일한 방법으로 배열을 반복합니다. 그러나 Form Generation Library에 한 번 해보십시오. 매우 쉽고 유효성 검사도 수행합니다. 또 다른 포인트 : 로직을 컨트롤러에 넣고 생성을위한 것이 아니라 출력을 위해 뷰를 사용하십시오. – Joerg

관련 문제