CodeIgniter Forums
Form validation always returns false in all cases - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Form validation always returns false in all cases (/thread-52973.html)



Form validation always returns false in all cases - El Forum - 07-04-2012

[eluser]JustinWyllie[/eluser]
Hi

I am quite new to Code Igniter.

I have a form like the one below. Whenever I post it to the controller and run the validation it is always in every case invalid even with both fields filled in or even without the rules defined at all.

The form is posted to a different url than the one on which the form is displayed. It is not an ajax post - I thought that might be the problem but it is now posted by the browser in response to the submit button being pressed.

Code:
<form action="http://domain/clients/admin/business/ajax_business" method="post" accept-charset="utf-8">
  <label for="first_name">First Name</label>
  &lt;input type="input" name="first_name"&gt;&lt;br>

  <label for="last_name">Last Name</label>
  &lt;input type="input" name="last_name"&gt;&lt;br>

  &lt;input type="submit" name="submit" value="Update"&gt;

&lt;/form&gt;

Code:
public function ajax_business() {

        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

               $this->form_validation->set_rules('first_name', 'First Name', 'required');
               $this->form_validation->set_rules('last_name', 'Last Name', 'required');        
                $response = new stdClass();


                if ($this->form_validation->run() == FALSE)
                {
//whatever i do this block is always run
                        $response->result = false;
                }
                else
                {
                        $response->result = true;
                }

}

Thanks

--Justin Wyllie


Form validation always returns false in all cases - El Forum - 07-04-2012

[eluser]jmadsen[/eluser]
everything looks right to me, Justin.

Put a var_dump($_POST); on your first line of the ajax_business() function and double check what is getting passed. It is possible there is something in your setup stripping the post values


Form validation always returns false in all cases - El Forum - 07-04-2012

[eluser]tpetrone[/eluser]
Code:
&lt;form action="http://domain/clients/admin/business/ajax_business" method="post" accept-charset="utf-8"&gt;

Try :
replace the http://domain....../...../.../../ajax_business with just the path to your controller.
ie

Code:
&lt;form action="business/ajax_business" method="post" accept-charset="utf-8"&gt;


Reason.

They way you have defined the action path makes it look like you have the following stucturesetup.


applications/
controllers/
clients/
admin/
business <-- this is your controller and your calling ajax_business method.


Unless your doing something funky. Or unless you have the main CI direcctory burry deep under your domain.

Just a thought.




Form validation always returns false in all cases - El Forum - 07-04-2012

[eluser]JustinWyllie[/eluser]
Working now. No idea what the problem was.

Nope nothing funky. The root to my CI app is in /clients and then I have sub-folders in the controllers folder so /admin is just a folder under /controllers.

I think the problem may have been something like there was an error in my Ajax code so I did a POST but with no data and when I started submitting the form by a normal form submit it still sent no data. Something like that.