Twitter Bootstrap and CodeIgniter's Validation_Errors

Go To StackoverFlow.com

0

I am using Twitter Bootstrap to style validation_errors for a registration page:

<?php
echo "<div class='alert alert-error span4'>";
echo validation_errors(); 
echo "</div>";
?>

The validations work and show up but part of the styling is always present (the div tag has a red background). Is there a way to have the styling show up ONLY when the validation_errors are present. I have tried a few things (embedding html in php tags and enclosing the php in div tags) but the result is the same.

2012-04-04 01:35
by Julian25


2

You can try something like this it works for me.

<?php if(validation_errors()):?>
<div class='alert alert-error span4'><?php echo validation_errors(); ?></div>
<?php endif;?>
2012-04-04 12:24
by Altrim
This did it for me. Thanks so much - Julian25 2012-04-04 18:45
Even better, store the validation_errors() call in a variable, pass that to the view, so you don't have to call the same function twice - Greg 2012-04-04 23:16


4

The reason that the div structure still appears is because it's echoing without regard to whether there are errors or not.

You could set the value of a variable to the result of your validation_errors() function in your Controller, then only display the alert div in your View if you've actually got an error...

Your controller would assign the variable to hold the (potential) error:

$this->data['reported_error'] = validation_errors();
$this->load->view('view_name', $this->data);

Then your view would only display the alert div if there was an error:

if ( isset($reported_error) )
{
    echo "<div class='alert alert-error span4'>".$reported_error."</div>";
}

This requires your validation_errors function to only return a value if there is an error.

2012-04-04 02:35
by James Chevalier
I attempted to do this but am still getting the red background as part of the div. Is there any other way to check if there are validation errors and if so present them with specific styling - Julian25 2012-04-04 04:59
If you're still getting the red background, it means you're doing something wrong. This way will work.

Can you update your code - Bram 2012-04-04 09:34

The problem is likely in the validation_errors() code - if it returns anything then that will cause isset() to be true. So, your validation_errors() function should only return anything if there is an error - James Chevalier 2012-04-04 21:47


1

I use the following construct to put the errors with special classes next to the fields that they reference:

<div class="<?php echo my_error_class('fieldname') ?>">
<input type="whatever" name="fieldname" />
<span class="whatever"><?php echo my_error_msg('fieldname') ?></>
</div>

with the following functions in a CI helper:

<?php
    if ( ! function_exists('my_error_class')) {
      function my_error_class($field, $error_class = "error") {
        if (FALSE === ($OBJ =& _get_validation_object())){
            return '';
          }
        if(isset($OBJ->_field_data[$field]['error']) && !empty($OBJ->_field_data[$field]['error'])) {
          return $error_class;
          }
        else {
          return '';
        }
      }
    }

if ( ! function_exists('my_error_msg')) {
  function my_error_msg($field,$default = '') {
    if (FALSE === ($OBJ =& _get_validation_object())){
        return $default;
      }
    if(isset($OBJ->_field_data[$field]['error']) && !empty($OBJ->_field_data[$field]['error'])) {
      return $OBJ->_field_data[$field]['error'];
      }
    else {
      return $default;
    }

  }
}
2012-05-01 16:07
by dwilkins
I am getting an error here "Cannot access protected property CIFormvalidation::$fielddata" pointing to the myerrorclass function, line that starts if(isset - Kinjal Dixit 2013-08-23 19:54


1

I was having the same issue. I assigned validation_errors() to a variable in my controller and passed it to the view.

Going off James' solution, I had to change:

if ( isset($reported_error) )

to:

if (!empty($reported_error))

Once I did this, the alert block only displayed when there was an error.

2018-01-06 01:27
by Kyle McCardle


0

I didn't find this in the guide but apparently validation_errors takes two optional args for a prefix and suffix... so you can do:

validation_errors('<p class="text-warning">', '</p>');
2013-04-30 19:06
by user2337224
Ads