Welcome Guest, Not a member yet? Register   Sign In
Form Validartion with input array
#1

(This post was last modified: 05-11-2020, 11:15 AM by Misael Picco.)

Hello!

I'm beginning to work with CI4 and I got a problem with Form Validation, I'm working with an input array called "tags[]" but the problem is when the validation runs.

If I use:


PHP Code:
$rules = [
    'tags' => ['label' => 'Tags''rules' => 'required|numeric']
]; 

Or

PHP Code:
$rules = [
    'tags.*' => ['label' => 'Tags''rules' => 'required|numeric']
]; 




It validates the required but when I fill the input I get:
Argument 1 passed to CodeIgniter\Validation\FormatRules::numeric() must be of the type string or null, array given, called in...

If I use:
PHP Code:
$rules = [
    'tags[]' => ['label' => 'Tags''rules' => 'required|numeric']
]; 

It doesn't recognize the input.

Thank you for all the help!
Reply
#2

Those function only works with an string, as the error messages states. You need to create your own validation rule, that foreach your array and on each item check numeric().

https://codeigniter.com/user_guide/libra...parameters

Create a array_numeric function. And pass it along as array_numeric[], as you will need to have access to the $data array.

[] in this case are used for optional parameters, it dosen't tell CodeIgniter that the input are an array or not.
Reply
#3

(05-11-2020, 10:10 AM)jreklund Wrote: Those function only works with an string, as the error messages states. You need to create your own validation rule, that foreach your array and on each item check numeric().

https://codeigniter.com/user_guide/libra...parameters

Create a array_numeric function. And pass it along as array_numeric[], as you will need to have access to the $data array.

[] in this case are used for optional parameters, it dosen't tell CodeIgniter that the input are an array or not.
 
Oh, thanks, I thought that it would work as CI3 did with the input names with [].

I have an idea to do that to all kind of rules I'm gonna try that, thanks again.
Reply
#4

Hmm, interesting. Didn't even know that where possible, but the userguide says it is (and looks like it does in the source code too). Have no recollection if I have used it and forgot about it. I use in_list a lot, as I knew what type of data I wanted. But that only works if you got an <select> pretty much.
Reply
#5

(05-11-2020, 10:10 AM)jreklund Wrote: Those function only works with an string, as the error messages states. You need to create your own validation rule, that foreach your array and on each item check numeric().

https://codeigniter.com/user_guide/libra...parameters

Create a array_numeric function. And pass it along as array_numeric[], as you will need to have access to the $data array.

[] in this case are used for optional parameters, it dosen't tell CodeIgniter that the input are an array or not.
 Oh, I just realized that I let "numeric[]" It was my last resource, I know It is for parameters but I did it just in case, I already edit the post.  Big Grin
Reply
#6

Okay, then this should be the valid one, according to the user guide. Can't say why it dosen't work thought.

PHP Code:
$rules = [
    
'tags.*' => ['label' => 'Tags''rules' => 'required|numeric']
]; 
Reply
#7

(05-11-2020, 11:19 AM)jreklund Wrote: Hmm, interesting. Didn't even know that where possible, but the userguide says it is (and looks like it does in the source code too). Have no recollection if I have used it and forgot about it. I use in_list a lot, as I knew what type of data I wanted. But that only works if you got an <select> pretty much.

I thought to use in_list but I don't know if it is a good idea because the tags come from a database and the list may become larger.
Reply
#8

It looks like in CodeIgniter 4.0.3 they solve this, I was using 4.0.2.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB