Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
1 Responder
289 Visualizações



<xpath expr="//field[@name='phone']" position="attributes">

<attribute name="placeholder">05XXXXXXXX</attribute>

<attribute name="required">1</attribute>

<attribute name="maxlength">10</attribute>

</xpath>

Avatar
Cancelar
Melhor resposta

Hi,

To prevent the user from entering more than 10 digits in a phone number field in Odoo, especially from the XML view side, your current approach will not work in standard Odoo for Char fields because Odoo does not natively pass maxlength from XML to the HTML input.


*  Add size=10 to the field definition in Python

       In your model (res.partner or custom model), define:

            phone = fields.Char(string="Phone", size=10)

                   -This limits stored data to 10 characters at the database level.

                   - But it won’t prevent a user from entering more than 10 until saving.


* Backend validation with @api.constrains

            @api.constrains('phone')

             def _check_phone_length(self):

                         for rec in self:

                                phone = (rec.phone or '').strip()

                                digits_only = re.sub(r'\D', '', phone)

                                 if len(digits_only) != 10:

                                         raise ValidationError(_("Phone number must contain exactly 10 digits."))

This guarantees data integrity even if a frontend check is bypassed.


Hope it helps

Avatar
Cancelar
Publicações relacionadas Respostas Visualizações Atividade
2
jul. 25
232
2
jul. 25
946
4
jul. 25
794
1
jul. 25
111
2
jul. 25
230