0

I have this code, the $vatValidator validates the vat number. The $validator validates other request data.

For example if the user dont insert a value for the name field it will appear a validation error:

{success: false,…} errors: {name: ["Fill all fields."]} success : false

But if the user insert a invalid value for the vat no validation message appears because in the response the errors are empty:

{success: false, errors: []} errors :[]

Do you know how to show a message when the vat number is incorrect? A message like "Invalid vat".

$vatValidator = VatValidator::validateFormat($request->country . $request->vat);
$validator = Validator::make($request->all(), $rules, $messages);

$errors = $validator->errors();
$errors = json_decode($errors);

if ($validator->fails() || $vatValidator == false) {
    return response()->json([
        'success' => false,
        'errors' => $errors
    ], 422);
}

Full method:

 public function storeInfo(Request $request, $id, $slug = null, Validator $validator){
        ...

        $rules = [];
        $messages = [];

        if ($all) {
            $rules["name.*"] = 'required|max:255|string';
            $rules["surname.*"] = 'required|max:255|string';
        }

        $vatValidator = VatValidator::validateFormat($request->country . $request->vat);
        $validator = Validator::make($request->all(), $rules, $messages);


        $errors = $validator->errors();
        $errors = json_decode($errors);

        if ($validator->fails() || $vatValidator == false) {
            return response()->json([
                'success' => false,
                'errors' => $errors
            ], 422);
        }

        if ($validator->passes()) {

        }
        return response()->json([
            'success' => true,
            'message' => 'success'
        ], 200);

    }
5
  • With " $vatErrors = $vatValidator->errors(); dd($vatErrors);" it appears ""Call to a member function errors() on boolean". Commented Jun 2, 2018 at 11:55
  • Is VatValidator a class that you've created or from a package you're pulling in? If it's one you've created please may you show the contents of it, otherwise can you add a link to the package? Commented Jun 2, 2018 at 12:33
  • Is from a package, the package is this "packagist.org/packages/dannyvankooten/laravel-vat". Commented Jun 2, 2018 at 12:33
  • What version of Laravel are you using? Commented Jun 2, 2018 at 12:36
  • The version 5.5. Commented Jun 2, 2018 at 12:37

2 Answers 2

0

You could use a closure for the validation:

$rules['vat'] = [
    function ($attr, $value, $fail) use ($request) {

        if (!VatValidator::validateFormat($request->country . $request->vat)) {
            $fail('Invalid vat.');
        }

    }
];
Sign up to request clarification or add additional context in comments.

Comments

0
$errors = $validator->messages();
$errors->merge($vatValidator->messages())

variable $error contains all error messages

2 Comments

Thanks but it appears with " $errors->merge($vatValidator->messages()); " this ""Call to a member function messages() on boolean" ".
The vat validation is using a package " dannyvankooten/laravel-vat".

Your Answer

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