• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validating forms

#1
[eluser]_Smilie_[/eluser]
Hi,
So as to my understanding I was supposed to put this somewhere in my viewfile where I want the errors to show up, in case of failure:
Code:
<?php echo validation_errors(); ?>
Very sadly this generates an error (undefined function), so I guess I made a mistake. Can any one point me in the right direction?

My controller:
Code:
function register(){
  if(!empty($_POST)){
   //Load required lib's:
   $this->load->helper(array('form', 'url'));
   $this->load->library('form_validation');
  
   //Setting rules
   $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
   $this->form_validation->set_rules('password', 'Password', 'required|matches[password_confirm]');
   $this->form_validation->set_rules('password_confirm', 'Confirm password', 'required');
   $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
   $this->form_validation->set_message('checkIPban', 'Your IP has previously been banned and you may not regiser again.');
   $this->form_validation->set_rules('ip', 'Banned IP', 'checkIPban');
  
   //check rules
   if ($this->form_validation->run() == FALSE){
    $data['formResponse'] = "Form validation failed!";
   }
  
   // load fielddatas
   if($ipbwi->member->create($_POST['username'], $_POST['password'], $_POST['email'], true, $_POST['displayname'])){
    if($ipbwi->member->login($_POST['username'], $_POST['password'])){
     header('location: '.ipbwi_WEB_URL).die();
    }
   }
  
  }
  //Show files
  $this->load->view('themes/fusion/global/form_header');
  $this->load->view('themes/fusion/account_register');
}

#2
[eluser]CroNiX[/eluser]
Not sure why that wouldn't work as validation_errors() is located in the form_helper code, which you are loading, but you can try replacing
Code:
$data['formResponse'] = "Form validation failed!";
with
Code:
$data['formResponse'] = validation_errors();
and in your view see if $formResponse has any data and display it if it does. It will be an empty string if there are no errors.

#3
[eluser]_Smilie_[/eluser]
Yea, it could fix the problem, but how about set
Code:
<?php //echo $this->input->ip_address(); ?>
It's also in my view file, but it causes my header file not to be loaded and the input field not to be shown.

#4
[eluser]CroNiX[/eluser]
They changed some things in CI and the global CI object isn't available to views. They probably did this because in MVC the Controller should be sending any data to the view and not the view directly accessing it on its own.

Just send the IP in a variable to the view just like with form_errors() or any other dynamic data you are sending to the view.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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