iam supreeth
iam supreeth

Reputation: 497

Python constraints (error) message

I have written a function like below in Python. works like if user selects holiday start-date and holiday end-date with time correctly it will accepts, else it will show an error message like 'Error: Entered Invalid Date or Time' as shown in figenter image description here

Python code:

def _date_start_end_validate(self, cr, uid, ids, context=None):
        sr_ids = self.search(cr, 1 ,[], context=context)
        for self_obj in self.browse(cr, uid, ids, context=context):
            if self_obj.date_start >= self_obj.date_end:
                return False
        return True

_constraints = [(_date_start_end_validate, 'Error: Entered Invalid Date or Time', ['Dates'])]

I need to display which start-date is showing error here, that date should to displayed.

How to get that start-date information in constraints.

Upvotes: 1

Views: 254

Answers (2)

Er.Ankit H Gandhi
Er.Ankit H Gandhi

Reputation: 656

Try below code may be use full.

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

sr_ids = self.search(cr, 1 ,[], context=context)

for self_obj in self.browse(cr, uid, ids, context=context):

    if self_obj.date_start >= self_obj.date_end:

        return False

return True

def _check_msg_with_start_date(self, cr, uid, ids, context):

data = self.browse(cr, uid, ids, context=context)

return "Error: Entered Invalid Date or Time {} ".format(data.date_start)

_constraints = [

(_date_start_end_validate, lambda self, *a, **kw: self._check_msg_with_start_date(*a, **kw), ['date_start','date_end'])

]

Thanks & Regards

Ankit H Gandhi

Upvotes: 2

iam supreeth
iam supreeth

Reputation: 497

It is working I defined:

for self_obj in self.browse(cr, uid, ids, context=context):
            xcv = self_obj['description']
            if self_obj.date_start >= self_obj.date_end:
                raise osv.except_osv(_('Error:'),_('Entered Invalid Date/Time: %s')%(xcv))
        return True

Works fine now.

Upvotes: 1

Related Questions