0

I have a Laravel 5.2 Backend API and a AngularJS Frontend and at some point I perform a laravel validation and return and error if validation fails.

When I iterate trough errors and display them on the frontend I get somthing like this:

["The email has already been taken."]

and I would like to be like this

The email has already been taken.

without the [""] stuff.

My code is:

Angular controller:

if (error.statusText === "Unprocessable Entity") {

          $scope.registerErrors = error.data;
}

Angular template:

<div class="alert alert-warning animated pulse" ng-if="registerError && isLoading === false">
<p ng-repeat="(error, errorText) in registerErrors">{{errorText}}</p>
</div>

Laravel controller:

$this->validate($request, [
          'firstname' => 'required|max:100|min:3',
          'lastname' => 'required|max:100|min:3',
          'email' => 'required|email|unique:users|max:255',
]);

Console.log:

enter image description here

Thanks in advance!

2
  • use a filter that replaces /[\[\]\'\"]/g with ''? Commented May 4, 2016 at 18:36
  • I will try to make a custom filter ... thanks Commented May 4, 2016 at 18:39

3 Answers 3

1

because of errorText contain email array and you shown full email array. if email contain multiple error then can try like:

<p ng-repeat="(error, errorObject) in registerErrors">
  <span ng-repeat=" singleError in errorObject">{{singleError}}</span>
</p>

or for single error can try like:

<p ng-repeat="(error, errorText) in registerErrors">{{errorText[0]}}</p>

or just assign error.data.email instead of error.data

if (error.statusText === "Unprocessable Entity") {
     $scope.registerErrors = error.data.email;
}
Sign up to request clarification or add additional context in comments.

2 Comments

use span tag in p tag should be work updated my answer @vitaminasweb
It's working! I have some doubts on what will happens if error.data will contain and array of arrays ... I tried to simulate several validation fails on the backend and it seams that laravel return the first encountered error when using $this->validate($request ... It's ok for me. Thanks to everyone!
1

Actually you need change $scope.registerErrors = error.data; into $scope.registerErrors = error.data.email;. Because error.data.email is an array.

If you have several errors at the same time, it's better to try this

if (error.statusText === "Unprocessable Entity") {
      $scope.registerErrors=[];
      if(error.data.email)$scope.registerErrors.push(error.data.email[0]);
      if(error.data.firstname)$scope.registerErrors.push(error.data.firstname[0]);
      if(error.data.lastname)$scope.registerErrors.push(error.data.lastname[0]);
}

5 Comments

The error.data will not always contain just email, could contain other several errors in the same time ... for that reason I use ng-repeat
You can add the cases of firstname and lastname, I've updated the answer
If you have several errors at the same time, you can see the updated answer.
Thanks, hope it could be helpful
Yes it is. But I already marked as correct other answer. Your solution seams very god too. I sew it late . :( ... Thanks again!
0

Working fiddle: https://jsfiddle.net/udr9dksa/

{{emailTaken | cleanError}}

Filter:

app.filter("cleanError", function() {
    return function(input) {
    if(!input) return "";

    return input.slice(2, -2);
  }
});

1 Comment

For some reason it's not work well ... I get as response only [ ]

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.