1

I'm trying to display the validation errors thrown by the Codeigniter validator function. Codeigniter validator function sends <p></p> with each and every error as default.

The JSON output back to the page displays this HTML as it is, instead of displaying it in HTML paragraph one below another.

The PHP Codeigniter code is as follows (here ouptut is an array containing formvalidation errors)

//Send output back to NG
$output['error'] = validation_errors(); 
$json = json_encode($output, JSON_HEX_QUOT | JSON_HEX_TAG);
echo $json;

The AngularJS code is as follows

var request = $http({
        method: "post",
        url: "/user/processregister",
        data: $scope.regdata,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    });

    request.then(       

        function (data) {

            if(data.data.state == 1) {
                $scope.alertMessage = "Registration Successful. Please wait while you are being redirected.";
                $window.location.href = '/member';
            } else {                            
                $scope.alertMessage = data.data.error;
            }

        },

        function(data) {
            // Handle error here
            $scope.alertMessage = 'Error in form submission. Please contact support.';
        }

    );

Error displayed in RAW text

1 Answer 1

4

Here is what are you looking for $this->form_validation->error_array(); now you have an array with all the validation errors, you can now loop through that array and do whatever you want.

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

2 Comments

Is there a way to display these errors one below another without having to create separate element pointer?
Not sure what you mean but you can loop through the array and put each element in .alert div or something then append it to your view or a better way that i do and prefer put each element in .invalid-feedback under each input and append the class .is-invalid to that input (all of that supposingly that you are using bootstrap, but you got the idea anyways)

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.