How to Use ValidationError and UserError in Odoo

Hello Odooers & Developers!

Welcome to this hands-on guide to Odoo!

Odoo prevents users from making mistakes or acting incorrectly while working on custom modules or changing business rules. It provides built-in exceptions like ValidationError and UserError to indicate when something goes wrong clearly. These exceptions help check conditions and simply alert users. They also help keep system data clean and guide users through the correct process.

Common Exceptions in Odoo

Odoo provides several types of exceptions, such as:

  1. ValidationError
  2. UserError
  3. AccessError
  4. MissingError
  5. AccessDenied
  6. IndirectWarning
  7. CacheMiss

But don't worry! You don’t need to learn all of them at once.

There are many types of errors (called exceptions) in Odoo, but in this blog, we will look at just two important ones that are used most often:

  • UserError

  • ValidationError

What is ValidationError?

ValidationError is used when data entered by the user is not valid.
You can think of it like saying: "Hey, this input is wrong. Please fix it before saving."

Example:

Imagine you are creating a new partner (customer), and someone enters an email that another partner already uses. You want to stop them and show a clear message.

.py

from odoo import models, api, _
from odoo.exceptions import ValidationError

class ResPartnerInherited(models.Model):

   _inherit = "res.partner"
   @api.model
  def create(self, vals):
        if self.env["res.partner"].search([("email", "=", vals.get('email'))]):
            raise ValidationError(_("Another user is already using this email. Please use a different email."))
        return super(ResPartnerInherited, self).create(vals)

 What it does:

  • Before saving the new partner, it checks if the email already exists.
  • If yes, it raises a ValidationError with a friendly message.
  • The form will not save until the user corrects the email.
  • Yes, it stops the action.
  • Yes, it shows a message to the user.
  • Message: "Another user is already created using this email."

In this screen, a contact named Kenil was successfully created with the email [email protected]. Since no validation error was triggered, the email was accepted, and the contact was saved normally in the system.

ValidationError and UserError in Odoo

In this screen, a new contact named Kunj is being created using the same email [email protected]. However, a Validation Error is shown because another contact (Kenil) has already used this email. The system prevents duplicate emails to ensure each contact has a unique email address.

ValidationError and UserError in Odoo

What is UserError?

UserError is used when you want to show a warning message to the user, but it’s not about wrong data.
It’s more like: “You’re not allowed to do this” or “This action is not possible.”

Example:

Let's say only admin users are allowed to upload SVG image files. If a regular user tries, we want to show a clear message.

.py

from odoo import models, fields, api, _
from odoo.exceptions import UserError

class ExceptionHandler(models.Model):

_name = "test.exception"
_description = "Test Exception for SVG Upload"
name = fields.Char("Name")
svg_file = fields.Binary("SVG File")
file_name = fields.Char("File Name")  # optional: filename
@api.constrains('svg_file', 'file_name')
def _check_svg_file_upload(self):

    for record in self:

     if record.svg_file and record.file_name and record.file_name.lower().endswith('.svg'):
            if self.env.user.id != self.env.ref('base.user_admin').id:
            raise UserError(_("Only admin users can upload SVG files."))

 What it does:

  • When this function runs, it immediately stops and shows the error message.
  • The message is clear and helps the user understand what went wrong.
  • You can raise UserError if you want to guide the user with a message.
  • Yes, it stops the action.
  • Yes, it shows a message to the user.
  • Message: "Only admins can upload SVG files."

ValidationError and UserError in Odoo

This screen represents a scenario where an SVG file was successfully uploaded by the admin, and the system. 

ValidationError and UserError in Odoo

The currently logged-in user is Marc Demo (highlighted in the top-right corner of the screen).

Marc Demo is not an Admin user.

Marc tried to upload an SVG file (file1.svg).

As a result, the system triggered a User Error, blocking the upload.

Conclusion

Working on custom modules or updating business rules in Odoo prevents users from taking incorrect actions or entering invalid data. Odoo makes this easy with built-in exceptions like ValidationError and UserError. These help developers show clear and friendly messages when something goes wrong, guiding users to follow the correct steps. At Devintellecs, a trusted Odoo service provider, we often use these tools to build smarter, safer, and more Odoo apps for our clients.

Odoo DEV May 26, 2025
Share this post
Archive
Sign in to leave a comment
How to Create Sequence Numbers in Odoo 18