Ashitha
Ashitha

Reputation: 81

form validation in codeigniter

I set $this->validation->set_message('required', 'required');but i don't want to use the required message. Instead of this i have to set a class name, class="errorRequired" to the following <p> tag:

<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>

How can I accomplish this?

Upvotes: 0

Views: 1164

Answers (2)

toopay
toopay

Reputation: 1635

You can just echoing the class directly instead...

// 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>

Upvotes: 1

user904142
user904142

Reputation:

You can do both actually, by combining php inline with javascript. So the idea is $error will triggering a javascript function to add class related div. Something like...

<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>

Upvotes: 2

Related Questions