Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validating indexed subarrays? #759

Closed
codmpm opened this issue Nov 10, 2016 · 3 comments
Closed

Validating indexed subarrays? #759

codmpm opened this issue Nov 10, 2016 · 3 comments

Comments

@codmpm
Copy link

codmpm commented Nov 10, 2016

Hi guys,

Sorry for crossposting, but I think this is a more appropriate place...
http://stackoverflow.com/questions/40511540/validating-sub-arrays-with-respect-validation

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

@codmpm codmpm changed the title Validatin subarrays in post data? Validating subarrays in post data? Nov 10, 2016
@codmpm codmpm changed the title Validating subarrays in post data? Validating indexed subarrays? Nov 10, 2016
@alganet
Copy link
Member

alganet commented Nov 10, 2016

Maybe the Each validator works best for this case. Something like ->key('options', v::optional(v::arrayType()->each(v::intVal()))).

@codmpm
Copy link
Author

codmpm commented Nov 10, 2016

You sir, are my hero.
This is far too simple if I had not overread each()...

Cheers!

@torian257x
Copy link

btw how to request at least say, 3 check boxes to be checked? e.g. at least 3 values in the array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants