← Back to team overview

openerp-community team mailing list archive

How to use _constraints to show only one validation message by field

 

Hello appreciated community.

I am beginner about OpenERP and my consultation is as follows: We can use the */_constraints/* to validate one o more fields, but my need is when we use two fields and we have to show only one message by field.

_constraints = [
        (function, msg, ['field1', 'field2']),
]

When the user makes a mistake, OpenERP will show one message indicating the two fields: */Error occurred while validating the field(s): field1, field2/*

*S**o how do the user know on what field is the error and display only one message by field, even if there were (exaggerating) 50 **fields? *Is there a way to give focus to the text box, highlight it?

I was digging in the server source code and found the part where does this:

OpenERP version (client and server): *6.0.3*
File: *orm.py*
Lines: *990-1017*

    def _validate(self, cr, uid, ids, context=None):
        context = context or {}
        lng = context.get('lang', False) or 'en_US'
        trans = self.pool.get('ir.translation')
        error_msgs = []
        for constraint in self._constraints:
            fun, msg, fields = constraint
            if not fun(self, cr, uid, ids):
                # Check presence of __call__ directly instead of using
                # callable() because it will be deprecated as of Python 3.0
                if hasattr(msg, '__call__'):
                    tmp_msg = msg(self, cr, uid, ids, context=context)
                    if isinstance(tmp_msg, tuple):
                        tmp_msg, params = tmp_msg
                        translated_msg = tmp_msg % params
                    else:
                        translated_msg = tmp_msg
                else:
translated_msg = trans._get_source(cr, uid, self._name, 'constraint', lng, source=msg) or msg
                error_msgs.append(
*_("Error occurred while validating the field(s) %s: %s") % (','.join(fields), translated_msg)*
                )
                self._invalids.update(fields)
        if error_msgs:
            cr.rollback()
            raise except_orm('ValidateError', '\n'.join(error_msgs))
        else:
            self._invalids.clear()

Thanks in advance.

Follow ups