[IMP] improvements in form fields error control

This commit is contained in:
braisab
2021-05-05 21:34:18 +02:00
parent a627003bd7
commit e4bdf41ce0
2 changed files with 113 additions and 41 deletions

View File

@@ -118,12 +118,6 @@ class PortalFolio(CustomerPortal):
self, folio_id, access_token=None, report_type=None, download=False, **kw
):
values = self._prepare_portal_layout_values()
values.update(
{
"error": {},
"error_message": [],
}
)
try:
folio_sudo = self._document_check_access(
"pms.folio",
@@ -133,7 +127,7 @@ class PortalFolio(CustomerPortal):
except (AccessError, MissingError):
return request.redirect("/my")
values.update(self._folio_get_page_view_values(folio_sudo, access_token, **kw))
values.update({"no_breadcrumbs": True})
values.update({"no_breadcrumbs": True, "error": {}})
return request.render("pms.portal_my_folio_precheckin", values)
@@ -262,7 +256,7 @@ class PortalReservation(CustomerPortal):
values = self._reservation_get_page_view_values(
reservation_sudo, access_token, **kw
)
values.update({"no_breadcrumbs": True})
values.update({"no_breadcrumbs": True, "error": {}})
return request.render("pms.portal_my_reservation_precheckin", values)
@@ -309,7 +303,7 @@ class PortalPrecheckin(CustomerPortal):
except (AccessError, MissingError):
return request.redirect("/my")
values = self._precheckin_get_page_view_values(checkin_sudo, access_token, **kw)
values.update({"no_breadcrumbs": True})
values.update({"no_breadcrumbs": True, "error": {}})
return request.render("pms.portal_my_precheckin_detail", values)
@http.route(["/my/precheckin"], type="http", auth="user", website=True, csrf=False)
@@ -362,6 +356,7 @@ class PortalPrecheckin(CustomerPortal):
"success": True,
"checkin_partner": checkin_partner,
"no_breadcrumbs": True,
"error": {},
}
)
return request.render("pms.portal_my_precheckin_detail", values1)
@@ -383,11 +378,14 @@ class PortalPrecheckin(CustomerPortal):
["/my/precheckin/folio_reservation"],
type="http",
auth="user",
website=False,
csrf=True,
website=True,
csrf=False,
)
def portal_precheckin_folio_submit(self, **kw):
errors = {}
e_messages = []
counter = 1
has_error = False
checkin_partners = False
if kw.get("folio_id"):
folio = request.env["pms.folio"].browse(int(kw.get("folio_id")))
@@ -416,26 +414,76 @@ class PortalPrecheckin(CustomerPortal):
"mobile": kw.get("mobile-" + str(counter)),
"email": kw.get("email-" + str(counter)),
}
lastname = True if kw.get("lastname-" + str(counter)) else False
firstname = True if kw.get("firstname-" + str(counter)) else False
lastname2 = True if kw.get("lastname2-" + str(counter)) else False
if not checkin.partner_id and (lastname or firstname or lastname2):
ResPartner = request.env["res.partner"]
res_partner = ResPartner.create(values)
values.update(
{
"partner_id": res_partner.id,
}
)
elif checkin.partner_id:
res_partner = checkin.partner_id
res_partner.write(values)
checkin.write(values)
error, error_message = self.form_validate(values)
errors.update({counter: error})
if error_message:
for e in error_message:
e_messages.append(e)
has_error = True
else:
lastname = True if kw.get("lastname-" + str(counter)) else False
firstname = True if kw.get("firstname-" + str(counter)) else False
lastname2 = True if kw.get("lastname2-" + str(counter)) else False
if not checkin.partner_id and (lastname or firstname or lastname2):
ResPartner = request.env["res.partner"]
res_partner = ResPartner.create(values)
values.update(
{
"partner_id": res_partner.id,
}
)
elif checkin.partner_id:
res_partner = checkin.partner_id
res_partner.write(values)
checkin.write(values)
counter = counter + 1
values = {"no_breadcrumbs": True}
if has_error:
for e in errors:
error = errors[e]
values.update({"error": error})
values.update(
{
"error_message": e_messages,
}
)
else:
values.update(
{
"success": True,
}
)
if kw.get("folio_id"):
folio = request.env["pms.folio"].browse(int(kw.get("folio_id")))
values.update(
{
"folio": folio,
}
)
return request.render("pms.portal_my_folio_precheckin", values)
elif kw.get("reservation_id"):
reservation = request.env["pms.reservation"].browse(
int(kw.get("reservation_id"))
)
values.update(
{
"reservation": reservation,
}
)
return request.render("pms.portal_my_reservation_precheckin", values)
def form_validate(self, data):
error = dict()
error_message = []
if data["mobile"]:
if not re.match(
r"^(\d{3}[\-\s]?\d{2}[\-\s]?\d{2}[\-\s]?\d{2}[\-\s]?|"
r"\d{3}[\-\s]?\d{3}[\-\s]?\d{3})$",
data["mobile"],
):
error["mobile"] = error
error_message.append("Invalid phone")
if data["document_number"]:
if data["document_type"] == "D":
if not re.match(r"^\d{8}[ -]?[a-zA-Z]$", data["document_number"]):
@@ -504,5 +552,4 @@ class PortalPrecheckin(CustomerPortal):
if data["email"] and not tools.single_email_re.match(data["email"]):
error["email"] = "error"
error_message.append("Email format is wrong")
return error, error_message

View File

@@ -28,6 +28,7 @@
for="firstname"
>Name</label>
<input
t-att-class="error"
type="text"
name="firstname"
t-attf-class="form-control"
@@ -66,7 +67,7 @@
<input
type="date"
name="birthdate_date"
t-attf-class="form-control"
t-attf-class="form-control #{error.get('birthdate_date') and 'is-invalid' or ''}"
t-att-value="birth_date or checkin_partner.birthdate_date"
/>
</div>
@@ -112,7 +113,7 @@
<input
type="text"
name="document_number"
t-attf-class="form-control"
t-attf-class="form-control #{error.get('document_number') and 'is-invalid' or ''}"
t-att-value="doc_number or checkin_partner.document_number"
/>
</div>
@@ -124,7 +125,7 @@
<input
type="date"
name="document_expedition_date"
t-attf-class="form-control"
t-attf-class="form-control #{error.get('document_expedition_date') and 'is-invalid' or ''}"
t-att-value="doc_exp_date or checkin_partner.document_expedition_date"
/>
</div>
@@ -134,9 +135,9 @@
for="mobile"
>Mobile</label>
<input
type="number"
type="phone"
name="mobile"
t-attf-class="form-control"
t-attf-class="form-control #{error.get('mobile') and 'is-invalid' or ''}"
t-att-value="mobile or checkin_partner.mobile"
/>
</div>
@@ -145,7 +146,7 @@
<input
type="email"
name="email"
t-attf-class="form-control"
t-attf-class="form-control #{error.get('email') and 'is-invalid' or ''}"
t-att-value="email or checkin_partner.email"
/>
</div>
@@ -182,6 +183,17 @@
action="/my/precheckin/folio_reservation"
method="get"
>
<div
t-if="error_message"
class="alert alert-danger"
role="alert"
>
<t t-foreach="error_message" t-as="err"><t t-esc="err" /><br
/></t>
</div>
<div t-if="success" class="alert alert-success" role="alert">
<span>Data saved successfully!</span>
</div>
<t
t-foreach="reservation.checkin_partner_ids"
t-as="checkin_partner"
@@ -218,7 +230,6 @@
t-att-name="'firstname-' + str(count)"
t-attf-class="form-control"
t-att-value="firstname or checkin_partner.firstname"
t-att-id="idcheckin"
/>
</div>
<div t-attf-class="form-group col-xl-6">
@@ -231,7 +242,6 @@
t-att-name="'lastname-' + str(count)"
t-attf-class="form-control"
t-att-value="lastname or checkin_partner.lastname"
t-att-id="idcheckin"
/>
</div>
<div t-attf-class="form-group col-xl-6">
@@ -292,6 +302,9 @@
t-att-value="document_type or checkin_partner.document_type"
>
<option value="D">DNI</option>
<option
value="P"
>Passport</option>
<option
value="C"
>Driving License</option>
@@ -336,7 +349,7 @@
for="mobile"
>Mobile</label>
<input
type="number"
type="phone"
t-att-name="'mobile-'+ str(count)"
t-attf-class="form-control"
t-att-value="mobile or checkin_partner.mobile"
@@ -416,6 +429,17 @@
action="/my/precheckin/folio_reservation"
method="get"
>
<div
t-if="error_message"
class="alert alert-danger"
role="alert"
>
<t t-foreach="error_message" t-as="err"><t t-esc="err" /><br
/></t>
</div>
<div t-if="success" class="alert alert-success" role="alert">
<span>Data saved successfully!</span>
</div>
<t t-foreach="folio.reservation_ids" t-as="reservation">
<t t-set="count_reservation" t-value="0" />
<tr class="bg-light">
@@ -475,8 +499,7 @@
type="text"
t-att-name="'firstname-' + str(count)"
t-attf-class="form-control"
t-att-value="firstname or checkin_partner.firstname"
t-att-id="idcheckin"
t-att-value="firstname or (checkin_partner.firstname)"
/>
</div>
<div
@@ -491,7 +514,6 @@
t-att-name="'lastname-' + str(count)"
t-attf-class="form-control"
t-att-value="lastname or checkin_partner.lastname"
t-att-id="idcheckin"
/>
</div>
<div
@@ -562,6 +584,9 @@
<option
value="D"
>DNI</option>
<option
value="P"
>Passport</option>
<option
value="C"
>Driving License</option>
@@ -612,9 +637,9 @@
for="mobile"
>Mobile</label>
<input
type="number"
type="phone"
t-att-name="'mobile-'+ str(count)"
t-attf-class="form-control"
t-attf-class="form-control #{error.get('mobile') and 'is-invalid' or ''}"
t-att-value="mobile or checkin_partner.mobile"
/>
</div>