Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
1 Beantwoorden
376 Weergaven



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

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

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

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

</xpath>

Avatar
Annuleer
Beste antwoord

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
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
1
jul. 25
102
2
jul. 25
195
1
jul. 25
1390
2
jul. 25
232
2
jul. 25
1084