I have a dynamic field and I can't get the old value because I added this dynamic field with jQuery, but after validation I don't have the fields.
My jQuery code:
var fielddd = 1;
$(document).on('click', '.btn.add-field', function() {
fielddd++;
$('#newfield').append('<textarea class="from-control" name="somename[]" id="field_' + field + '"></textarea>' + '<button class="btn add-field">add</button>' + '<textarea class="from-control" name="somename2[]" id="field_' + field + '"></textarea>');
});
HTML code:
<div class="row">
<div class="{{($errors->has('somename[]')}}"
<textarea class="from-control" name="somename[]" id="field">
</textarea>
{!! $errors->first('somename[]',:message) !!}
</div>
<button class="btn add-field">add</button>
<div class="{{($errors->has('name[]')}}"
<textarea class="from-control" name="name[]" id="field">
</textarea>
{!! $errors->first('somename[]',:message) !!}
</div>
</div>
<div class="row" id="newfield">
<!-- new textarea -->
</div>
How can I get the same fields with the same input after validation?
I for example some field is empty. If I press add field, and after that pressing the button submit the laravel check if the field is empty and if yes then it redirect to the same page and say to put some value inside the text area.
You could print out the old response in JavaScript in the form, that you could then fetch from. If you printed out old('somefield[]')
as JSON, you could then read each value and re-populate the form with textarea
s.
var oldValues = json_encode(old('somefield[]'))
Then you could use oldValues
oldValues.forEach(function(oldValue) {
// create new textarea using oldValue as value
})