Aura.Filter

23 views
Skip to first unread message

Henry Combrinck

unread,
Feb 4, 2018, 8:27:26 AM2/4/18
to The Aura Project for PHP
Greetings,

Trying out Aura.Filter for the first time instead of competing array validation libraries.

Is it possible to validate an array which has sub-arrays?  For example:

$subject = array(
   
'username' => 'shskfhsdjh',
   
'subArray' => array(
       
1,2,3
   
),
   
'subArray2' => array(
       
'a1' => 'a',
       
'a2' => 'b',
   
)
);


Validating 'username' is easy, but how to validate the sub-arrays 'subArray' and 'subArray2'?:

# validate username
$filter
->validate('username')->is('alnum');
$filter
->validate('username')->is('strlenMin', 6);


If it's possible, I'd appreciate some pointers.

Thanks
Henry



Hari K T

unread,
Feb 4, 2018, 10:20:38 AM2/4/18
to aur...@googlegroups.com
I think you can use one of the below ones

1 . Callback https://github.com/auraphp/Aura.Filter/blob/2.x/docs/validate.md#callback and write custom logic if you prefer


There is inKeys : https://github.com/auraphp/Aura.Filter/blob/2.x/docs/validate.md#inkeys  ( But this probably is not what you are looking for in the case of subArray2 , but may be a combination of inValues and inKeys may help ? )

Hari K T

You can ring me : +91 9388 75 8821

Skype  : kthari85
Twitter : harikt

--
You received this message because you are subscribed to the Google Groups "The Aura Project for PHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to auraphp+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Henry Combrinck

unread,
Feb 7, 2018, 10:13:57 AM2/7/18
to The Aura Project for PHP
On Sunday, February 4, 2018 at 5:20:38 PM UTC+2, Hari K T wrote:
I think you can use one of the below ones

1 . Callback https://github.com/auraphp/Aura.Filter/blob/2.x/docs/validate.md#callback and write custom logic if you prefer


There is inKeys : https://github.com/auraphp/Aura.Filter/blob/2.x/docs/validate.md#inkeys  ( But this probably is not what you are looking for in the case of subArray2 , but may be a combination of inValues and inKeys may help ? )

Hari K T

You can ring me : +91 9388 75 8821

Skype  : kthari85
Twitter : harikt


Thanks Hari - I also managed to figure this out using 'array' and a callback:

Valitron\Validator::addRule('arrayMin',
    function($field, $value, array $params, array $fields) {
    if (count($value) < $params[0]) {
        return false;
    }
    return true;
}, 'array size too small');

Valitron\Validator::addRule('inArray',
    function($field, $value, array $params, array $fields) {
    print "DEBUG: value:--> ".print_r($value,true)."<-- in params:[" . print_r($params,true)."]\n";
    if (in_array($value, $params)) {
        return true;
    }
    return false;
}, 'value not in array');

$rules
= [
    'agentConf.*.groups' => ['required', 'array', ['arrayMin', 2]]
];


However, the following has me a bit stumped:

Given the following JSON structure:

$data = <<<ENDDATA
    {
        "agentConf": [{
            "first_name": "aaaa",
            "last_name": "bbbbb",
            "groups": [
                "sales",
                "Other"
            ],
            "groupPerms": [
                {
                "Other": [
                        "111",
                        "222",
                        "333"
                        ]
                },
                {
                "stats": [ "111" ]
                },
                {
                "sales": [ "111" ]
                }
            ]
        }]
    }
ENDDATA;

$rules = [
    'agentConf' => ['required', 'array', ['arrayMin', 1]],
        'agentConf.*.first_name' => ['required', ['regex', '/.*/']],
        'agentConf.*.last_name' => ['required', ['regex', '/.*/']],
        'agentConf.*.groups' => ['required', 'array', ['arrayMin', 2]],
            'agentConf.*.groups.*' => ['required', ['regex', '/.*/']],
        'agentConf.*.groupPerms' => ['required', 'array', ['arrayMin', 3]],
            'agentConf.*.groupPerms.*' => ['required', ['inArray', ['Other', 'stats', 'sales']]]
];
$data = json_decode($data, true);

$validator = new Valitron\Validator($data);
$validator->mapFieldsRules($rules);

if ($validator->validate()) {
    echo "OK\n";
} else {
    print_r($validator->errors());
    print "ERROR\n";
}

Validating 99% of the structure is relatively simple, however I'm not sure how to validate the groupPerms array to check whether it contains the keys "Other", "stats" and "sales"  --  ie the last rule:

'agentConf.*.groupPerms.*' => ['required', ['inArray', ['Other', 'stats', 'sales']]]

I tried inKeys, but it fails since it has not been registered with ::addRule().  I was under the impression it's a built-in Rule...

Thanks for any assistance.

Regards
H

Hari K T

unread,
Feb 7, 2018, 10:37:32 AM2/7/18
to aur...@googlegroups.com
I have not used Valitron nor this group supports discussion of a different library unless you are looking to use the two via some sort of bindings.

You may want to get support for Valitron on the means they provide.

Hope that helps and clear things.

Thank you.

Hari K T

You can ring me : +91 9388 75 8821

Skype  : kthari85
Twitter : harikt

--

Henry Combrinck

unread,
Feb 7, 2018, 11:05:14 AM2/7/18
to The Aura Project for PHP
eek!  My apologies - not sure what the hell happened.  Looks like I ended up using the wrong tab and assumed I was still on Aura/Filter!

Let me sort my sh*t out

Sorry once again
To unsubscribe from this group and stop receiving emails from it, send an email to auraphp+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages