Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
263 Lượt xem

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)

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 10 25
168
2
thg 10 25
265
1
thg 9 25
315
1
thg 9 25
392
2
thg 9 25
545