Why date form field is appearing with current date by default?
I have a page to edit the administrators of a post. This page has a form with some radio buttons, each radio button corresponds to an administrator.
I have this jQuery that receives an array with the administrators of a post from the controller and populate the details of each administrator in the form when the corresponding radio button is selected.
The issue is that, there is also a radio button "Create new admin" and when this radio button is clicked the form fields should reset. The issue is that using the moment plugin js to format the date when the "Create new administrator" radio button is selected all fields reset unless the date field, this date field appears with the current date.
Do you know why?
jQuery:
- $(document).ready(function () {
-
- var admins= {!! $admin!!}
-
- $("input[name='radiobutton']").click(function() {
- let id = $(this).attr("id");
- let data = admins.find(e => e.id == id) || {
- name: "",
- date: "",
- };
-
- //alert(data.date);
- $("input[name='name']").val(data.name);
- ...
- $("input[name='date']").val(moment(data.date).format('DD MMMM YYYY h:mm'));
-
- });
- });
Form:
- <form method="post" class="clearfix" action="{{route('admins.update', ['id' => $post->id])}}" enctype="multipart/form-data">
- {{csrf_field()}}
- @foreach($administrators $admin)
- <div class="form-check">
- <input class="form-check-input" type="radio" name="radiobutton" id="{{$admin->id}}" value="">
- <label class="form-check-label" for="">
- {{$admin->name}}
- </label>
- </div>
- @endforeach
- <div class="form-check">
- <input class="form-check-input" type="radio" name="radiobutton" id="create_administrator"
- value="option2">
- <label class="form-check-label" for="exampleRadios2">
- Create new administrator
- </label>
- </div>
- <div class="form-group">
- <label for="name">Name</label>
- <input type="text" required class="form-control" value="{{ $admin->name }}" name="name">
- </div>
- <div class="input-group date" data-provide="datepicker">
- <input type='text' name="date" value="{{ $admin->date->format('d F Y - H:i') }}"
- class="form-control" placeholder="DD/MM/YYY" />
- <span class="input-group-addon"><i class="fa fa-calendar text-primary" aria-hidden="true"></i></span>
- </div>
- <!-- below I have more form fields like administrator name, email, etc -->
-
- <input type="submit" id="adminStoreButton" class="btn btn-primary" value="Create"/>
- <input type="submit" id="adminUpdateButton" class="btn btn-primary" value="Update"/>
- </form>
Some debug notes:
- writing "moment(data.date).format('DD MMMM YYYY h:mm')" on the console it appears "referenceError: data is not defined"
- if I write moment() in the console it appears a moment object. Moment {_isAmomentObject...} so moment is defined
- alert(data) returns "[object Object]"
- alert(data.date) returns "2018-02-07 06:30:00"
- alert(Date(data.date)) shows the current date "Thu Mar 01.."
- alert(Date(data.date)) shows the current date "Thu Mar 01.." however the date field on the form is populated with the correct created date of the selected admin, it appears on the form field the created date of the selected admin: "07 February 2018 - 06:30", but in the alert appears the current date