I am trying to dynamically add new row to a form using JQuery. This works well but my form validation does not work. Please I need help here. Note: I am working with laravel 5.3
This is my html file
<form action="" method="post">
@if(isset($errors))
@if(count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li> here</li>
@endForeach
</ul>
</div>
@endIf
@endIf
<div class="row" id="newlink">
<div class="form-group col-sm-12">
<div class="form-group col-sm-2">
<label class="">Title</label>
<input name="title[]" id="title" type="text" class="form-control"/>
</div>
<div class="form-group col-sm-3">
<label class="">Description</label>
<textarea name="description[]" class="form-control"></textarea>
</div>
<div class="form-group col-sm-2">
<label class="">Unit Price</label>
<input name="price[]" id="price" type="text" class="form-control"/>
</div>
<div class="form-group col-sm-2">
<label class="">Quantity</label>
<input name="quantity[]" id="quantity" type="text" class="form-control"/>
</div>
<div class="form-group col-sm-3">
<label class="">Total</label>
<input name="total[]" id="total" type="text" class="form-control"/>
</div>
<div class="form-group col-sm-3">
<input name="request_id[]" id="request_id" type="hidden" class="form-control" value="@if(isset($detail)) @endif "/>
</div>
</div>
</div>
<div id='newlinktpl' style='display:none'>
<div class="form-group col-sm-12">
<div class="form-group col-sm-2">
<label class="">Title</label>
<input name="title[]" id="title" type="text" class="form-control"/>
</div>
<div class="form-group col-sm-3">
<label class="">Description</label>
<textarea name="description[]" class="form-control"></textarea>
</div>
<div class="form-group col-sm-2">
<label class="">Unit Price</label>
<input name="price[]" id="price" type="text" class="form-control"/>
</div>
<div class="form-group col-sm-2">
<label class="">Quantity</label>
<input name="quantity[]" id="quantity" type="text" class="form-control"/>
</div>
<div class="form-group col-sm-3">
<label class="">Total</label>
<input name="total[]" id="total" type="text" class="form-control"/>
</div>
<div class="form-group col-sm-3">
<input name="request_id[]" id="request_id" type="hidden" class="form-control" value="@if(isset($detail)) @endif "/>
</div>
</div>
</div>
<div class="form-group col-sm-12">
<button class ="btn btn-sm btn-danger" onclick="new_link();"><i class="fa fa-plus"> Add Item</i></button>
</div>
<div class="form-group col-sm-12">
<input type="submit" value="Submit" class="btn btn-sm btn-primary"/>
</div>
</form>
This is my javascript file
var ct = 1;
function new_link()
{
ct++;
var div1 = document.createElement('div');
div1.id = ct;
// link to delete extended form elements
var delLink = '<div style="text-align:right;margin-right:65px"><a href="javascript:delIt('+ ct +')">Del</a></div>';
div1.innerHTML = document.getElementById('newlinktpl').innerHTML + delLink;
document.getElementById('newlink').appendChild(div1);
//e.preventDefault();
event.preventDefault();
}
// function to delete the newly added set of elements
function delIt(eleId)
{
d = document;
var ele = d.getElementById(eleId);
var parentEle = d.getElementById('newlink');
parentEle.removeChild(ele);
}
This is my controller file where I am trying to validate the data entered into these rows
public function saveRequests(Request $request){
$method = $request->isMethod('post');
if($method){
$validator = Validator::make($request->all(), [
'title' => 'required|max:255',
'description' => 'required|max:255',
'price' => 'required|numeric',
'quantity' => 'required|numeric',
'total' => 'required|numeric'
]);
if($validator->fails()){
return redirect('/saveRequests')->withErrors($validator)->withInput();
//echo "failed";
}
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire