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

I have a form and on save the python check code is run which throws a ValidationError.

@api.constrains('a', 'b')
def _check_a_and_b(self):
for record in self:
if not record.a and not record.b:
raise ValidationError(_('You have to specify at least a or b'))

If the ValidationError is triggered from the form save I want the form field 'a' to get the focus.

Ảnh đại diện
Huỷ bỏ

A better idea would be to make the fields a and b as required fields. Upon save if both fields aren't filled, both would be highlighted in red.

Tác giả

The example checks that at least one of the two fields is filled in. I know that this could be solved with attrs=required...

But unfortunately the real use case is more complex than the simple example I provided and the condition in which an ValidationError is raised is not available on the client side.