0
votes

I have built a Login form under PHP Codeigniter framework and its validation rules are set. The rules are working fine, but what I want to achieve is something different.

The login form has Username & Password Textbox set under a container called control-group. Those who uses Bootstrap framework for HTML, knows this very well.

So the textbox code is like this :

<div class="control-group">
    <label for="username">Username:</label>
    <input type="text" id="username" name="username" value="" placeholder="Username" class="login username-field" />
    <?php echo form_error('username','<span class="help-inline error">', '</span>'); ?>
</div>

You can see I have also placed span wrappers to error, so that it can be displayed in red color style. But what other thing I want to do is, I want to apply the error class to the control-group.

So that it become from

<div class="control-group">

To

<div class="control-group error">

I have already tried this by taking $error_class variable and passing it from Controller like this.

$data['error_class'] = '';
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');

if ($this->form_validation->run() == FALSE){
    $data['error_class'] = 'error';
    $this->load->view('administrator/login_view', $data);
}

But what it does is, it applied the error class to the control-group all the time. Even without submitting the form.

Any idea how to achieve this ?

Thanks in advance.

1

1 Answers

1
votes

You have to run the validation only when the user is submitting the form.

try doing this

if($this->input->post())
{
 //your form validation
}

This will only run when user submits a post request.

EDIT:

In addition to populate error on each individual field, use form_error('field_name') to check for error in each individual fields and echo the error class

eg.

<?=(form_error('field_name')?'error':'')?>