avatar
validate request PHP

In many case, determine what fields want to validate.

$credentials = $request->only('email', 'password', 'confirm_password');

Establish validations for each particular field following the requirement.

$rules = [
    'email' => 'regex:/^([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9_\-.]+)\.([a-zA-Z]{2,5})$/|unique:users',
    'password' => 'required',
    'confirm_password' => 'required',
];

Adjust message in each particular condition.

$customMessages = [
    'email.regex' => 'The mail address is not properly formatted.',
    'email.unique' => 'This email address is already being used.',
    'password.required' => 'The password cannot be left blank.',
    'confirm_password.required' => 'The confirm password cannot be left blank.'
];

For instance, will return structure JSON below.

$validator = Validator::make($credentials, $rules, $customMessages);
if ($validator->fails()) {
    return json_encode(([
        'message' => [
            'status' => "error",
            'description' => $validator->errors()->first()
        ],
    ]));
} else {...}
24
create Laravel project with composer and specific version check version laravel laravel telescope
You need to login to do this manipulation!