CodeIgniter Forums
An efficient system to trim form data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: An efficient system to trim form data (/showthread.php?tid=79162)



An efficient system to trim form data - motownphilippe - 05-01-2021

The Codeigniter 3 Validation class was able to trim data. This was an efficient. The validation class in CI4 does not offer preformatting like this (despite the documentation "Pre-format the data if needed (Does the data need to be trimmed? HTML encoded? Etc.)".

Does anybody have an efficient, clean way of doing this to all their form data before it is validated and saved?


RE: An efficient system to trim form data - InsiteFX - 05-02-2021

Yes it doe's.

You can also use any native PHP functions that permit up to two parameters, where at least one is required (to pass the field data).


RE: An efficient system to trim form data - motownphilippe - 05-02-2021

So why can’t I just use trim()?

See this thread (also commented on by InsiteFX) 
https://forum.codeigniter.com/thread-75543.html


RE: An efficient system to trim form data - InsiteFX - 05-02-2021

Yes you are correct I think it is miss understanding also. I think you need to trim the data from the input
before it is ran through the validation.


RE: An efficient system to trim form data - alphastudiofr - 02-10-2023

This is probably not very clean, but what I do is add "preformat" entries to the rules array like this :

Code:
$rules = [
      'ville' => [
                'label' => 'Ville',
                'rules' => 'required|max_length[50]',
                'preformat' => 'trim|convert_accented_characters',
      ],
]

then pass the rules to my own prep_form_data($rules) helper function in the controller before running the validation itself.

The main advantage for me is to keep the pre-formatting instructions with the validation rules in the code logic.
When all is said and done, in my usage, "required" is useless on a text input that hasn't been trimmed, thus the validation rules needs to be informed by the pre-formatting.