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

I have a field that is defined as:-

request_text = fields.Html('Incident Description', required=True)

and in the xml:-

<field name="request_text" widget="html" />​

But somehow the field is not mandatory in the form.

How do I make sure that the field is made mandatory and it is highlighted as mandatory in the form and the form shows validation error when it is submitted without filling this field?

Update:-

I've tried the following changes in the XML file unsuccessfully :-

<field name="request_text" widget="html" required="1"/>
<field name="request_text" widget="html">
<attribute name="required">True</attribute>
</field>

This is really strange given that the field definition is set to "required=true". Is this a bug?

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

Just quick check the code (didn't dig deeper) and it seems HTML field has fallback value  `<p><br/></p>` while fields have no value for avoiding the crash of Wysiwyg editor.
that's why it didn't raise the required warning on save of record because it has a fallback value.

an alternate is overriding create method (python) and check the value and if it's `<p><br/></p>` raise the error.
another option extends HTML field override the _setValue for the same reason :)

ref: https://github.com/odoo/odoo/blob/13.0/addons/web_editor/static/src/js/backend/field_html.js#L372

Ảnh đại diện
Huỷ bỏ
Tác giả

Ravi, thanks for the quick check. That was helpful insight.

I tried as you suggested, to overriding the create method like so:-

@api.model

def create(self, vals):

if vals.get('request_text') == "<p><br/></p>":

raise exceptions.Warning("Incident Description cannot be empty")

But unfortunately, didn't seem to have any effect whatsoever. Not sure if I messed up in following your suggestion or if there's something more here. Kindly let me know if you're able to find out more. Thanks.

there is no backslash after br tag

so it should be like

if vals.get('request_text') == "<p><br></p>":

Tác giả

Thanks Ravi, this worked!

Câu trả lời hay nhất

Try this:

<field name="request_text" widget="html" required="1"/>​

Ảnh đại diện
Huỷ bỏ
Tác giả

I have tried this, didn't work. Please check update for more things that I've tried.

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 9 23
3315
0
thg 5 22
3309
4
thg 6 21
7094
1
thg 6 17
10884
3
thg 3 15
8709