0

in laravel controller I am sending the validation error to a ajax request by a code like that

$validator = Validator::make(Input::all(), $rules);


        if ($validator->fails())
        {

            $data = array('errors' =>  $validator->errors()->toArray());                
                    return Response::json($data);

        }

Now in receive by ajax I want it (the validation error) to impress the text area and error display div. For that I am doing this .

    bla bla blaala

success:function(data, textStatus, xhr, error){

                         if(data == "OK") 
                         {
                            $('div.result').html('You have successfully posted ! Please refresh to see your post <hr/>');
                            //window.location.reload(true);

                         }
                         else
                         {
                          //  $('div.arr').data(data.errors);  

                            //alert("---"+data);
                             $('div.result').html('Your post can not be possible ! Please check the text bOx ! <hr/> ');
                             $('div.arr.errors').val(data.errors);
                             $("#ask").attr("disabled", false)



                        }

I want to get the effect on this

    <div class="arr control-group{{ $errors->first('about', ' has-error') }}">

but only the

<div class="result" style="color:red;">


</div>

is working during the error.

how does the errors can be collected and shown in the view ?

thank you in advance \m/

1 Answer 1

1

Just dumping a javascript array/object into a div won't work. You pretty much have two choices to make it work. You can iterate through the array with PHP and create your HTML there and send the HTML as a string to Javascript and you'd just set the innerHTML of your div to the string. Or you can keep it the same way you have it now and use Javascript and jQuery to iterate through the object and fill out the div on the client side.

Doing it on the client side

jQuery.each(data.errors, function(i, val)
{
    error = document.createElement('SPAN');
    error.innerHTML = val;
    error.className = 'error'; // Can set the css class of the element
    error.style.backgroundColor = '#F00';  // Can set other css stuff as well =)
    $('div.arr.errors').append(error);
});

Doing it on the server side (still requires some client side work)

// PHP Stuff
$html = "<div class='errorClass'>";
foreach($validator->errors()->toArray() as $error)
{
    $html .= "<span>" . $error . "</span>";
}
$html .= "</div>";
return Response::json(array('html' => $html));

// Javascript Stuff
$('div.arr.errors').html(data.html);

Note: I'm not that great with jQuery but I don't believe $('div.arr.errors') will work with what you have. I'd make it less confusing on myself and just give it an ID of errorArray or something and just use $('#errorArray')

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

1 Comment

Thanks for the reply bro ! I will try to implement it. And let you know if there is any error. Pls join my site click here:) thanks !

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.