• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form validation stange issue

#1
[eluser]BenRob[/eluser]
I've used CI for a good couple of years and used the function a lot but for some reason its not working this time

please please please can someone help

Code:
$this->load->library('form_validation');
  $this->load->helper('cookie');

  $data = array();


  if($_POST) {
   // Set validation rules including additional validation for uniqueness
   $this->form_validation->set_rules('yourname', 'Your Name', 'trim|required');
   $this->form_validation->set_rules('youremail', 'Your Email', 'trim|required|valid_email');
   $this->form_validation->set_rules('friendname', 'Friends Name', 'trim|required');
   $this->form_validation->set_rules('friendemail', 'Friends Email', 'trim|required|valid_email');

   // Run the validation and take action
   if($this->form_validation->run()) {
    echo 'valid';
   }
  }
  else{
   echo 'problem';
  }

#2
[eluser]danmontgomery[/eluser]
Can you be more specific about what the problem is?

#3
[eluser]BenRob[/eluser]
the validation just isn't running

When i submit the form i get 'problem' echoed

I've tried to take it right back to find the problem and a simple valid or problem shows the validation is not working

Any ideas its killing me

#4
[eluser]Bart v B[/eluser]
You need to setup youre view where the form is..
there you need to add the 'error views'.

Just add this in youre view where youre form is:
Code:
<?php echo validation_errors(); ?>

Yes, you can seperate it, look at the user guide for that Wink


And youre controller:
Code:
<?php  
  $this->load->library('form_validation');
  $this->load->helper('cookie');


   // Set validation rules including additional validation for uniqueness
   $this->form_validation->set_rules('yourname', 'Your Name', 'trim|required');
   $this->form_validation->set_rules('youremail', 'Your Email', 'trim|required|valid_email');
   $this->form_validation->set_rules('friendname', 'Friends Name', 'trim|required');
   $this->form_validation->set_rules('friendemail', 'Friends Email', 'trim|required|valid_email');
   $this->form_validation->set_error_delimiters('<div class="error">','</div>');

  if($this->form_validation->run() == FALSE)
  {
      
    $this->load->view('Youre_Form_View');    
  }
  else
    {
      // do something..    
    }

#5
[eluser]danmontgomery[/eluser]
That doesn't mean it's not running, it just means that it's not passing validation... What if you put:

Code:
echo validation_errors();

instead of 'problem'?

#6
[eluser]Bart v B[/eluser]
Noctrum..

it is a 'problem' when it's not running Wink
Just joking..

#7
[eluser]flaky[/eluser]
are you using HMVC ?

#8
[eluser]hagbard Celine[/eluser]
[quote author="BenRob" date="1271292550"]I've used CI for a good couple of years and used the function a lot but for some reason its not working this time

please please please can someone help

Code:
$this->load->library('form_validation');
  $this->load->helper('cookie');

  $data = array();


  if($_POST) {
   // Set validation rules including additional validation for uniqueness
   $this->form_validation->set_rules('yourname', 'Your Name', 'trim|required');
   $this->form_validation->set_rules('youremail', 'Your Email', 'trim|required|valid_email');
   $this->form_validation->set_rules('friendname', 'Friends Name', 'trim|required');
   $this->form_validation->set_rules('friendemail', 'Friends Email', 'trim|required|valid_email');

   // Run the validation and take action
   if($this->form_validation->run()) {
    echo 'valid';
   }
  }
  else{
   echo 'problem';
  }
[/quote]

remove your $_POST condition.

or try this...

Code:
$this->load->library('form_validation');
  $this->load->helper('cookie');

  $data = array();


  if(!empty($_POST)) {
   // Set validation rules including additional validation for uniqueness
   $this->form_validation->set_rules('yourname', 'Your Name', 'trim|required');
   $this->form_validation->set_rules('youremail', 'Your Email', 'trim|required|valid_email');
   $this->form_validation->set_rules('friendname', 'Friends Name', 'trim|required');
   $this->form_validation->set_rules('friendemail', 'Friends Email', 'trim|required|valid_email');

   // Run the validation and take action
   if($this->form_validation->run()) {
    echo 'postback: validation ok';
   }
else {
echo 'postback: validation error';
}
  }
  else{
   echo 'no postback: problem';
  }


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.