Welcome Guest, Not a member yet? Register   Sign In
Problem with validation and 'trim'
#8

(This post was last modified: 10-25-2021, 12:02 AM by ikesela.)

(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)
Reply


Messages In This Thread
RE: Problem with validation and 'trim' - by ikesela - 10-25-2021, 12:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB