<?php

namespace Ire\Base\Requests;

use Illuminate\Foundation\Http\FormRequest;

use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Contracts\Validation\Validator;

class IreRequest extends FormRequest
{

    /**
     * Return validation errors as json response
     *
     * @param Validator $validator
     */
    protected function failedValidation(Validator $validator)
    {
        $response = [
            'success' => false,
            'message' => $this->errorArayToString($validator->errors()),
            'errors' => $validator->errors(),
        ];

        throw new HttpResponseException(response()->json($response, 200));
    }

    public function errorArayToString ($errors) {
        $string_errors = "";
        $collect = collect($errors)->collapse()->toArray();
        foreach ($collect as $key => $value) {
            $string_errors .= $value . "<br>";
        }
        return $string_errors;
    }
}
 