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.

2
  • stackoverflow.com/questions/5802729/… check it Commented Apr 27, 2017 at 6: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 Commented Apr 27, 2017 at 6:41

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.