Mask for entering and displaying data in Char fields

Is it possible to use a mask to enter and display data in Char fields?
(Something like inputMask property of QLineEdit widget in Qt.)

We do not have such feature but you can define a domain on the Char field which tries to validate the input.
But it may be a good improvement to validate input using the domain.

I would like to enter the IP-address (000.000.000.000) without dots or MAC-address (HH:HH:HH:HH:HH:HH) without colons, but the separators (dots, colons) should be visible when entering.

Well maybe you can do like the phone number on contact mechanism. Having a formatted and compact value synchronized.

Hi. I have done something similar for what you are asking for. Maybe it results useful

     import re 

     mac_address = fields.Char('MAC address',
        help="Format FF:FF:FF:FF:FF:FF", size=17,
        domain=[
            ('mac_address_length','=', 17),
            ('mac_address_validation', '!=',
                Eval('mac_address_invalid_string'))
            ])
    mac_address_length = fields.Integer('MAC address length')
    mac_address_validation = fields.Char('MAC address validation')
    mac_address_invalid_string = fields.Function(
        fields.Char('Invalid string'),
        'get_mac_address_invalid_string')
    mac_address_check = fields.Function(
        fields.Boolean('MAC address valid', help="Checked if the address is right"),
        'on_change_with_mac_address_check')

   def get_mac_address_invalid_string(self, name=None):
        return gettext('maintenance_main.msg_non_valid_format')

    @fields.depends('mac_address')
    def on_change_with_mac_address_length(self, name=None):
        return self._get_mac_address_lenght()

    @fields.depends('mac_address')
    def on_change_with_mac_address_check(self, name=None):
        res = False
        if self._get_mac_address_lenght() == 17 and \
            self._get_mac_address_validation() != \
            gettext('maintenance_main.msg_non_valid_format'):
            res = True
        return res

    @fields.depends('mac_address')
    def on_change_with_mac_address_validation(self, name=None):
        return self._get_mac_address_validation()

    def _get_mac_address_lenght(self):
        if self.mac_address:
            return len(self.mac_address)
        return 17

    def _get_mac_address_validation(self):
        if self.mac_address:
            p = re.compile(r'(?:[0-9a-fA-F]:?){12}')
            res = re.findall(p, self.mac_address)
            if len(res) == 1 and res[0] == self.mac_address:
                return 'Valid format'
            return gettext('maintenance_main.msg_non_valid_format')
        return 'no mac address'

    @staticmethod
    def default_mac_address_invalid_string():
        return gettext('maintenance_main.msg_non_valid_format')

Hope it helps for someone else.
Regards
Francisco

2 Likes

Thanks a lot for the helpful solution. We will use it after the war.
We are from Ukraine. Stop war in Ukraine

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.