0

A newbie here in terms of Laravel validation - I've searched and checked the manual but can't seem to find an answer to this. I'm using Laravel 8.0.

As per the Laravel manual I have created a manual validation to validate my array, it contains data similar to the below:

$row = array('John','Doe','[email protected]','Manager');

Because the array has no keys, I can't work out how to reference the array items using their index when creating the Validator, I've tried this:

$validatedRow = Validator::make($row, ['0' => 'required|max:2', '1' => 'required']);
$validatedRow = Validator::make($row, [0 => 'required|max:2', 1 => 'required']);
$validatedRow = Validator::make($row, [$row[0] => 'required|max:2', $row[0] => 'required']);

But no luck - does anyone have any suggestions?

1
  • 1
    I guess this cannot be possible. Laravel validator creates validation messages automatically based on some basic properties like :attribute (key names). So without giving the exact key name some of the features won't work and would make some bugs and errors. I recommend adding some required keys to the array(maybe using a method or service for this?) or store them with keys at first place. Commented Aug 30, 2021 at 11:26

1 Answer 1

1

Use Validator

use Illuminate\Support\Facades\Validator;

Then Update your code

        $row = array('John','Doe','[email protected]','Manager');
        $rule = [
            '0' => 'required|string',
            '1' => 'required',
            '2' => 'required|email',
            '3' => 'required'
        ];

        $validator = Validator::make($row, $rule);

        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()->all()], 422);
        }else{
            echo 'ok';
        }
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.