lundi 23 novembre 2015

Laravel Validation and Bootstrap Modal

I have used the bootstrap modal to hold the form. Everything went fine (even the save functionality) until i hit the validation of the form. I want to use the laravel built-in validation facilities but do not really know how to show the errors in my modal when my validation fails. Any help please

My json

{"errors": {"title": ["The title field is required."]}}

form

                 <div class="modal fade" id="Portfolio" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                  <div class="modal-dialog">
                    <div class="modal-content">
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                        <h3 class="modal-title" id="myModalLabel">Acrescentar portfolio</h3>
                      </div>
                      <div class="modal-body">
                        {!! Form::open(array('url' => 'backend/portfolio', 'id' => 'form', 'name' => 'InserirPortfolio', 'role' => 'form', 'files'=> true))!!}

                        <input type="hidden" name="id" value="{{Auth::user()->id}}">

                        <div class="alert alert-info info" style="display: none;">
                            <ul></ul>
                        </div>

                        <div class="row" style="margin-bottom: 20px;">
                            <div class="col-md-3 col-lg-3">
                                {!! Form::label('titulo', 'Titulo', ['class' => 'label_perfil']) !!}
                            </div>
                            <div class="col-md-9 col-lg-9">
                                {!! Form::text('titulo', '', ['class' => 'form-control input-md' , 'placeholder' => 'Titulo']) !!}
                            </div>
                        </div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                        {!! Form::submit('Acrescentar portfolio', ['class' => 'btn btn-primary']) !!}
                      </div>
                    {!! Form::close() !!}


                    </div>
                  </div>
                </div>
                    {!! Form::close() !!}

JS

                    <script>
                        $(document).ready(function() {
                            $('form').submit(function(e){
                                e.preventDefault();
                                 var titulo = $('#titulo').val();

                                $.ajax({
                                    url: 'backend/portfolio',
                                    type: 'post',
                                    dataType: 'json',
                                    data:{titulo: titulo,
                                    error:function(data){
                                        $(".alert-info").append('titulo');
                                    }
                                });
                            });
                        });
                    </script>   

controller

    public function InserirPortfolio() { 
        $portfolio = Input::except('_token'); 
        $validation = Validator::make($portfolio, Portfolio::$portfolio); 
        if ($validation->passes()) {
            $user = Portfolio::insert($portfolio); 
            Session::flash('portfolio', 'Redes sociais editadas com sucesso'); 
            return Redirect::to('backend/portfolio'); 
        } else {
            return Response::json($validation->errors()->toArray());
        }
    }   



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire