odooopenerp-7raiseerror

how to create a function to raise error in openerp


I want to create a function that raise an error when the user enters a not valid date,so if we enter a date before the current date it will show an error, any ideas?

My class looks like:

class business_trip(orm.Model):
_columns = {'start_trip': fields.datetime('Trip Starts on:',translate=True, required=False),
        'start_business': fields.datetime('Business Starts on:',translate=True, required=False),
        'end_business': fields.datetime('Business Ends on:',translate=True, required=False),
        'end_trip': fields.datetime('Trip Ends on:',translate=True, required=False),

Solution

  • You can add Add Python constraints like,

    @api.one
    @api.constrains('start_trip', 'start_business', 'end_business', 'end_trip')
    def _check_date(self):
        now = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
        if self.start_trip and  now < self.start_trip:
            raise exceptions.ValidationError("Entered Date Should be greter then Today")
        elif self.start_business and now < self.start_business:
            raise exceptions.ValidationError("Entered Date Should be greter then Today")
        elif self.end_business and now < self.end_business:
            raise exceptions.ValidationError("Entered Date Should be greter then Today")
        elif self.end_trip and now < self.end_trip:
            raise exceptions.ValidationError("Entered Date Should be greter then Today")