无法从控制器获得验证json响应以在laravel 4中运行?

时间:2014-03-17 13:54:16

标签: json forms laravel controller

这是我的mypostitem.blade.php'查看文件。

 <h3> Post an Item</h3>
 {{ Form::open(array('class' => 'form-horizontal', 'id' => 'postitems')) }}
 <div class="control-group">
   {{ Form::label('name', 'Item Name', array('class' => 'control-label')) }}
   <div class="controls">
     {{ Form::text('name', null, array('id' => 'name')) }}
     {{ $errors->first('name', '<div class = emerror>:message</div>')}}
   </div>
 </div>
 <div class="control-group">
   {{ Form::label('price', 'Item Price', array('class' => 'control-label')) }}
   <div class="controls">
     {{ Form::text('price', null, array('id' => 'price')) }}
     {{ $errors->first('price', '<div class = emerror>:message</div>')}}
   </div>
 </div>
 <div class="control-group">
   {{ Form::label('description', 'Description', array('class' => 'control-label')) }}
   <div class="controls">
     {{ Form::textarea('description', null, array('id' => 'description')) }}
     {{ $errors->first('description', '<div class = emerror>:message</div>')}}
   </div>
 </div>
 <div class="control-group">
   {{ Form::label('city', 'City', array('class' => 'control-label')) }}
   <div class="controls">
     {{ Form::text('city', null, array('id' => 'city')) }}
     {{ $errors->first('city', '<div class = emerror>:message</div>')}}
   </div>
 </div>
 <div class="control-group">
   {{ Form::label('telephone', 'Telephone Number', array('class' => 'control-label')) }}
   <div class="controls">
     {{ Form::text('telephone', null, array('id' => 'telephone')) }}
     {{ $errors->first('telephone', '<div class = emerror>:message</div>')}}
   </div>
 </div>
 <div class="control-group">
   {{ Form::label('address', 'Address', array('class' => 'control-label')) }}
   <div class="controls">
     {{ Form::text('address', null, array('id' => 'address')) }}
     {{ $errors->first('address', '<div class = emerror>:message</div>')}}
   </div>
 </div>
 <div class="control-group">
   <div class="controls">
     {{ Form::submit('Post Item', array('class' => 'btn btn-primary')) }}
   </div>
 </div>
 {{ Form::close() }}

 <div id="results"></div>
 <script>
   $(function() {
     $("#postitems").on("submit", function(e) {
       e.preventDefault();
       var results = '';
       $.post('myprofile/storeitem',
       {
         name:          $("#name").val(), 
         price:         $("#price").val(),
         description:   $("#description").val(),
         city:      $("#city").val(),
         telephone:     $("#telephone").val(),
         address:       $("#address").val()
       }, function(data) {
         $.each(data, function(){
           results += this + '<br>';
         });
         $("#results").html(results);
       });
     });
   });
 </script>

On MyprofileController.php我有..

public function postStoreitem() 
{
  $validation = Validator::make(Input::all(), Product::$rules);
  if($validation->fails()) {
    return Response::json($validation->errors()->toArray());
  }
}

在我的routes.php中,我有......

  Route::controller('myprofile', 'MyprofileController');

我面临的问题是,提交表单时没有显示任何内容?任何输入都非常感谢。感谢

2 个答案:

答案 0 :(得分:1)

我可以立即看到一个错误:如果验证成功,postStoreitem()将不返回任何内容。即使您希望验证失败,也需要将其用于调试目的:

public function postStoreitem() 
{
  $validation = Validator::make(Input::all(), Product::$rules);
  if($validation->fails()) {
    return Response::json($validation->errors()->toArray());
  } else {   
    return "Validation successful";
  }
}

你的回调功能也不起作用:

function(data) {
  $.each(data, function(){
    results += this + '<br>';
  });
  $("#results").html(results);
});

data是一个字符串(在本例中是一个JSONified数组,它是一个字符串)。您需要在data上使用json_decode,然后才能使用$.each()。首先尝试将data打印为字符串,以确定您是否收到验证错误。

答案 1 :(得分:1)

您可以在Controller

中尝试这样的事情
public function postStoreitem() 
{
    $validation = Validator::make(Input::all(), Product::$rules);
    if($validation->fails()) {
        $result = array('result' => 'fail', 'errors' => $validation->errors()->toArray());
    } else {   
      // Insert into db or whatever then
      $result = array('result' => 'success');
    }
    return Response::json($result);
}

JavaScript处理程序的success中:

function(data) {
    var obj = $.parseJSON(data);
    if(obj.result == 'fail') {
        $.each(obj.errors, function(key, value){
            result += value + '<br />';
        });   
    }
    else {
        results = 'Operation Successful!';
    }
    $("#results").html(results);
}