Problem To View Data Json View Blade

Asked

Viewed 236 times

0

Hello! I’ve tried several times and I’m having difficulty visualizing the data of a Json in a View.

I’m passing this data like this in Controller:

$validatorES = new EntradaSaidaFormRequest();
if(!$validatorES->validar($request)){
   $errors = $validatorES->messages();    
   return $this->index()->with(compact('errors'));
}

Na View:

<div class="container col-sm-9">
    @if (!$errors->isEmpty())
    <div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        <ul>            
            @foreach ($errors as $error)
            {{ $error }}
            @endforeach 
        </ul>
    </div>
    @endif
</div>

My $errors variable is like this:

HTTP/1.0 400 Bad Request Cache-Control: no-cache, private Content-Type: application/json Date: Tue, 14 Jan 2020 13:53:30 GMT {"errors":{"descricao":["The descricao field is required."],"registro":["The registro field is required."]}}

But, I cannot print this variable in the View with Foreach above. I am getting the following error as return:

htmlspecialchars() expects parameter 1 to be string, array given 

2 answers

1

Kenia, I believe that your $errors variable is not a string. It is actually an array of array’s containing the errors of each input. Hence the error as {{}} represents an echo in an array, in your case. You can give a var_dump($errors) in your view to understand better. I see you’re using the Formrequest feature. Only at the simplification level, by using the Formrequest resource, the $validatorES->Validated() method would already take care of returning an array with its validated data (according to the validation of the Input file). Or, in case of an error in the validation, it already takes care of redirecting by providing the variable $errors (which is an object). Example:

$validatorES = new EntradaSaidaFormRequest();
//abaixo, o método já cuida de redirecionar ou devolver um array com dados aprovados pela validação do arquivo FormRequest criado
$validators = $validatorES->validated();

In the view, as $errors will be an object, errors are accessed like this :

@if($errors->has('name_do_input'))
    $errors->first('name_do_input')
@endif

Or to access all errors relating to a single input

@if($errors->has('name_do_input'))
    @foreach($errors->get('name_do_input') as $error)
        {{$error}}
    @endforeach
@endif

Follow the documentation for details : https://laravel.com/docs/6.x/validation

  • Your solution is very valid but, I followed a different path because I did not do it the way I should. I used a single Controller to make Insert in two different tables which made things a little difficult (lesson for the next implementations).

0


You solved it for me this way:

<div class="container col-sm-9">
@if (!$errors->isEmpty())
<div class="alert alert-danger">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
    <ul>
        {!! str_replace('"errors":', ' ', $errors->getContent()) !!}
    </ul>
</div>
@endif

Since it is a red area for displaying form fill errors, I will display the contents of the variable as follows: $errors->getContent(), without displaying the other information being loaded.

I also used str_replace to remove the word errors. The variable was printed on the screen like this:

{ {"descricao":["The descricao field is required."],"registro":["The registro field is required."]}}

It’s not the solution I wanted but, answer me this way. I’ll customize the error messages later.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.