Reputation: 1841
I want to restrict the user selecting previous date from the date picker of odoo-8. Please give me how to disable previous dates in odoo datepicker
Upvotes: 2
Views: 5662
Reputation: 769
For anyone looking for a solution that works in Odoo 17.
First, create a datetime_field.js
module somewhere inside the static/
directory and ensure that it is added in the web.assets_backend
section of the __manifest__.py
, for example:
{
...,
"assets": {
"web.assets_backend": [
"<module_name>/static/src/views/**/*",
]
},
Then define the DateTimeField
extension:
/** @odoo-module **/
import {
DateTimeField,
dateTimeField,
} from "@web/views/fields/datetime/datetime_field";
import { registry } from "@web/core/registry";
export class CustomDateTimeField extends DateTimeField {
static defaultProps = {
...DateTimeField.defaultProps,
minDate: "today",
};
}
export const customDateTimeField = {
...dateTimeField,
component: CustomDateTimeField,
};
registry.category("fields").add("custom_datetime", customDateTimeField);
Use this custom widget in the XML like this:
<field name="some_datetime_field" widget="custom_datetime" />
Upvotes: 0
Reputation: 9624
There's a module for that https://apps.openerp.com/apps/modules/8.0/web_widget_datepicker_options/
If you have a date field named current_date
<field name="current_date" />
After installing the module, just add the option for the jquery datepicker minDate
and set it to 0 like this
<field name="current_date" options="{'datepicker':{'minDate': 0}}"/>
I previously did this by setting an onchange on the field that'll be triggered every time the field is changed, and in the onchange you can convert the date to a python date (with odoo's default time format) and compare it to the current date
from datetime import datetime
from openerp import api
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
from openerp.exceptions import Warning
@api.onchange('current_date')
def onchange_date(self):
if datetime.strptime(self.current_date, DEFAULT_SERVER_DATE_FORMAT).date() < datetime.now().date():
raise warning('Please select a date equal/or greater than the current date')
return False
return my_date
Upvotes: 3