2

In CustomFormRequest i have something like this:

public function rules(): array
{
    return [
        'events' => ['array'],
        'events.*.type' => ['required'],
        'events.*.data' => [app(SomeRule::class)],
    ];
}

SomeRule:

public function passes($attribute, $value): bool
{
    var_dump($attribute);//events.0.data
    var_dump($value);exit;
}

In SomeRule::passes i need to have access to events.X.type (so for events.5.data i need events.5.type). Any ideas?

4
  • Use helper function request() to get your data Commented May 15, 2018 at 10:42
  • hmm... but i need to get index from events.0.data. Is better way to do that than explode? Commented May 15, 2018 at 10:46
  • 2
    maybe review your validation to do 'events.*' => [app(SomeRule::class)], and then $value should have both type and data (maybe, I don't know if it will work) Commented May 15, 2018 at 11:01
  • @apokryfos I think this would be the most clean solution.. Commented May 15, 2018 at 11:03

1 Answer 1

2

As a user commented in my question Validating array in Laravel using custom rule with additional parameter, you can do it with this code, in your case it would be something like

class SomeRule implements Rule
{
    public function passes($attribute, $value)
    {
        $index = explode('.', $attribute)[1];
        $type = request()->input("events.{$index}.type");

        return someConditional ? true : false;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.