2011-08-22 103 views
0

我設置了$this->validation->set_message('required', 'required');但我不想使用所需的信息。取而代之的是,我必須設置一個類名,class="errorRequired"以下<p>標籤:codeigniter中的表單驗證

<tr> 
    <td colspan="2"> 
    <p> 
     <label for="label">Your name <span class="required">*</span></label> 
     <input type="text" name="name" id="name" 
      value="<?php echo set_value('name'); ?>" /> 
    </p> 
    </td> 
</tr> 

我怎樣才能做到這一點?

回答

2

您可以通過將php inline與javascript結合來實際執行這兩個操作。所以這個想法是$錯誤會觸發一個javascript函數來添加類相關的div。喜歡的東西...

<tr> 
<td colspan="2"> 
<!-- note i add id attribute below --> 
<p id="target"> 
    <label for="label">Your name <span class="required">*</span></label> 
    <input type="text" name="name" id="name" 
     value="<?php echo set_value('name'); ?>" /> 
</p> 
</td> 
</tr> 

// Inline with above html, you can have something like this (using jQuery) 
<script> 
<?php if(form_error('name') : ?> 
$(function() { 
    // Add error class 
    $("#target").addClass("errorRequired"); 
}); 
<?php endif; ?> 
</script> 
1

你只需附和類,而不是直接...

// In controller which run the callback function 
$this->form_validation->set_message('required', 'errorRequired'); 
// Then in your view 
<tr> 
<td colspan="2"> 
    <p class="<?php echo form_error('name') ?>"> 
    <label for="label">Your name <span class="required">*</span></label> 
    <input type="text" name="name" id="name" 
     value="<?php echo set_value('name') ?>" /> 
    </p> 
</td> 
</tr>