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

View File

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