Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
251 Visualizzazioni

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)

Avatar
Abbandona
Risposta migliore

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

Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
3
ott 25
134
2
ott 25
251
1
set 25
311
1
set 25
388
2
set 25
545