Rami Wafai
Rami Wafai

Reputation: 71

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),

Upvotes: 1

Views: 4072

Answers (2)

Rami Wafai
Rami Wafai

Reputation: 71

Thank you guys, that was my solution:

  def _check_date(self,cr,uid,ids,context=None):

        record=self.browse(cr,uid,ids,context=None)
        for data in record:
            if data.start_trip >= data.start_business or data.start_trip >= data.end_business or data.start_trip >= data.end_trip or data.start_business >= data.end_business or data.start_business >= data.end_trip or data.end_business >= data.end_trip:
                return False
            else:
                return True

_constraints = [(_check_date, 'Error: You Entered Wrong Dates, Please Enter the Right Dates ',['start_trip', 'start_business', 'end_business', 'end_trip'])]

Upvotes: 1

Atul Arvind
Atul Arvind

Reputation: 16733

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")

Upvotes: 1

Related Questions