|
1 | 1 | # Copyright 2024 Roger Sans <roger.sans@sygel.es> |
2 | 2 | # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | +import re |
3 | 4 |
|
4 | | -from odoo import api, fields, models |
| 5 | +from odoo import _, api, fields, models |
| 6 | +from odoo.exceptions import ValidationError |
5 | 7 |
|
6 | 8 |
|
7 | 9 | class ResPartner(models.Model): |
@@ -77,3 +79,47 @@ def _address_fields(self): |
77 | 79 | "private_country_id", |
78 | 80 | ] |
79 | 81 | return res |
| 82 | + |
| 83 | + @api.constrains("private_phone", "private_mobile", "private_email") |
| 84 | + def _check_internal_contact_info_format(self): |
| 85 | + phone_pattern = re.compile(r"^\+?[0-9\s\-]{7,20}$") |
| 86 | + email_pattern = re.compile(r"^[\w\.-]+@[\w\.-]+\.\w+$") |
| 87 | + |
| 88 | + for record in self: |
| 89 | + if record.private_phone and not phone_pattern.match(record.private_phone): |
| 90 | + raise ValidationError( |
| 91 | + _( |
| 92 | + "Invalid format for Private Phone. Examples: " |
| 93 | + "'+34 912 345 678', '912-345-678'." |
| 94 | + ) |
| 95 | + ) |
| 96 | + if record.private_mobile and not phone_pattern.match(record.private_mobile): |
| 97 | + raise ValidationError( |
| 98 | + _( |
| 99 | + "Invalid format for Private Mobile. Examples: " |
| 100 | + "'+34 600 100 200', '601-602-603'." |
| 101 | + ) |
| 102 | + ) |
| 103 | + if record.private_email and not email_pattern.match(record.private_email): |
| 104 | + raise ValidationError( |
| 105 | + _( |
| 106 | + "Invalid format for Private Email." |
| 107 | + "Example: 'john.doe@example.com'." |
| 108 | + ) |
| 109 | + ) |
| 110 | + |
| 111 | + @api.onchange("private_phone", "country_id", "company_id") |
| 112 | + def _onchange_private_phone_validation(self): |
| 113 | + if self.private_phone: |
| 114 | + self.private_phone = ( |
| 115 | + self._phone_format(fname="private_phone", force_format="INTERNATIONAL") |
| 116 | + or self.private_phone |
| 117 | + ) |
| 118 | + |
| 119 | + @api.onchange("private_mobile", "country_id", "company_id") |
| 120 | + def _onchange_private_mobile_validation(self): |
| 121 | + if self.private_mobile: |
| 122 | + self.private_mobile = ( |
| 123 | + self._phone_format(fname="private_mobile", force_format="INTERNATIONAL") |
| 124 | + or self.private_mobile |
| 125 | + ) |
0 commit comments