0
votes

I am creating rest api and for that i am adding some rules to validate form. for testing purpose i have used just variables.

my rules are as below.

$this->form_validation->set_rules('user_id', User ID, 'required');
$this->form_validation->set_rules('firstname', First Name, 'required');

my response is as below.

when i am not passing any of the variables.

The User ID field is required.\nThe Firstname field is required.\n

when i am passing only user_id with blank value.

The Firstname field is required.\n

when i am passing only user_id with some value.

The Firstname field is required.\n

when i am passing both user_id and firstname, user_id with some value and firstname as blank. The form validation gets successfull.

i don't know why codeigniter is not counting blank field as empty field not not throwing required validation on it. i am not newbee in codeingiter but never used this form validation before. i have used laravel form validator and laravel work fine. what is problem with CI?

1
could you explain, what blank value that you mean? is it a space(s), or something else? - Hasta Dhana

1 Answers

0
votes

as you said it is required in the rule. so it only checks if field is posted or not. its not check the value. if you need to check value is not empty you can use something like this

$this->form_validation->set_rules('user_id', User ID, 'required|trim');
$this->form_validation->set_rules('firstname', First Name, 'required|trim');

it checks if it is empty or have only space character it shows that it has error.

remember to translate your language for the trim validation.