phpcodeignitervalidationconditional-statements

How to print CodeIgniter's form_error() message or else some other HTML


I display the form validation error in codeigniter as below:

<?php echo form_error('name', '<div class="form_error">', '</div>'); ?>

I want to do it so that if there is error, then it should print error, otherwise it should print the info div.

For example,

if form_error, then 

<?php echo form_error('name', '<div class="form_error">', '</div>'); ?>

else
<div class="info">Your first and last name. </div>

As form_error() is not just a simple variable that I can check if it is empty then print info. How can I do it?


Solution

  • You can do something like this:

    if ( form_error('name') )
    {
      echo form_error('name');
    }
    

    For form_error might not be a variable but it's a function that returns a string. If the string is empty (NULL, FALSE, "", 0, ...), the if statement will fail (meaning there is no error) and the form_error('name') won't be called.