I've been using the Vue Formulate library (which is awesome).
I need the user to only be able to pick a date from today (included) onwards.
I am using the "after" default for validation, but today (the current date) is not valid. In other words the validation kicks in when choosing todays date, and the form cannot be submitted.
What is the best way to get around this?
https://codesandbox.io/s/vue-formulate-reproduction-template-forked-kmpgq?fontsize=14&hidenavigation=1&theme=dark
A little workaround by giving the after attribute a Date value before today:
<template>
<FormulateInput
name="date"
label="Date:"
:validation="
'bail|required|after:' + new Date(Date.now() - 24 * 60 * 60 * 1000)
"
:validation-messages="{ after: 'The date has to be today or later' }"
type="date"
/>
</template>
https://codesandbox.io/s/vue-formulate-reproduction-template-forked-ky1hu?fontsize=14&hidenavigation=1&theme=dark
EDIT: In fact you can do it also with a computed property by returning the date before today.
After validation should defined by a Date. Here is the solution by calculating today by a computed property. Codesandbox
<template>
<FormulateInput
name="date"
label="Date:"
:validation="'bail|required|after:' + today"
:validation-messages="{ after: 'The date has to be today or later' }"
type="date"
/>
</template>
<script>
export default {
computed: {
today() {
return new Date().toLocaleDateString();
},
},
};
</script>
Related
I am using element ui datepicker from there. I want to disable past dates.
<el-date-picker
v-model="form.startDate"
type="date"
placeholder="Tarih Seçiniz."
style="width: 100%"
/>
Can you help me to handle this issue?
You can achieve this by adding a :disabled-date attribute in the <el-date-picker> element and pass the boolean value to this attribute based on the calculation.
<el-date-picker :disabled-date="disabledDate" ...
In Script:
const disabledDate = (time: Date) => {
const date = new Date();
const previousDate = date.setDate(date.getDate() - 1);
return time.getTime() < previousDate;
}
I am working on a housing application that currently uses a datetime field for move in date and move in time. I don't really like how the datetime format looks on the frontend, but I want to keep it in my SQL database, so my idea on how to execute this was:
1: When page is loaded, split the datetime field into seperate date and time fields.
2: If the user make changes to the date or time and clicks "update", then the fields will be merged back into the datetime field.
I thought it would be simple, but when I try to do this, I get the error "Cannot set properties of null (setting '0')"
Here is a simplified version of my code. I am using Vue Bootstrap for this application.
<template>
<b-form-row>
<b-col lg="6">
<b-form-group :label="$t('Date and time')">
<date-time-picker
v-model="tenancy.moveInDate"
:format="format"
:show-second="false"
type="datetime"
/>
</b-form-group>
</b-col>
</b-form-row>
<b-form-row>
<b-col lg="6">
<b-form-group :label="$t('Move in date')">
<b-date-picker v-model="tenancy.date"/>
</b-form-group>
</b-col>
</b-form-row>
<b-form-row>
<b-col lg="6">
<b-form-group :label="$t('Move in time')">
<b-time-picker v-model="tenancy.time" />
</b-form-group>
</b-col>
</b-form-row>
</template>
export default {
data() {
return {
tenancy: {
moveInDate: moment('DD/MM/YYYY HH:mm'),
date: moment('DD/MM/YYYY'),
time: moment('HH:mm'),
}
}
},
computed: {
format() {
return localeOptions[this.$i18n.locale].datepicker.format + ' LT';
}
},
methods: {
update(){
this.tenancy.moveInDate[0] = this.tenancy.date;
this.tenancy.moveInDate[1] = this.tenancy.time;
}
},
created() {
this.tenancy.date = tenancy.moveInDate.split('')[0]
this.tenancy.time = tenancy.moveInDate.split('')[1]
}
}
Dates are never going to be simple, sorry. You're off to a bad start attacking them with string methods. Moment is deprecated, and you should be able to do without it. This is doesn't directly fix your problem, but I really recomend you set yourself some rules for date handling. Mine would be: dates should be date objects in your model, formatted at the last moment for display. On the wire, they should be ISO UTC date strings. In the database, they should be UTC datetimes. If you then need to split a datetime into date and time, the date object has all the methods you need: toLocaleTimeString(), toLocaleString(), toLocaleDateString() etc.
I have series of bootstrap date fields like this
<input id="TxtNextReviewDate" type="text" asp-for="RequestFormMaster.NextReviewDate" class="form-control form-control-sm datepicker">
<input id="DateSalesRequest" asp-for="SLAInformation.SalesDateOfSubmission" type="text" class="form-control form-control-sm datepicker" />
<input id="DateFinalInfoReceived" asp-for="SLAInformation.SalesFinalInfoReceived" type="text" class="form-control form-control-sm datepicker" />
i want to disable past dates for Next Review Date Calendar(First Calendar input).For the rest i should able to select the past dates.
This is how i am disabling the past dates, its disabling other date field also.how can i restrict this for only one field?
<script>
var date = new Date();
date.setDate(date.getDate());
$('.datepicker').datepicker({
format: 'dd/mm/yyyy',
startDate: date
});
</script>
Thanks,
Teena
how can i restrict this for only one field?
Three Datepicker inputs contains different id. You can use id selector to distinguish the datepicker.
startDate: date cannnot disable the past date selection, it just set the Datepicker's start date. You need use minDate: 0 to disable past date selection.
Change your code like below:
$('#DateSalesRequest,#DateFinalInfoReceived').datepicker({
format: 'dd/mm/yyyy'
});
$('#TxtNextReviewDate').datepicker({
format: 'dd/mm/yyyy',
minDate: 0
});
Result:
minDate is not working for me. However i managed to fix the issue. Thanks for the hint.
<script>
var date = new Date();
date.setDate(date.getDate());
$('#TxtNextReviewDate').datepicker({
format: 'dd/mm/yyyy',
startDate: date
});
$('#TxtLastDate,#TxtDateFinalApproval').datepicker({
format: 'dd/mm/yyyy',
});
</script>
I need to disable some dates. My attempt is as below.
<v-date-picker
v-model="dates3"
:max-date="today">
</v-date-picker>
data() {
return {
today: '2021-06-04',
dates3: '2021-06-02'
}
}
But it will not disable all the dates after 2021-06-04. Where I was get wrong and how can I fix it.
Took Docs as reference its just max not max-date
<v-date-picker
v-model="date3"
max="today"
/>
Should solve it :)
I am using bsDaterangepicker when clicked it shows current month and next in the popup. All date ranges in my application are in the past, so I need to show current and previous month. How can I configure that?
You use this workaround to solve it:
In HTML:
<input
formControlName="dateRange"
type="text"
bsDaterangepicker
#rangePicker="bsDaterangepicker"
(onShown)="onDateRangePickerShow()"/>
</div>
In component:
export class Component {
#ViewChild('rangePicker') rangePicker;
onDateRangePickerShow() {
// This is a workaround to show previous month
const prevMonth = new Date(moment().subtract(1, 'month'));
this.rangePicker._datepicker.instance.monthSelectHandler({ date: prevMonth });
}