M.Lamine Lalmi
M.Lamine Lalmi

Reputation: 88

How to show validation error like the error when i miss required field?

I'm new with odoo 11, I want to show a custom error popup but I want to show it like the popup error displayed when we miss a required field and this is not possible with ValidationError. Is that possible ? If not, is it possible to use JavaScript api for notification like sweet alert ?

Upvotes: 1

Views: 3791

Answers (5)

Vinh VO
Vinh VO

Reputation: 703

On Odoo 11, if you want to

  • raise an Error by Python

    # Import the exceptions from odoo
    from odoo import exceptions
    
    # Then somewhere in your code
    raise exceptions.ValidationError('Your error message here!')
    
    # Or more generic error by
    raise exceptions.UserError('This is not a ValidationError.')
    
  • do_warn (notification) by Javascript

    // Maybe inside your Widget
    var self = this;
    self.do_warn('Your title', 'Your message')
    

Upvotes: 1

Kenly
Kenly

Reputation: 26688

You can show the same error as when you miss required fields using the following javascript code:

this.do_warn(_t("The following fields are invalid :"), "msg");

You can also use SweetAlert, just add the javascript to your module.

Upvotes: 0

Hilar AK
Hilar AK

Reputation: 1675

you can follow the below method.

@api.constrains('field_name')
def _check_field_name(self):
    for record in self:
        if not record.field_name: # or your conditions
        raise ValidationError(_('Error COntext'))

First of ALL, you cant save the form without filling the required field. If you try to save you will get a warning ant cant save.

Upvotes: 2

Ajay Chauhan
Ajay Chauhan

Reputation: 109

You can't achieve this with validationerror because framework will not pass the values till it satisfied, so create & write method will not get called.

You need to make changes in JavaScript & change the behavior as you want.

Upvotes: 0

rahul mehra
rahul mehra

Reputation: 438

Yes, you can raise an alert using ValidationError but for this you need to override the create and write method and need to check this field's value and if it is empty you can raise ValidationError alert.

Upvotes: 1

Related Questions