2

I've used Respect/Validation successfully for my general concern.

But now I'm validating some form Input where the user can check multiple checkboxes and the data is send with an array. The form looks something like this:

<form method="post" action="">
    <input type="text" name="firstname">
    <input type="text" name="lastname">

    <input type="checkbox" name="options[]" value="1">
    <input type="checkbox" name="options[]" value="2">
    <input type="checkbox" name="options[]" value="3">

    <button type="submit">Send</button>
</form>

So, my post-data will look like this:

Array
(
    [firstname] => Peter
    [lastname] => Parker
    [options] => Array
        (
            [0] => 1
            [1] => 3
        )
)

I've build a validation rule which works:

<?php
//used in class, so "use Respect\Validation\Validator AS v;"

$validReq = v::create()
    ->key('firstname', v::stringType()->length(1, 32))
    ->key('lastname', v::stringType()->length(1, 32))
    ->key('options', v::optional(v::arrayType()))
    ->setName('valid request');

My question now is, how do I validate the array options with (e.g.) v::intVal()?

Maybe I've just oversaw how to accomplish this. Thank you for your time.

Cheers, Patrik

pmayer
  • 341
  • 2
  • 13

1 Answers1

2

Solved with the help of alganet over at github.

This could be accomplished using each():

<?php    
$validReq = v::create()
    ->key('firstname', v::stringType()->length(1, 32))
    ->key('lastname', v::stringType()->length(1, 32))
    ->key('options', v::optional(v::arrayType()->each(v::intVal())))
    ->setName('valid request');

Cheers, Patrik

pmayer
  • 341
  • 2
  • 13