Is there a creative and easy way to check many form fields at once.
I have a form with generated fields on the fly, each has a unique id.
The thing is submittin
Right now, I have a custom function which will batch check every submission on my form to make sure it meets a set of rules. I am thinking that it may work to set up my form as an html array (data[]
), retrieve it in code igniter, then use the CI callback_
validation function to make sure everything turns out okay. It seems complex, so I haven't yet entirely wrapped my head around it, but maybe this can get your wheels turning in the right direction.
EDIT:
$this->load->library('form_validation');
// If there is any posted data, then we should assign it to our $post_data array.
$post_data = $this->input->post('project_data');
if (empty($post_data)) {die('empty form');}
// Now, we are ready to validate the incoming data.
// We will send the data through a callback function which will check to make sure it is valid.
// If it is not valid, the callback function will trigger a codeigniter validation error.
// Let's temporarily remove any commas from the submission data to avoid delimiter confusion when sending it through the callback
$post_data = str_replace(",", "DELIMITEDCOMMA", $post_data);
$post_data_str = http_build_query($post_data);
$this->form_validation->set_rules("project_data[errors]", 'Errors', "required|callback__validate_project_data[$post_data_str]");
$this->form_validation->run();
Then, just write your custom validation function based on what it is that you need to validate.
function _validate_project_data($value, $request)
{
// A callback rule check is being attempted by the CI validator
// $value is the actual value of the submission, while $request is the key and value
$request = explode(",", $request);
$request = str_replace("DELIMITEDCOMMA", ",", $request);
// rename the keys in the request back to the original convention
parse_str($request[0], $request);
//var_dump($request);
// perform validation here and return true or false (valid or invalid)
}
Not entirely sure what you mean, as the fields would have to be checked individually whichever way you do it. Unless you're concatenating all the inputs and checking that? Maybe I've missed something there though.
Just go for client side: http://flowplayer.org/tools/demos/validator/index.html
Serverside: http://formigniter.org/
Hope this helps...
Try this:
$_POST['data_you_want_to_validate_together'] = $_POST['first_field'] . $_POST['second_field'];
$this->form_validation->set_rules('data_you_want_to_validate_together','Some Data', 'required|callback_some_function');
Now you can get the data via:
echo $this->input->post('data_you_want_to_validate_together');