Skip to content

How can we access the value inside the callback of Rule::when(), when the field is an array. #38865

@itxshakil

Description

@itxshakil
  • Laravel Version: 8..55.0
  • PHP Version: 7.4.9
  • Database Driver & Version:

Description:

How can we access the value inside the callback of Rule::when(), when the field is an array.

  • The problem is we don't know the iteration(key), so we can't access it from request.

I guess we may pass the current value as the second parameter to the callback.

$validator = Validator::make($request->all(), [
'openings.*.from' => ['required_with:openings.*.id', 'date_format:H:i'],
'openings.*.to' => ['required_with:openings.*.id', Rule::when(function($a){
// Need the current value for conditional check
},['date_format:H:i']), 'after:openings.*.from'],
]);

If we wanted to add validation(date_format:H:i) on the basis of the current value of openings's to.

But I can not access the value of current openings to.

Originally posted by @itxshakil in #38361 (comment)

Steps To Reproduce:

  • Add an array field for validation (For eg. : request()->validate([ 'abc.*' => ['required'] ]);
  • Try to add conditional validation on the basis of the value of field.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions