How to check for Form validation error for an individual element in codeigniter ?
I need to set 'has-error' class to the elements which failed the validation. This is not about printing validation errors.
This is what I currently have :
<div class="form-group <?php /**if validation error for this element then*/ echo 'has-error'; ?>">
<label for="name">Name</label>
<div class="validation-error">
<?php echo form_error ( 'name' ); ?>
</div>
<div class="input-group">
<div class="input-group-addon"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></div>
<input id="name" type="text" class="form-control validate" name="name" placeholder="Your full name" value="<?php echo set_value('name'); ?>">
</div>
</div>
Can you try like below
<div class="form-group <?php echo form_error('name') ? 'has-error' : ''; ?>">
<label for="name">Name</label>
<div class="validation-error">
<?php echo form_error ( 'name' ); ?>
</div>
<div class="input-group">
<div class="input-group-addon"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></div>
<input id="name" type="text" class="form-control validate" name="name" placeholder="Your full name" value="<?php echo set_value('name'); ?>">
</div>
The form_error
method will be empty, if there is no error on that field. So you have to check like this:
<div class="form-group <?php echo form_error('name') !== '' ? 'has-error' : '' ?>">
<label for="name">Name</label>
<div class="validation-error">
<?php echo form_error ( 'name' ); ?>
</div>
<div class="input-group">
<div class="input-group-addon"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></div>
<input id="name" type="text" class="form-control validate" name="name" placeholder="Your full name" value="<?php echo set_value('name'); ?>">
</div>
</div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.