Search code examples
inheritanceattributesodooodoo-14

Inherit view - add attribute maxlength to char (input text in web) field


Inherit view - add attribute maxlength to char (input text in web) field Avatar Victor Torralba 24 mayo 2022 viewsinheritanceattributes

Hello, I'm trying to add the attribute maxlength="100" to the description field in the products form.

I've done an inheritance and also modified 'placeholder' value and added a new class 'border' to the element.

But I'n not able to define maxlength=10.

Here is the code:

<odoo>
  <data>
    <record id="inherit_view_product_template_product_form" model="ir.ui.view">
      <field name="name">inherit_view_product_template_product.form</field>
      <field name="model">product.template</field>
      <field name="inherit_id" ref="product.product_template_form_view" />
      <field name="arch" type="xml">
        <xpath expr="//field[@name='name']" position="attributes">
          <field name="name" placeholder="Max length 10 chars">
            <attribute name="class" add="border" remove="" separator=" " />
            <attribute name="maxlength" add="10" remove="" separator=" " />
          </field>
        </xpath>
      </field>
    </record>
  </data>
</odoo>

What am I missing?

Thanks for your help.


Solution

  • You can't do it from xml, What you can do is:

    1. Use constraints to show error message if size more than specific number and this error will raise once you click on save button :
    @api.constrains('name')
    def _check_name(self):
        for product in self:
            if len(product.name) > 10:
                raise ValidationError('The name of product must be less than or equal 10 characters')
    
    1. You can set the size attribute on field level:
     name = fields.Char('Name', index=True, required=True, translate=True, size=10)