Witam, mam taki problem. Przesyłam dane z pewnego formularza za pomocą ajaxa do funkcji w kontrolerze. Walidacja działa do momentu w którym spełnię wszystkie warunki potrzebne do przejścia dalej i wtedy nagle kod się nie wykonuje.
Kod HTML:
<div class="alert alert-danger print-error-msg" style="display:none">
<ul></ul>
</div>
<form id="add_vat_form">
@csrf
<table style="width: 50%; margin-left:50%; margin-bottom: 20px;">
<tr>
<th style="width: 50%; text-align: right;">Sprzedawca</th>
<th style="width: 50%; text-align: right;">Klient</th>
</tr>
<tr>
<td style="text-align: right;" class="form-inline">
<div class="form-group">
<label for="exampleInputName2"> Nazwa: </label>
<input type="text" name="seller" class="form-control" placeholder="Nazwa">
</div>
</td>
<td style="text-align: right;" class="form-inline">
<div class="form-group">
<label for="exampleInputEmail2"> Nazwa: </label>
<input type="text" name="client" class="form-control" placeholder="Nazwa">
</div>
</td>
</tr>
<tr>
<td style="text-align: right;" class="form-inline">
<div class="form-group">
<label for="exampleInputName2"> NIP: </label>
<input type="text" name="seller_nip" class="form-control" placeholder="NIP">
</div>
</td>
<td style="text-align: right;" class="form-inline">
<div class="form-group">
<label for="exampleInputEmail2"> NIP: </label>
<input type="text" name="client_nip" class="form-control" placeholder="NIP">
</div>
</td>
</tr>
<tr>
<td style="text-align: right;" class="form-inline">
<div class="form-group">
<label for="exampleInputName2"> Miejscowość: </label>
<input type="text" name="seller_city" class="form-control" placeholder="Miejscowość">
</div>
</td>
<td style="text-align: right;" class="form-inline">
<div class="form-group">
<label for="exampleInputEmail2"> Miejscowość: </label>
<input type="text" name="client_city" class="form-control" placeholder="Miejscowość">
</div>
</td>
</tr>
<tr>
<td style="text-align: right;" class="form-inline">
<div class="form-group">
<label for="exampleInputName2"> Ulica: </label>
<input type="text" name="seller_street" class="form-control" placeholder="Ulica">
</div>
</td>
<td style="text-align: right;" class="form-inline">
<div class="form-group">
<label for="exampleInputEmail2"> Ulica: </label>
<input type="email" name="client_street" class="form-control" placeholder="Ulica">
</div>
</td>
</tr>
<tr>
<td style="text-align: right;" class="form-inline">
<div class="form-group">
<label for="exampleInputName2"> Kod pocztowy: </label>
<input type="text" name="seller_postcode" class="form-control" placeholder="Kod pocztowy">
</div>
</td>
<td style="text-align: right;" class="form-inline">
<div class="form-group">
<label for="exampleInputEmail2"> Kod pocztowy: </label>
<input type="email" name="client_postcode" class="form-control" placeholder="Kod pocztowy">
</div>
</td>
</tr>
</table>
<table class="table table-bordered">
<tr>
<th >Nazwa produktu</th>
<th style="width: 7%;">Liczba produktów</th>
<th style="width: 10%;">Cena jednostkowa netto</th>
<th style="width: 10%;">Wartość całkowita netto</th>
<th style="width: 10%;">Stawka VAT</th>
<th style="width: 7%;">Kwota VAT</th>
<th style="width: 10%;">Cena jednostkowa brutto</th>
<th style="width: 10%;">Wartość całkowita brutto</th>
<th style="width: 5%;">Akcje</th>
</tr>
<tbody id="products_table">
<tr id="1">
<td><input type="text" class="form-control" name="name[]" id="name1" placeholder="Nazwa produktu"></td>
<td><input type="number" class="form-control" onchange="count_changed(1)" name="count[]" id="count1" value="1" placeholder="Liczba produktów"></td>
<td><input type="text" class="form-control" onchange="change_brutto(1)" name="price_netto[]" id="price_netto1" value="0.00" placeholder="Cena netto"></td>
<td><input type="text" class="form-control" name="summary_netto[]" id="summary_netto1" value="0.00" placeholder="Wartość całkowita netto" readonly></td>
<td class="input-group"><input type="text" class="form-control" onchange="vat_changed(1)" name="vat_rate[]" id="vat_rate1" value="23" placeholder="Stawka VAT">
<span class="input-group-addon">%</span>
</td>
<td><input type="text" class="form-control" name="vat_price[]" id="vat_price1" value="0.00" readonly></td>
<td><input type="text" class="form-control" onchange="change_netto(1)" name="price_brutto[]" id="price_brutto1" value="0.00" placeholder="Cena brutto"></td>
<td><input type="text" class="form-control" name="summary_entity[]" id="summary1" value="0.00" readonly></td>
<td><button type="button" onclick="delete_product(1)" class="btn btn-alert">Usuń</button></td>
</tr>
</tbody>
</table>
<button type="button" id="add_product" style="margin-bottom: 20px;" class="btn btn-primary">Dodaj produkt</button>
<table class="table table-bordered">
<tr>
<th style="width: 38%; text-align: right;"></th>
<th style="text-align: left;">Cena netto:</th>
<th style="text-align: left;">Cena VAT</th>
<th style="text-align: left;">Cena brutto:</th>
</tr>
<tr>
<td style="width: 38%; text-align: right;">Razem do zapłaty: </td>
<td style="text-align: left;"><input class="form-control" type="text" id="final_netto" name="final_netto" value="" readonly></td>
<td style="text-align: left;"><input class="form-control" name="final_vat" id="final_vat" type="text" value="" readonly></td>
<td style="text-align: left;"><input class="form-control" name="final_brutto" id="final_brutto" type="text" value="" readonly></td>
</tr>
</table>
<button type="success" id="add_product" style="margin-bottom: 20px;" class="btn btn-submit btn-success">Zapisz fakturę</button>
</form>
Kod AJAX:
$(document).ready(function() {
$(".btn-submit").click(function(e){
e.preventDefault();
var _token = $("input[name='_token']").val();
var seller = $("input[name='seller']").val();
var seller_nip = $("input[name='seller_nip']").val();
var seller_city = $("input[name='seller_city']").val();
var seller_street = $("input[name='seller_street']").val();
var seller_postcode = $("input[name='seller_postcode']").val();
var client = $("input[name='client']").val();
var client_nip = $("input[name='client_nip']").val();
var client_city = $("input[name='client_city']").val();
var client_street = $("input[name='client_street']").val();
var client_postcode = $("input[name='client_postcode']").val();
var final_netto = $("input[name='final_netto']").val();
var final_vat = $("input[name='final_vat']").val();
var final_brutto = $("input[name='final_brutto']").val();
var name = [];
$('input[name^="name"]').each(function() {
name.push(this.value);
});
var price_netto = [];
$('input[name^="price_netto"]').each(function() {
price_netto.push(this.value);
});
var price_brutto = [];
$('input[name^="price_brutto"]').each(function() {
price_brutto.push(this.value);
});
var vat_rate = [];
$('input[name^="vat_rate"]').each(function() {
vat_rate.push(this.value);
});
var count = [];
$('input[name^="count"]').each(function() {
count.push(this.value);
});
$.ajax({
url: "{{ route('add.vat') }}",
type:'POST',
data: {_token:_token, seller:seller, seller_nip:seller_nip, seller_city:seller_city, seller_street:seller_street, seller_postcode:seller_postcode, client:client, client_nip:client_nip, client_city:client_city, client_street:client_street, client_postcode:client_postcode, name:name, price_netto:price_netto, price_brutto:price_brutto, vat_rate:vat_rate, count:count},
dataType: 'json',
success: function(data) {
if($.isEmptyObject(data.error)){
alert(data.success);
}else{
printErrorMsg(data.error);
}
}
});
});
function printErrorMsg (msg) {
$(".print-error-msg").find("ul").html('');
$(".print-error-msg").css('display','block');
$.each( msg, function( key, value ) {
$(".print-error-msg").find("ul").append('<li>'+value+'</li>');
});
}
});
Kod PHP:
public function add_vat(Request $request)
{
$validator = Validator::make($request->all(), [
'seller' => 'required',
'seller_city' => 'required',
'seller_street' => 'required',
'seller_postcode' => 'required',
'client' => 'required',
'client_city' => 'required',
'client_street' => 'required',
'client_postcode' => 'required',
'name.*' => 'required',
'price_netto.*' => 'required',
'price_brutto.*' => 'required',
'vat_rate.*' => 'required',
'count.*' => 'required',
]);
if ($validator->passes())
{
$data = $request->all();
$Vat = Vat::create([
'seller' => $data['seller'],
'seller_nip' => $data['seller_nip'],
'seller_city' => $data['seller_city'],
'seller_street' => $data['seller_street'],
'seller_postcode' => $data['seller_postcode'],
'client' => $data['client'],
'client_nip' => $data['client_nip'],
'client_city' => $data['client_city'],
'client_street' => $data['client_street'],
'client_postcode' => $data['client_postcode'],
'final_price_netto' => $data['final_netto'],
'final_price_vat' => $data['final_vat'],
'final_price_brutto' => $data['final_brutto']
]);
$vat_id = $Vat->id;
foreach($data['name'] as $key => $value)
{
Product::create([
'name' => $value,
'price_netto' => $data['price_netto'][$key],
'price_brutto' => $data['price_brutto'][$key],
'vat_rate' => $data['vat_rate'][$key],
'vat_id' => $vat_id,
'count' => $data['count'][$key]
]);
}
return response()->json(['success'=>'Added new records.']);
}
$data = $request->all();
return response()->json(['error'=>$request->input('name')]);
}