0

I am working with validation on a view with 2 forms (login/register).

I am using isset method to check which form is submitted and accordingly run the validation.

Controller function login

   public function login(){

    if (isset ($_POST['btnSignIn']))    
    {        
  if($this->form_validation->run() == FALSE)  {
        // do tasks
    }
}

Controller function register

   public function register(){

    if (isset ($_POST['btnSignUp']))    
    {        
  if($this->form_validation->run() == FALSE)  {
        // do tasks
    }
}

Even with these code in place, Submitting either form displays error for both the forms.

However I have config array initialized in form_validation.php with all the controls in both forms.

And removing any of these control from config array is the only way I can stop errors from displaying, but then it wont display ever.

My question is, Is there any way I can implement this to separate config array or validation for proper display of messages.

I hope my issue is clear.

Any help is appreciated.

Dheeraj Kumar
  • 3,917
  • 8
  • 43
  • 80
  • http://stackoverflow.com/questions/5802729/codeigniter-2-forms-on-one-page-validation-errors-problem check it – Ahmed Ginani Apr 27 '17 at 06:37
  • @AhmedGinani I believe you have misunderstood my question. My code can identify which form is submitted, but even after that errors displayed for both forms because all the controls are initialized in form_validation.php which is global. FYI - I had tried your solution before posting this question – Dheeraj Kumar Apr 27 '17 at 06:41

1 Answers1

0

This is how I got it worked.

As mentioned in CodeIgniter documentation. https://www.codeigniter.com/userguide3/libraries/form_validation.html#saving-sets-of-validation-rules-to-a-config-file

This is how we can separate controls initialized in config array for each form.

$config = array(
    'signup' => array(
            array(
                    'field' => 'username',
                    'label' => 'Username',
                    'rules' => 'required'
            ),

    ),
    'email' => array(
            array(
                    'field' => 'emailaddress',
                    'label' => 'EmailAddress',
                    'rules' => 'required|valid_email'
            ),
            a
    )
);

And while running validation in controller, we need to use

$this->form_validation->run('signin')
// this will run validation only for controls initialized under signin array

instead of

$this->form_validation->run()
// this will run validation only for all controls
Dheeraj Kumar
  • 3,917
  • 8
  • 43
  • 80