mardi 26 janvier 2016

Form validation error in ajax

Hi i have some problem when i using Formvalidation library intergrated with laravel 5 . Here it's the link

So if i dont use validation i can alert success message after i post comment,if i use the validation i just get the json {"mess":"Success"}

//validation
    function commentForm(){
        $("#commentForm").formValidation({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                allowance: {
                    validators: {
                        notEmpty: {
                            message: "Vui lòng nhập kinh phí"
                        },
                        hex:{
                            message: "Kinh phí phải là số"
                        }
                    }
                },
                completed_day: {
                    validators: {
                        notEmpty: {
                            message: 'Vui lòng nhập ngày hoàn thành'
                        },
                        hex:{
                            message: "Ngày hoàn thành phải là số"
                        }
                    }
                },
                introduce: {
                    validators: {
                        notEmpty: {
                            message: 'Vui lòng nhập giới thiệu bản thân'
                        }
                    }
                },
            }
        });
    }


$(document).ready(function() {
    if(commentForm()){
    $('#btnInsertComment').click(function(event) {
        event.preventDefault();
        var data=$("#commentForm").serialize();
        alert(data);
        $.ajax({
            url: '/postComment',
            type: 'POST',
            data: data,
            success:function(data) {
                alert(data.mess);
                window.location.reload();
                $("#commentForm")[0].reset();
            },
            error:function(data) {
                alert(data.err);
            }
        });
    })
    }
});

Here it's controller i'm using json to return success message

public function newComment(Request $request)
    {
        try{
            $date_format = date('Y-m-d');
            $comment=new Comment();
            $comment->user_id=Auth::user()->id;
            $comment->introduce=$request->introduce;
            $comment->completed_day=$request->completed_day;
            $comment->allowance=str_replace( ',', '', $request->allowance);
            $comment->post_at=$date_format;
            $comment->job_id=$request->job_id;
            $comment->save();
                return response()->json(array('mess'=>'Success'));
            }
        catch(Exception $ex){
                    return response()->json(array('err'=>'Error'));
            }

        }

So any suggest about this.Thanks for help



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire