Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
1 Vastaa
252 Näkymät



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

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

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

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

</xpath>

Avatar
Hylkää
Paras vastaus

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
Hylkää
Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
0
heinäk. 25
27
1
heinäk. 25
144
2
heinäk. 25
132
1
heinäk. 25
412
0
heinäk. 25
376