Welcome Guest, Not a member yet? Register   Sign In
How do you prevent validation errors from showing up on first load?
#1

[eluser]The Revel[/eluser]
I made myself a form with validation rules, that upon validate, it does some SQL work, else it shows the form. However when i load the page for the first time, it shows me validation errors because nothign has been filed out yet.

The format i have is as follows (code condensed as its a lotta code):
Code:
//validate form input
  $this->form_validation->set_rules('qty1', 'First Quantity', 'required|xss_clean');
  $this->form_validation->set_rules('pr1', 'First Price', 'required|xss_clean');
  $this->form_validation->set_rules('admin', 'Admin ID', 'required|xss_clean');

if ($this->form_validation->run() == true)
  {
    //Run SQL Queries, do some back end work
    //...
    //If Successful set the flashdata message
    $this->session->set_flashdata('message', "Transaction(s) Added");
    //Pass a viable back using flashdata
    $this->session->set_flashdata('searchcode', $this->input->post('searchcode', true));
    //redirect them back        
    redirect('auth/search_scancode/', 'refresh');
  }
else
  {
    $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
    // pass form value ararys
    // ...
     $this->template
             ->title( 'My Site', 'Print Card')
             ->set_layout('default')
             ->build('auth/scan_view', $this->data);
  }

Is there a way to prevent the passing of the validation errors when nothing has been entered to begin with?
#2

[eluser]CroNiX[/eluser]
Sure, only run the validation if the form has been submitted.
Code:
//Check for existence of submit button
if ($this->input->post('name-of-your-submit-buttom') !== FALSE)
{
  if ($this->form_validation->run() === FALSE)
  {
    //fail
  }
  else
  {

  }
}
#3

[eluser]The Revel[/eluser]
Not sure if this would help... I am not sure where to put the validation etc.
#4

[eluser]CroNiX[/eluser]
Depends on your intention. Your original code was showing something that happened if it passed validation and something else if it didn't, so that's what my code was based on except it only does it if the form was submitted.
#5

[eluser]The Revel[/eluser]
[quote author="CroNiX" date="1337033727"]Depends on your intention. Your original code was showing something that happened if it passed validation and something else if it didn't, so that's what my code was based on except it only does it if the form was submitted.[/quote]

I am so confused now LOL.

I have looked at the Login and change password functions in Ion Auth, and they follow the same thing I have

Code:
function login()
{
  $this->data['title'] = "Login";

  //validate form input
  $this->form_validation->set_rules('identity', 'Identity', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');

  if ($this->form_validation->run() == true)
  { //check to see if the user is logging in
   //check for "remember me"
   $remember = (bool) $this->input->post('remember');

   if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
   { //if the login is successful
    //redirect them back to the home page
    $this->session->set_flashdata('message', $this->ion_auth->messages());
    redirect('account', 'refresh');
   }
   else
   { //if the login was un-successful
    //redirect them back to the login page
    $this->session->set_flashdata('message', $this->ion_auth->errors());
    redirect('auth/login', 'refresh'); //use redirects instead of loading views for compatibility with MY_Controller libraries
   }
  }
  else
  {  //the user is not logging in so display the login page
   //set the flash data error message if there is one
   $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

   $this->data['identity'] = array('name' => 'identity',
    'id' => 'username',
    'type' => 'text',
                'placeholder' => 'Email',
    'value' => $this->form_validation->set_value('identity'),
   );
   $this->data['password'] = array('name' => 'password',
    'id' => 'password',
                'placeholder' => 'Password',
    'type' => 'password',
   );

   //$this->load->view('auth/login', $this->data);
            $this->template
            ->title( 'MyPrintCard.info', 'Login')
            ->set_layout('login')
            ->build('auth/login', $this->data);
  }
}

And it does not show the Validation Errors.
Yet mine does.




Theme © iAndrew 2016 - Forum software by © MyBB