Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
140 Vizualizări

Please modify the module so that the three fields appear in red or with a red star next to the field name to alert the user that these fields are required before saving.

(Phone, Identity Number, Nationality)

<?xml version="1.0" encoding="UTF-8"?>

<odoo>

<data>

<record id="view_partner_form_inherit_training" model="ir.ui.view">

<field name="name">res.partner.form.training.center</field>

<field name="model">res.partner</field>

<field name="inherit_id" ref="base.view_partner_form"/>

<field name="priority">99</field>

<field name="arch" type="xml">


<xpath expr="//field[@name='website']" position="before">

<field name="identity_number" placeholder="1012345678"/>

<field name="gender"/>

<field name="nationality_id"/>

</xpath>


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

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

</xpath>


</field>

</record>

<!-- Search View -->

<record id="view_res_partner_filter_inherit_identity" model="ir.ui.view">

<field name="name">res.partner.search.inherit.identity</field>

<field name="model">res.partner</field>

<field name="inherit_id" ref="base.view_res_partner_filter"/>

<field name="arch" type="xml">

<field name="name" position="attributes">

<attribute name="filter_domain">['|', '|',

('name', 'ilike', self),

('phone', 'ilike', self),

('identity_number', 'ilike', self)

]</attribute>

<attribute name="string">Name / Phone / ID</attribute>

</field>

<!-- Group By -->

<xpath expr="//group[@expand='0']" position="inside">

<filter string="Phone" name="group_by_phone" context="{'group_by': 'phone'}"/>

<filter string="ID Number" name="group_by_id_number" context="{'group_by': 'identity_number'}"/>

<filter string="Salesperson" name="group_by_salesperson" context="{'group_by': 'user_id'}"/>

</xpath>

</field>

</record>

</data>

</odoo>




#Python

class ResPartner(models.Model):

_inherit = 'res.partner'


identity_number = fields.Char(string="Identity Number", index=True, size=10, required=True)

gender = fields.Selection([('male', 'Male'), ('female', 'Female')], string="Gender")

nationality_id = fields.Many2one('res.country', string='Nationality', required=True)

phone = fields.Char(string="Phone", required=True)

Imagine profil
Abandonează
Cel mai bun răspuns

Hello! Here's how you can visually highlight required fields in Odoo 18:



  To make fields visually stand out as required, you can use CSS styling combined with Odoo's standard required attribute.

  First, ensure the fields are set as required in your Python model:
  ```python
  phone = fields.Char(string="Phone", required=True)
  identity_number = fields.Char(string="Identity Number", index=True, size=10, required=True)
  nationality_id = fields.Many2one('res.country', string='Nationality', required=True)
  ```

  Next, in your XML view, add a class to the field to apply custom styling:
  ```xml
  
  
  
  ```

  Finally, define the CSS style in your module's static/src/css/custom.css file (you might need to create this file):
  ```css
  .o_required_highlight:before {
    content: "*";
    color: red;
    padding-right: 2px;
  }
  ```

  Remember to link your CSS file in your module's manifest file under 'assets':
  ```python
  'assets': {
    'web.assets_backend': [
      'your_module/static/src/css/custom.css',
    ],
  },
  ```

  After these changes, update the module for the changes to take effect.


For personalized assistance:
https://www.pragtech.co.in/contact-us-mql.html

Imagine profil
Abandonează
Cel mai bun răspuns

Hi,

You can make the fields visually alert the user by marking them as required (which you already did in Python) and adding a red asterisk or changing the label color in the form view. In Odoo, required fields automatically show a red asterisk in the form, but if you want to explicitly color the field label or add styling, you can do it via the form view XML.


Modify the XML as follows,


<?xml version="1.0" encoding="UTF-8"?>

<odoo>

    <data>

        <record id="view_partner_form_inherit_training" model="ir.ui.view">

            <field name="name">res.partner.form.training.center</field>

            <field name="model">res.partner</field>

            <field name="inherit_id" ref="base.view_partner_form"/>

            <field name="priority">99</field>

            <field name="arch" type="xml">

                <xpath expr="//field[@name='website']" position="before">

                    <!-- Identity Number -->

                    <field name="identity_number" placeholder="1012345678" required="1">

                        <attribute name="widget">char</attribute>

                        <attribute name="options">{'no_label': False}</attribute>

                    </field>


                    <!-- Gender -->

                    <field name="gender"/>


                    <!-- Nationality -->

                    <field name="nationality_id" required="1"/>

                </xpath>


                <!-- Phone field placeholder -->

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

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

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

                </xpath>


                <!-- Optional: Add red label using style (if you want extra color besides default required asterisk) -->

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

                    <attribute name="class">o_field_label_red</attribute>

                </xpath>

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

                    <attribute name="class">o_field_label_red</attribute>

                </xpath>

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

                    <attribute name="class">o_field_label_red</attribute>

                </xpath>

            </field>

        </record>

        <!-- Search View -->

        <record id="view_res_partner_filter_inherit_identity" model="ir.ui.view">

            <field name="name">res.partner.search.inherit.identity</field>

            <field name="model">res.partner</field>

            <field name="inherit_id" ref="base.view_res_partner_filter"/>

            <field name="arch" type="xml">

                <field name="name" position="attributes">

                    <attribute name="filter_domain">['|', '|',

                        ('name', 'ilike', self),

                        ('phone', 'ilike', self),

                        ('identity_number', 'ilike', self)

                    ]</attribute>

                    <attribute name="string">Name / Phone / ID</attribute>

                </field>

                <!-- Group By -->

                <xpath expr="//group[@expand='0']" position="inside">

                    <filter string="Phone" name="group_by_phone" context="{'group_by': 'phone'}"/>

                    <filter string="ID Number" name="group_by_id_number" context="{'group_by': 'identity_number'}"/>

                    <filter string="Salesperson" name="group_by_salesperson" context="{'group_by': 'user_id'}"/>

                </xpath>

            </field>

        </record>


        <!-- Optional CSS to style red labels -->

        <template id="partner_form_styles" inherit_id="web.assets_frontend">

            <xpath expr="." position="inside">

                <style>

                    .o_field_label_red .o_field_label {

                        color: red !important;

                    }

                </style>

            </xpath>

        </template>

    </data>

</odoo>


- Required fields: In Python, you already set required=True, so Odoo automatically adds a red asterisk.

- Optional red label: Added a class="o_field_label_red" to force the label text to appear in red for extra emphasis.

-CSS: A small style snippet to color labels red.


Hope it helps

Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
1
sept. 25
284
1
sept. 25
353
2
sept. 25
515
1
sept. 25
392
3
sept. 25
521