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

Better docmentation / example for unnamed values / items #18

Open
th-lange opened this issue Jun 18, 2020 · 3 comments
Open

Better docmentation / example for unnamed values / items #18

th-lange opened this issue Jun 18, 2020 · 3 comments

Comments

@th-lange
Copy link
Collaborator

Requirement:

I want to validate an array of objects, like:

[
    {
        "foo": "foo1",
        "bar": "bar1",
        "baz": "baz1"
    },
    {
        "foo": "foo2",
        "bar": "bar2",
        "baz": "baz2"
    },
    {
        "foo": "foo3",
        "bar": "bar3",
        "baz": "baz3"
    }
]

So I tried this:

        $validator = new Validator();
        $rules = RulesBuilder::create()
            ->rule('collection', RulesBuilder::create()
                ->field('foo', 'required')
                ->field('bar', 'required')
                ->field('baz', 'required')
                ->build()
            )->build();

But I get the error:
array_shift() expects parameter 1 to be array, string given
vendor/philiplb/valdi/src/Valdi/Validator.php:131

It would be nice, to get an example on how to validate "unnamed" collections.

@philiplb
Copy link
Owner

Hi,

is this covered by the A More Complex Example? There is a field "postings" which is an array of objects.

@th-lange
Copy link
Collaborator Author

th-lange commented Jun 18, 2020

I got it now:
Using a collection also requires a "nested" element. But still no luck...

        $validator = new Validator();
        $rules = RulesBuilder::create()
            ->rule('collection', $validator, RulesBuilder::create()
                ->rule('nested', $validator, RulesBuilder::create()
                    ->field('foo', 'required')
                    ->field('bar', 'required')
                    ->field('baz', 'required')
                    ->build()
                )
                ->build()
            )
            ->build();

@th-lange
Copy link
Collaborator Author

th-lange commented Jun 18, 2020

I can work with named elements... Yeah. But it will just not work with "unnamed" sequences for me.
The above example works, when i use:

        $rules = RulesBuilder::create()
            ->field('namedItem', 'collection', $validator, RulesBuilder::create()
                ->rule('nested', $validator, RulesBuilder::create()
                     ...

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

2 participants