Laravel验证以及路由

时间:2015-12-10 10:54:51

标签: javascript php laravel-5

我是Laravel的新手 - 我使用验证器来验证我的表格有点困惑。任何人都可以帮我这个吗?

当我使用验证器时,它不显示错误,而是显示:Route not defined error.

Controller.blade

public function showAccountpost(Request $request)
    {
         $id                 = Input::get('id');
        //$request->merge(array_map('trim', $request->all()));
         $messsages = array(
       // 'email.required'=>'You cant leave Email field empty',
        'firstname.required'=>'You cant leave name field empty ',
        'firstname.max'=>'The field has to be 20:max chars long',
        );

            $rules = ['firstname' => 'required|max:20'];
            $validator = Validator::make(Input::only('firstname'),$rules,$messsages);
            if( $validator->fails() )
            { echo "failed";
                //return Redirect::to('/Usermanagement/usermanagementpost')->with_input()->with_errors($validator);
                //return redirect('usermanagementpost')->withErrors($validator)->withInput();
                //return Redirect::back()->withErrors($validator);
                return redirect()->route('Usermanagement.usermanagementpost/{id}')->withErrors($validator)->withInput();
            }
             else{ 

                try{
                   $model            = UserManagement::findorfail($id);
                   $model->firstname = $request->firstname;  
                   $model->lastname  = $request->lastname; 
                   $model->cardno    = $request->cardno; 
                   $model->phone     = $request->phone; 
                   $model->email     = $request->email; 
                   $model->address   = $request->address; 
                   $model->country_id= $request->country; 
                   $model->city_id   = $request->city; 
                   $model->password  = Hash::make($request->phone);
                   $model->role      = $request->role; 
                   $model->status    = $request->status; 
                   $model->save(); 

                    Session::flash('success', 'Settings updated successfully');
                   exit();
                }
                catch(ModelNotFoundException $e) {
                    exit();
                }
             }
        }

Route.php

Route::get('/usermanagementpost/{id}', ['middleware' => 'auth',
   'as' => 'usermanagementpost', 'uses' => 'UserController@showAccount'
]);
Route::post('/userposteditajax', ['middleware' => 'auth',
   'as' => 'userposteditajax', 'uses' => 'UserController@showAccountpost'
]);

我的观点 Page-usermanagementpost.blade

  <div class="modal fade bs-modal-lg in" style="display:block;">
       <div class="modal-dialog modal-lg">
          <div class="modal-content" style="background:#fff;">
             <div class="modal-header">
                <button type="button" class="close" onclick="$.magnificPopup.close()" data-dismiss="modal" aria-hidden="true"></button>
                <h4 class="modal-title">Edit User Details</h4>
             </div>
             <div class="modal-body" >
                <div class="alert alert-success" id="ajax_success" style="display:none">
                   <strong>Success!</strong>&nbsp;User Added Successfully
                </div>
                <div class="alert alert-danger" id="ajax_error" style="display:none">
                   <strong>Error!</strong>&nbsp;<span id="ajax_error_mess"></span>
                </div>
                {!! Form::open( array('route' => 'userposteditajax', 'method' => 'post', 'id' => 'ajax_user')) !!}
                <div class="form-body">

                    <div class="form-group">
                        <div class="col-sm col-sm-12">
                            <div class="col-sm col-sm-6">
                                <label>FirstName</label>                                    
                                {!! Form::text('firstname',$users->firstname, array('class' => 'form-control','id' => 'firstname')) !!}
                            </div>
                            <div class="col-sm col-sm-6">
                                <label>LastName</label>
                                {!! Form::text('lastname', $users->lastname, array('class' => 'form-control','id' => 'ajax_lname')) !!}
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm col-sm-12">
                            <div class="col-sm col-sm-6">
                                <label>Phone</label>
                                <div class="input-group">
                                <span class="input-group-addon">
                                    <i class="fa fa-phone"></i>
                                </span>
                                 {!! Form::text('phone', $users->phone, array('class' => 'form-control','id' => 'ajax_phone')) !!}
                                </div>
                            </div>
                            <div class="col-sm col-sm-6">
                                <label>Email Address</label>
                                <div class="input-group">
                                <span class="input-group-addon">
                                    <i class="fa fa-envelope"></i>
                                </span>
                                 {!! Form::text('email', $users->email, array('class' => 'form-control','id' => 'ajax_email')) !!}
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm col-sm-12">
                            <div class="col-sm col-sm-6">
                                <label>Card No</label>
                                {!! Form::text('cardno', $users->cardno, array('class' => 'form-control','id' => 'ajax_cardno')) !!}
                            </div>
                            <div class="col-sm col-sm-6">
                                <label>Address</label>
                                {!! Form::textarea('address',$users->address, array('size'=>'50x3','class' => 'form-control','id' => 'ajax_address')) !!}
                        </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm col-sm-12">
                            <div class="col-sm col-sm-6">
                                <label>Country</label>
                                {!! Form::select('country',$country, $users->country_id, array('class' => 'chosen form-control','id' => 'ajax_country','data-placeholder' => 'Country','onchange'=>'select_bl(this.value)')) !!}
                            </div>
                            <div class="col-sm col-sm-6">
                                <label>City</label>
                                {!! Form::select('city',$city, $users->city_id, array('class' => 'chosen form-control','id' => 'ajax_city','data-placeholder' => 'City')) !!}
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm col-sm-12">
                            <div class="col-sm col-sm-6">
                                <label>Role</label>
                                <div class="input-group">
                                {!! Form::select('role',['2'=>'Administrator','3'=>'Standard User'] ,$users->role, array('class' => 'form-control','id' => 'ajax_role')) !!}
                                </div>
                            </div>
                            <div class="col-sm col-sm-6">
                                <label>Status</label>
                                <div class="input-group">
                                {!! Form::select('status',['1'=>'Active','0'=>'Inactive'] , $users->status, array('class' => 'form-control','id' => 'ajax_status')) !!}
                                </div>
                            </div>
                        </div>
                    </div>

             </div>
             <div class="form-actions">
               <button type="submit" id="ajax_submit" class="btn blue-hoki">Save</button>
               <button type="button" onclick="$.magnificPopup.close()" class="btn default red" data-dismiss="modal">Cancel</button>
             </div>
             {!! Form::hidden('id',$users->id,array('id'=>'id'))!!}
              {!! Form::close()!!}
          </div>



        </div>
    </div>
    <script>

    function select_bl(a)
    {
        if( parseInt(a) > 0 ) 
        {

            Metronic.blockUI( { boxed: true,
            message: 'Processing...'});

            $.ajax({

                    url: 'addcitypost',
                    type: 'post',
                    data: {'id':a},
                    dataType:'json',
                    success: function(data)
                    {

                        $('#ajax_city').html(data.city_drop);
                        $(".chosen").trigger("chosen:updated");
                        Metronic.unblockUI();

                    }

                    });         
        }
        else
        {

            $('#ajax_city').html('<option value="">No City To Display</option>');
            $(".chosen").trigger("chosen:updated");
            //sort_ticket();

            }
    }
    </script>
    <script>
    $('#ajax_user').on( 'submit', function() 
       {
        $('#ajax_success').hide();
        $('#ajax_error').hide();
        $('#ajax_submit').prop('disabled', true);
        var firstname  = $('#firstname').val();
        var lastname   = $('#ajax_lname').val();
        var cardno     = $('#ajax_cardno').val();
        var phone      = $('#ajax_phone').val();
        var email      = $('#ajax_email').val();
        var address    = $('#ajax_address').val();
        var country    = $('#ajax_country').val();
        var city       = $('#ajax_city').val();
        var password   = $('#ajax_phone').val();
        var role       = $('#ajax_role').val();
        var status     = $('#ajax_status').val();
        var id         = $('#id').val();        

        $.ajax({
              url: '{{ url("userposteditajax") }}',
              dataType: 'json',
              type: 'post',
              data: {firstname:firstname,lastname:lastname,cardno:cardno,phone:phone,email:email,address:address,country:country,city:city,password:password,role:role,status:status,id:id},
              success: function( data, textStatus, jQxhr ){

                  $.magnificPopup.close();
                  location.reload();

                  $('#ajax_submit').prop('disabled', false);

                    location.reload();
                    $.magnificPopup.close();

                },
                error: function( jqXhr, textStatus, errorThrown ){
                    console.log(jqXhr);
                    $('#ajax_submit').prop('disabled', false);
                    var errors = jqXhr.responseJSON;
                    $.each( errors, function( key, value ) {
                        $('#ajax_error').fadeIn('slow',function(){
                            $('#ajax_error_mess').html(value);
                        });
                    });
                }
           });
        return false;
       });
    if(jQuery('.chosen').length > 0 )
       {
        jQuery(".chosen").chosen({no_results_text: "nothing found"}); 
       }
     </script>

当我运行它时,它会显示如下错误:

**500 Internal Server Error**
        771ms   
jquery.min.js (line 4)
Object { readyState=4,  responseText="failed<!DOCTYPE html>\n<h...iv>\n    </body>\n</html>",  status=500,  more...}

1 个答案:

答案 0 :(得分:0)

这意味着你没有为

写路线
userposteditajax

警告:

除非您没有子页面,否则不应直接使用网址段。

即。如果你有你的网址yourapp.com/profile/id

那么你的ajax调用就像

一样
yourapp.com/profile/userposteditajax

所以你应该在ajax请求中提供完整的url。即,yourapp.com/userposteditajax

提示:

您可以在某个地方定义父网址,只需在ajax请求中连接

如果您有书面路线,请检查您的网络标签,