Welcome Guest, Not a member yet? Register   Sign In
Custom form validation doesn't display invidual error
#1

[eluser]JohnnyBravo[/eluser]
Hi!

I wrote a little script, that use a simple method to validate the posted values.

MY_Form_validation.php:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation
{

    function is_greater_than($value)
    {
        
        if ($value > 100)
        {
            return FALSE;
        }
        return TRUE;
    }

}

?>

The controller:
Code:
$this->form_validation->set_rules('list_limit', $this->lang->line('list_limit'), 'required|numeric|is_greater_than');

The view:
Code:
<?php echo validation_errors();
// It displays the errors correctly ?>
    <?php echo form_input('list_limit', set_value('list_limit', $list_limit), 'class="number"') . "\n"; ?>
    &lt;?php echo form_error('list_limit', '<p class="info error-message"', '</p>');
// It doesn't display the error if I post a value that is greater than 100
?&gt;

I don't know what is the problem...
#2

[eluser]danmontgomery[/eluser]
You have to set an error to be displayed:

Code:
function is_greater_than($value)
    {
        
        if ($value > 100)
        {
            $this->form_validation->set_message('is_greater_than', '%s must be less than 100');
            return FALSE;
        }
        return TRUE;
    }
#3

[eluser]JohnnyBravo[/eluser]
Thanks for your reply.

I've tried this way, but it doesn't work and I don't know why.
The validation runs well, but it doesn't display the error message if I use the form_error() function.

Any suggestion?
#4

[eluser]JohnnyBravo[/eluser]
I still couldn't find any solutions...
#5

[eluser]JohnnyBravo[/eluser]
Ooops!

I found the error...

I didn't close the <p> opening tag.
So I've changed this...
Code:
&lt;?php echo form_error('shop_url', '<p class="info error-message"', '</p>'); ?&gt;
To this...
Code:
&lt;?php echo form_error('shop_url', '<p class="info error-message">', '</p>'); ?&gt;

And everything's fine.




Theme © iAndrew 2016 - Forum software by © MyBB