1

I am working on teaching myself Angular at the moment, and I am currently getting to grips with sending http requests from my angular front-end.

I am successfully sending a POST request, and I am also getting the correct error response back. The error response returned is from Laravel and basically a JSON response.

return Response::json(['message' => $validator->errors()], 422);

This returns something that looks like,

   {
       "message": {
           "first_name": ["The first name field is required."],
           "surname": ["The surname field is required."],
           "email": ["The email field is required."],
           "school_name": ["The school name field is required."]
       }
   }

In my angular template I have this in my controller for now,

app.controller('signupController', function($scope, $http) {
    console.log("...signup controller initialised...");
    $scope.formData = {};
    $scope.error = "";
    $scope.processForm = function() {
        console.log($scope.formData);
        var postForm = $http.post('http://homestead.app/api/invites/create', $scope.formData);
        postForm.success(function(data, status, headers, config) {
            $scope.message = data;
        });
        postForm.error(function(data, status, headers) {
            $scope.error = data;
            console.log($scope.error);
        });
    }
});

In my template I form entries that looks like this,

<div class="form-group">
    <label for="firstName" class="is-required">First Name</label>
    <input type="text" class="form-control" id="firstName" name="first_name" ng-model="formData.firstname" ng-class="{'has-error': error.message.first_name}">
    <div class="error__block">{{ error.message.first_name }}</div>
</div>

Now on error the form input get the has-error class assigned, but the error block shows something like this,

["The first name field is required."]

When I would have thought it would show something like this,

The first name field is required

Am I doing something incorrectly on my laravel end of my angular end?

2
  • If your function returns an array, that's how arrays get encoded in JSON. If you want just the string, maybe reference it with [0] to take the first element? Be aware this design might be intentional to accommodate multiple errors. Commented Oct 31, 2016 at 22:00
  • I am being so stupid (long day at work). My form inputs can have multiple validations errors returned with hence the arrays! Commented Oct 31, 2016 at 22:03

2 Answers 2

2

from what I see you're returning from laravel an array of string values for the field first_name . That doesn't really sound okay, shouldn't it be simply a string?

If not, join the first_name array and you'll get the whole name from multiple values in array.

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

Comments

1

I don't know anything about Laravel so there may still be a way to change that, but you can make a change in your $http.success() function like this:

postForm.success(function(data, status, headers, config) {
    $scope.message = data[0];
});

Instead of setting $scope.message to the incoming array, it will grab the first element of the array and use that instead (index 0).

Comments

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.