CodeIgniter Forums
Form validation last validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Form validation last validation (/showthread.php?tid=527)



Form validation last validation - BABYpanda - 12-14-2014

Hey all,

I'm running a form validation which checks if all the fields are being send and within the range 0 - 10. Then the amount of this value is multiplied by a set amount in the config.

For example:

Field 1 --> 5
In config is set field 1 amount = 5.00.
So 5 * 5.00 = 25.00.

This applies to all the fields.

In the end I'd like to run a validation which counts all the sent fields and checks if it raises above the amount of 500.00. If so, it should return an error.

Is there any way to do this? I've been staring at the documentary for a few hours and I cannot find a proper way how to validate AFTER all fields have been validated...

I hope someone is able to help me out, I hope you understand what I am trying to achieve. All help is greatly appreciated.

Thanks in advance!

Kindest regards,
Reno


RE: Form validation last validation - includebeer - 12-14-2014

You can create your own rule with a callback function: http://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks

You need to validate multiple values from the submitted form, so you can read all the values you need from the POST array with $this->input->post and return TRUE or FALSE if the total amount is <= 500.


RE: Form validation last validation - BABYpanda - 12-14-2014

As far as I know the callback can only be called on a direct field, not on fields grouped together, or am I wrong there?

I am now doing a callback on every field to check if the value is more than 500.00, but it returns multiple errors on every field then when the value is higher than 500.00, that's the problem I am struggling with.

So I'm not sure if there's a way to do it using a callback to check if all fields grouped together > 500.00.


RE: Form validation last validation - Rufnex - 12-14-2014

You can make your calculation after the form validation like that

PHP Code:
if ($this->form_validation->run() == false)
{
   
// Error handling
}
else
{
   
$post $this->input->post();
   
// Do you calculation directly or with a method




RE: Form validation last validation - BABYpanda - 12-14-2014

aahh thanks for your answer! I got it fixed now Smile

Though I am struggling to get this working:
$this->form_validation->set_message('required', $this->lang->line('tb_err_field_required'));

Apparently you are not allowed to use language in set_message? How can I change the error message depending on what language the user has selected?

Thanks
- Reno

EDIT: I guess this has to be done in the form_validation.php file per language instead of a custom language file ? But I'd like to do this form dependent, without setting the language in the file itself :<


RE: Form validation last validation - Rufnex - 12-14-2014

You can make a language file under language/your_language/form_validation_lang.php we have a couple of language packs at

https://github.com/bcit-ci/codeigniter3-translations


RE: Form validation last validation - BABYpanda - 12-14-2014

And thanks again for your reply. I found the error as I was using multiple %s in my language file.

Basically one sprintf only covered 3 of the 4 %s so it output some decrypted errors.. I escaped it by another % in front of it so a second time a sprintf is ran it will be covered though.

Thanks though Smile


RE: Form validation last validation - Rufnex - 12-14-2014

Perfect ;o)