Welcome Guest, Not a member yet? Register   Sign In
Form validation checkbox doesn't work
#1

(This post was last modified: 11-08-2020, 05:28 AM by gbwebapps.)

Hello there.

I have a form with some checkboxes.
These checkboxes are not mandatory.

They work fine with is_exist rule.

This scenario works fine.

Checkboxes:
Code:
<input class="form-check-input" type="checkbox" name="users_permissions[]" value="add_brands">
<input class="form-check-input" type="checkbox" name="users_permissions[]" value="edit_brands">
<input class="form-check-input" type="checkbox" name="users_permissions[]" value="delete_brands">

Rules
Code:
'users_permissions[]' => [
'label'  => 'Permissions',
'rules'  => 'if_exist|alpha_dash'
]

When I send data, all is working fine.

But if I intentionally put a number as a checkbox value like this...
Code:
<input class="form-check-input" type="checkbox" name="users_permissions[]" value="1">

...having alpha_dash as a rule, I should receive an error which not occur.

The same if I upside down the situation like this:
Checkboxes
Code:
<input class="form-check-input" type="checkbox" name="users_permissions[]" value="add_brands">

Rules
Code:
'users_permissions[]' => [
'label'  => 'Permissions',
'rules'  => 'if_exist|Integer'
]

I don't get any error. The form is regularly sent.

Where am I wrong with checkboxes validation?

OK, I've just read a topic of yesterday and I read the guide in at this point:

Validating keys that are arrays

So, I'm trying this solution.:

Instead of
Code:
'users_permissions[]' => [
    'label'  => 'Permissions',
    'rules'  => 'if_exist|alpha_dash'
]

I put now this
Code:
'users_permissions.*' => [
    'label'  => 'Permissions',
    'rules'  => 'if_exist|alpha_dash'
]

But I don't get error messages anyway.
Some advices?
Thanks

Also tryng this way:
Code:
'users_permissions.*' => [
    'label'  => 'Permissions',
    'rules'  => 'if_exist|alpha_dash',
    'errors' => ['alpha_dash' => 'Error!!!']
]
Reply
#2

(This post was last modified: 11-08-2020, 06:51 AM by gbwebapps.)

Just a little piece of update.
I have this problem when I call getError(), for the single validation

This one doesn't return the array errors
Code:
$this->validator->getError('checkboxes')

This one returns the array errors
Code:
$this->validator->listErrors()
Reply




Theme © iAndrew 2016 - Forum software by © MyBB