mardi 28 août 2018

Laravel 5.6 - Mixture of POST and AngularJS

Question

How do I properly output "number:4" to "4" in a Laravel blade when returning angularjs data from old()?

Scenario

I'm posting an object using Laravel routing

<form method="POST" action="/pricestructures/create">

  <input name="_method" type="hidden" value="POST">

  

  <div class="form-group">
    <label for="">Name</label>
    <input type="text" name="name" class="form-control" placeholder="Name" required value="" required>
  </div>

  <div class="form-group">
    <label for="">Client</label>
    <select name="client_id" ng-model="data.client_id" ng-options="o.id as o.name for o in Clients" class="form-control" required ng-change="GetAssets()">
      <option value="">Please select</option>
    </select>
  </div>

  <div class="form-group">
    <label for="">Asset</label>
    <select name="asset_id" ng-model="data.asset_id" ng-options="o.id as o.name for o in Assets" class="form-control" required ng-disabled="!data.client_id">
      <option value="">Please select</option>
    </select>
  </div>

  <div class="form-group">
    <label for="">Quantity</label>
    <input type="number" name="quantity" class="form-control" placeholder="Quantity" required min="0" step="1" value="" required>
  </div>

  <div class="form-group">
    <label for="">Price</label>
    <input type="number" name="price" class="form-control" placeholder="Price" required min="0" step="0.01" value="" required>
  </div>


  <div class="form-group">
    <button type="submit" class="btn btn-primary">Save Changes</button>
  </div>

</form>

This form is within an AngularJS 1.6 controller and I monitor the client_id select and download relevant assets when it changes using GetAssets(). Because I am using AngularJS for this, I am storing the values of both the client_id and asset_id fields in the $scope.

Likewise when any errors are returned I would like to use old() to repopulate the $scope variables.

$scope.data = {
    client_id: ,
    asset_id: 
};

When I submit the form, I can see that the data being sent is being "typed", well, the values associated with the select dropdowns are anyway

FormData

As mentioned if there is an issue on submitting I repopulate the form with the submitted data using old(), however for the client_id and asset_id variable (i.e. the Angular JS ones) I am being returned "number:1" and I'm not sure how I turn that into "1".

If I happen to store the angularjs variables as strings they get posted as e.g. "string:1"



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire