I have a form that worked perfectly until I switched the form to method="get"
. Now I can't get form_validation->run()
to evaluate to TRUE.
This is how I open the form:
echo form_open( '', array( 'method' => 'get' ) );
This is the only piece that needs to validate:
$this->form_validation->set_rules( 'states', 'states', 'required' );
This is how I check to see if the form is validated:
if( $this->form_validation->run() == FALSE )
Is there something else I need to do to use Get parameters? I have get parameters turned on in the config ( $config['allow_get_array'] = TRUE;
). The form works ok if I skip the validation, so I know the CI system is reading the url fine.
For CodeIgniter 3, you can pass the GET array into the set_data
function. For example:
$this->form_validation->set_data($this->input->get());
This post on the Codeigniter Forum suggests that form validation does not work with Get Parameters and that is just the way Codeigniter is.
Just add:
$_POST['states'] = $this->input->get('states');
for validate states field in form validation just before
$this->form_validation->set_rules('states', 'states', 'required|trim');
Codeigniter has changed since some of these posts. I think gX's answer is correct.
The instructions in the user manual, specifically section Validating an Array (other than $POST), worked great for me (as of today) and it's very simple.
Before your $this->form_validation->set_rules line, you specify the array to be validated:
$data = array(
'username' => 'johndoe',
'password' => 'mypassword',
'passconf' => 'mypassword');
$this->form_validation->set_data($data);
As you can see on the Form_validation.php file, they have hardcoded $_POST everywhere in the class ;-(
If you want to support $_GET as well, it's quite easy (and dirty Oooo), you can create a new property :
public function __construct($rules = array())
{
$this->inputData = array_merge($_POST, $_GET);
...
}
And replace $_POST by $this->inputData everywhere in this class since there is no static methods.
I know it's not recommenced to edit vendor files, but really, you need it, do it ;-)
Was trying to do exactly something similar but it boils down to the structure of your code.
- Submit your form with the usual post method. This will allow you to carry out all post validations.
- Then use the redirect() to send all your data to a different method within or outside your controller using '/'.
- Finally, the method receiving all the parameters will handle the database transactions and present it to the view.
Hope it helps.