10

I want to convert laravel validation error array to a comma separated string. This is to use in an api service for an ios application. So that the iOs developer can process error messages easily.

I tried,

    $valArr = [];
    foreach ($validator->errors() as $key => $value) { 
        $errStr = $key.' '.$value[0];
        array_push($valArr, $errStr);
    }
    if(!empty($valArr)){
        $errStrFinal = implode(',', $valArr);
    }

But it is not working.

0

4 Answers 4

15

You should do like this :

$errorString = implode(",",$validator->messages()->all());

P.S. Assuming

$validator = Validator::make($dataToBeChecked,$validationArray,$messageArray)
Sign up to request clarification or add additional context in comments.

1 Comment

also for other MessageBags like $validator->errors()->all()
5
You are not converting validation errors to array.Please use the below function and pass validation errors as parameter.

 public function validationErrorsToString($errArray) {
        $valArr = array();
        foreach ($errArray->toArray() as $key => $value) { 
            $errStr = $key.' '.$value[0];
            array_push($valArr, $errStr);
        }
        if(!empty($valArr)){
            $errStrFinal = implode(',', $valArr);
        }
        return $errStrFinal;
    }
//Function call.
$result = $this->validationErrorsToString($validator->errors());

Comments

5

The $validator->errors() returns a MessageBag,

see: https://laravel.com/api/5.3/Illuminate/Support/MessageBag.html.

You are close, you need to call the getMessages() function on errors(), so:

foreach ($validator->errors()->getMessages() as $key => $value) {

Hope this helps :)

Comments

1

If you are doing it like me without your validator and you are pulling messages from the exception you can use laravel helper Arr::flatten($array);

Link and code are for laravel 8.x but I tested this with 5.7 ;) It works.

From documentation:

use Illuminate\Support\Arr;

$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];

$flattened = Arr::flatten($array);

// ['Joe', 'PHP', 'Ruby']

My code:

try {
  $request->validate([
    'test1' => 'required|integer',
    'test2' => 'required|integer',
    'test3' => 'required|string',
  ]);
} catch (ValidationException $validationException) {
  return response()->json([
    'type' => 'error',
    'title' => $validationException->getMessage(),
    'messages' => Arr::flatten($validationException->errors())
  ], $validationException->status);
} catch (\Exception $exception) {
  return response()->json([
    'type' => 'error',
    'title' => $exception->getMessage(),
  ], $exception->getCode());
}

As you can see I am pulling the message and setting it as my title. Then I am using Arr::flatten($validationException->errors()) to get the validation messages and but to flatten my array for SweetAlert2 on the frontend.

I know I am late but I hope it will help someone that comes across these problems.

Greetings! :)

1 Comment

I needed just the string of all errors, so this it is: $errMsg = implode(" ", Arr::flatten($e->errors()));

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.