CodeIgniter Forums
Problem with validation and 'trim' - 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: Problem with validation and 'trim' (/showthread.php?tid=75543)



Problem with validation and 'trim' - Fido L Dido - 02-19-2020

I'm using the validation library to validate the input received from an ajax request. I'm working through the tutorial, but converting to ajax, and moving the validations into the model. My validation rules look something like this:

PHP Code:
$validation->setRules([
            'title' => [
                'label' => 'Title',
                'rules' => 'trim|required|min_length[3]|max_length[128]'
            ],
            'body' => [
                'label' => 'Body',
                'rules' => 'trim|required|min_length[3]|max_length[65535]'
            ]
        ]); 

My validation is being run on an associative array of form elements grabbed from the ajax request:
PHP Code:
$validation->run($fields); 

This is generally working well, except for the trim. Leading and trailing spaces are being saved in the DB, and also leading spaces are being counted towards the string length, rendering the min_length[3] check as useless.

I can fix this by trimming all the inputs before running them through the validation rules, but my understanding was that the validation library was capable of doing this?


RE: Problem with validation and 'trim' - php_rocs - 02-19-2020

@Fido L Dido,

Are you sure that the trim is working properly as a rule? Have you printed it out? You could also remove trim() from the rules and add it to the sql statement to trim the value before insert/updating the database.


RE: Problem with validation and 'trim' - Fido L Dido - 02-20-2020

(02-19-2020, 01:16 PM)php_rocs Wrote: @Fido L Dido,

Are you sure that the trim is working properly as a rule?  Have you printed it out? You could also remove trim() from the rules and add it to the sql statement to trim the value before insert/updating the database.

It appears to me that none of the rules are able to alter the data it is validating, which would explain why 'trim' isn't doing anything but the other rules work fine. I shall resort to trimming the input data before validating which should address the issue. I may look to see if it is possible to create a custom rule that works with a reference to the data meaning that, in theory, it should be able to be modified.


RE: Problem with validation and 'trim' - jreklund - 02-20-2020

As you have find out, validation don't alter your data in any way. It just checks that it's correct according to your rules.
You should alter your data before inserting them in the DB, not before validation (as it checks post and not variables).


RE: Problem with validation and 'trim' - InsiteFX - 02-20-2020

If you check the validation rules in the CodeIgniter Users Guide there is no trim rule.


RE: Problem with validation and 'trim' - motownphilippe - 04-11-2021

The introduction to the CI 4 validation library states:
  • 4. Pre-format the data if needed (Does the data need to be trimmed? HTML encoded? Etc.)

The bottom of the same page sates:
  • 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).
This would include ‘trim’.

This is very misleading, especially in the context that CI 3 validation used to alter data.


RE: Problem with validation and 'trim' - kenjis - 10-24-2021

I updated the doc: https://github.com/codeigniter4/CodeIgniter4/pull/5233


RE: Problem with validation and 'trim' - ikesela - 10-25-2021

(02-19-2020, 09:39 AM)Fido L Dido Wrote: I'm using the validation library to validate the input received from an ajax request. I'm working through the tutorial, but converting to ajax, and moving the validations into the model. My validation rules look something like this:

PHP Code:
$validation->setRules([
            'title' => [
                'label' => 'Title',
                'rules' => 'trim|required|min_length[3]|max_length[128]'
            ],
            'body' => [
                'label' => 'Body',
                'rules' => 'trim|required|min_length[3]|max_length[65535]'
            ]
        ]); 

My validation is being run on an associative array of form elements grabbed from the ajax request:
PHP Code:
$validation->run($fields); 

This is generally working well, except for the trim. Leading and trailing spaces are being saved in the DB, and also leading spaces are being counted towards the string length, rendering the min_length[3] check as useless.

I can fix this by trimming all the inputs before running them through the validation rules, but my understanding was that the validation library was capable of doing this?
I dont think trim is need here. just do trim before add to database.

But trim possibly is needed when using this rules:
Code:
is_unique or is_not_unique

I got few problem here, especially for the mobile user, since the white space so small, hard to differentiate. (not all the time but for some users, mostly elderly user)