+1 vote
2.0k views
in Coach Views by (130 points)
how can we validate any field from validation event handler, can any one help with example and syntax?

1 Answer

0 votes
by (30.6k points)

The coach's validate event (Coach > Behavior > Validation on older releases; on CSHS the Validation boundary of a coach or the client-side script bound to the coach's validation) runs before the coach is submitted; you mark controls invalid and the coach stays on page. Two flavours:

1. Client-side validation script of a CSHS coach (Coach > Properties > Validation > "validate with a client-side script"):

// runs in the browser when a button fires a boundary event with "validate" enabled
var errors = [];
if (!tw.local.customer.email || !/^[^@]+@[^@]+\.[^@]+$/.test(tw.local.customer.email)) errors.push({ path: "tw.local.customer.email", message: "Enter a valid e-mail" });
if (tw.local.order.amount > 10000 && !tw.local.order.approver) errors.push({ path: "tw.local.order.approver", message: "Approver required above 10,000" });
// report: every control bound to the path shows the message (System Data "CoachValidation")
tw.system.coachValidation.clearValidationErrors();
errors.forEach(function (e) { tw.system.coachValidation.addValidationError(e.path, e.message); });
// returning errors keeps the coach open; no errors -> the flow continues

2. Control-level validation in the coach (UI Toolkit) - the Validate event of a control, or the button's handler before firing the boundary event:

// Button > On click
var ok = true;
if (${Email}.getData() === "") { ${Email}.setValid(false, "E-mail is required"); ok = false; } else ${Email}.setValid(true);
if (${Amount}.getData() > 10000) { ${Amount}.setValid(false, "Above the limit, needs approval"); ok = false; }
if (ok) ${Submit}.click(); else ${Status}.setText("Fix the highlighted fields");   // Submit = hidden button with the boundary event

Server-side (heritage services) the same object is tw.system.coachValidation.addValidationError("tw.local.x", "msg") inside the server script attached to the coach's validation. The path must be the exact binding path of the control (list items: tw.local.lines[2].qty).

References

Related questions

+1 vote
1 answer 4.9k views
0 votes
1 answer 1.6k views
0 votes
1 answer 2.3k views
0 votes
1 answer 1.2k views
asked Feb 13, 2021 by Manana (190 points)
0 votes
1 answer 723 views
0 votes
1 answer 2.7k views
0 votes
2 answers 3.7k views
0 votes
1 answer 2.5k views
0 votes
1 answer 1.5k views

723 questions

807 answers

98 comments

4.9k users

Join BPM Community Discord Channel

Welcome to BPM Tips Q&A, Community wiki/forum where you can ask questions and receive answers from other IBM BPM experts and members of the community. Users with 2000 points will automatically be promoted to expert level.
Created by Dosvak LLC
Our Youtube Channel
...