Hello,
I have table with form, here is code:
<tbody>
@foreach($cart_products as $index => $cart_product)
<tr>
<td>{{ $cart_product->product->name }}</td>
<td>
{!! Form::open(['action' => ['CartsController@updateQuantity', $cart_product->id], 'class' => 'form-inline']) !!}
<div class="input-group">
<div class="input-group">
{!! Form::text('quantity', $cart_product->quantity, ['class' => 'form-control']) !!}
<span class="input-group-btn">
{!! Form::submit('Set', ['class' => 'btn btn-success']) !!}
</span>
</div>
</div>
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
After I submit one of form with for example quantity value "10" and validation fails (because max available value is 5) all forms' inputs get value 10. Not just that form was submitted but all forms. How to prevent that?
To make more clear every thing before editing table is:
| Product 1 | [ 4 ] [SET] |
| Product 2 | [ 2 ] [SET] |
| Product 3 | [ 1 ] [SET] |
| Product 4 | [ 2 ] [SET] |
After validation failed tables looks like this:
| Product 1 | [ 10 ] [SET] |
| Product 2 | [ 10 ] [SET] |
| Product 3 | [ 10 ] [SET] |
| Product 4 | [ 10 ] [SET] |
In first row input I entered 10 and pressed first row "set" button.
Table should look like this:
| Product 1 | [ 10 ] [SET] |
| Product 2 | [ 2 ] [SET] |
| Product 3 | [ 1 ] [SET] |
| Product 4 | [ 2 ] [SET] |
UPDATE:
Here is validation code (it works fine):
public function __construct(Factory $factory)
{
$cart_product = CartProduct::findOrFail(Route::input('cart_product'));
$stock = $cart_product->product->stock;
$factory->extend('stockAvailable', function ($attribute, $value, $parameters) use ($stock)
{
return (int)$stock >= (int)$value;
},
'Sorry, in stock we have only '.$stock.' pcs.'
);
}
Hi,
Form::text('name') will get Input::old('name'), once you submit your form, the values of those fields will be replaced by Input::old('name'). Since you have the same field names on your textfield, all those fields will be overwritten. I suggest you append like an index/counter to your fields so it each fields will be treated differently.
beanmoss thank you for you answer,
But how should I validate then, if each form will have different field name?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community