Welcome Guest, Not a member yet? Register   Sign In
An efficient system to trim form data
#1

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?
Reply
#2

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).
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 05-02-2021, 01:50 AM by motownphilippe.)

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

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

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB