Greeneerg
Greeneerg

Reputation: 11

Can't get a validation json response from a controller to function in laravel 4?

This is my 'mypostitem.blade.php' view file.

 <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 I have ..

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

In my routes.php I have ...

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

The problem I am facing is that, nothing is displayed when I submit the form? Any input is much appreciated. Thanks

Upvotes: 1

Views: 399

Answers (2)

The Alpha
The Alpha

Reputation: 146239

You may try something like this, in your 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);
}

In your JavaScript in the success handler:

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);
}

Upvotes: 1

Tony Arra
Tony Arra

Reputation: 11119

I can see one immediate bug: postStoreitem() returns nothing if the validation is successful. Even if you're expecting the validation to fail, you need this for debugging purposes:

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

Your callback function also won't work:

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

data is a string (in this case, a JSONified array, which is a string). You need to use json_decode on data before you can use $.each() on it. Try printing data as a string first to see if you're getting validation errors back.

Upvotes: 1

Related Questions