Welcome Guest, Not a member yet? Register   Sign In
[solved]Form validation rules "lesser_than" not working
#1

[eluser]brucebat[/eluser]
Hi all,


Code:
$this->form_validation->set_rules ('patient_weight', 'Patient Weight', 'trim|numeric|required|lesser_than[200]|xss_clean'); //if patient is heavier than 200kg return false

For some reason if I enter anything greater than 200 it still accepts it even thought it should return saying that theres an error.

Is it my syntax

XAMPP , PHP 5.0.5 dev
#2

[eluser]toopay[/eluser]
'less_than' not 'lesser_then'
#3

[eluser]brucebat[/eluser]
Wow I feel really stupid now.

THanks
#4

[eluser]brucebat[/eluser]
Just another quick question.

When validation fails, I reload my form but the values dissapear. How can I save them?

Thanks
#5

[eluser]toopay[/eluser]
What reload? You reload from browser or your controller? If you reload at your browser, off course it will dissapear, because you are about sending a new "fresh" HTTP request!
#6

[eluser]brucebat[/eluser]
Sorry,

Code:
if ($this->form_validation->run() == FALSE)
                {
                    
                    $this->displayform(); //loads function that displays form
                }
#7

[eluser]toopay[/eluser]
And what your 'displayform' function contain? Post your whole controller is better here.
#8

[eluser]brucebat[/eluser]
Code:
class Submit extends CI_Controller
    {
    
            public function displayform()
            {
                //prevents guest users accessing or users not logged in
                if ( $this->session->userdata('name') == FALSE)
                {
                redirect ('site/index');// to home page
                }
            
            
            $page['page'] = 'submit';
            $this->load->view('template', $page );
        
            }
    
    
            public function validateform()
            
            {
                $this->load->library('form_validation'); //loads form validation library from CI
                
                
                /*for ($i =0; $i< 30; $i++)
                {
                    $this->form_validation->set_rules('time'.$i, 'Time'.$i, 'xss_clean|required');
                    $this->form_validation->set_rules('event'.$i, 'Event'.$i, 'xss_clean|required');
                    $this->form_validation->set_rules('supplies'.$i, 'Supplies'.$i, 'xss_clean');
                    $this->form_validation->set_rules('success'.$i, 'Success'.$i, '');
                    $this->form_validation->set_rules('comment'.$i, 'Comment'.$i, 'xss_clean');
                } */
            
                $this->form_validation->set_rules ('patient_age','Patient Age','numeric|trim|required|less_than[110]|xss_clean'); //if patient is older than 110 years return false
                $this->form_validation->set_rules ('patient_gender', 'Gender', 'required');
                $this->form_validation->set_rules ('patient_weight', 'Patient Weight', 'trim|numeric|required|less_than[200]|xss_clean'); //if patient is heavier than 200kg return false
                $this->form_validation->set_rules ('patient_height', 'Patient Height', 'trim|numeric|required|less_than[220]|xss_clean');  //if patient is taller than 220 cm return false
                
                
                
                
                if ($this->form_validation->run() == FALSE)
                {
                    echo "Failed validation";
                    $this->displayform();
                }
                
                else
                
                {
                    $this->load->model('submit_model');
                    $this->submit_model->create_record();
                    if ($this == FALSE)
                    {
                        echo "Failed to add to database";
                    }
                    
                    else
                    {
                        echo "Successfully added";
                    }
                }
            }

Basically what happens is when I call the function in the same controller called "Submit" I get my validation errors but the text boxes lose their values.

Once again thankyour for your kind help Smile
#9

[eluser]toopay[/eluser]
Either autoload the form validation library or put that in your controller constructor! Because with your recent code, if you put at your template/view file, this line
Code:
&lt;?php echo set_value('username') ?&gt;
What happens is, your pseudo variable at view file, on displayform function, try to find a value from form_validation library object which unfortunately, not available (because you didnt auto load or put that in your controller constructor).
Code:
function __construct()
{
  parent::__construct();
  $this->load->library('form_validation');
}




Theme © iAndrew 2016 - Forum software by © MyBB