How to stop server-side code running when client-side validation fails for a command button
Wednesday, January 2nd, 2008At times when developing UI functionality you will want to allow the user the ability to run a server side operation only if certain client side conditions are met. This can range from tasks such as checking the information entered on a form is sensible and realistic such as checking for a valid postcode or more sophisticated checks such as ensuring all data entered in a grid is valid before carrying out a dependent e-mail operation.
Normally this type of validation is carried out when a form is submitted and we can easily trap these conditions. Return false from a validation script and it will stop the submission and therefore the server side code, however if you try to do something similar from a command button it’s not so simple.
The routine method is to simply not allow the user the option of pressing the command button prior to all conditions being met - these conditions being calculated by a hidden check box (or series) and having the button’s visibility set dependent on these. i.e. When the conditions are okay the button is visible and when the conditions are not okay the button is not visible. Personally, I don’t really like this as it doesn’t give any opportunity for feeding back to the user to show why things may be wrong. From a design perspective it also creates a more cluttered view in the MBPM designer which can then lead to maintenance issues as a system grows. Also, one last minor and avoidable niggle, it can leave a “hole” in the form’s design.
I think it would be more beneficial to allow the the client-side script to perform processing and validation before deciding if it should hand control on to the server for server-side processing. Luckily this is quite simple to achieve.
To do this follow these 2 steps:
- When the form loads associate a new event handler with the command button
- Create the new event handler
When the form loads ensure it calls the initialisation function:
1: OnLoad=Init ()&Language=JScript
Create the initialisation function:
1: function Init()
2: {
3: //hook the command button to it's own event handler to allow
4: //validation before the server side operation is triggered
5: eworkData.FieldByName("btnCmd1").OnClick = btnCmd1Validate;
6: }
Now create the event handler:
1: function btnCmd1Validate()
2: {
3: try
4: {
5: var RESULT = doComplexValidation();
6:
7: if (RESULT==true)
8: {
9: ButtonClick(document.getElementById("btnCmd1"));
10: alert("Server-side functionality processed");
11: }
12: else
13: {
14: alert("Server-side functionality NOT processed");
15: }
16: }
17: catch (ex)
18: {
19: alert (ex.message);
20: }
21: }
Now when the the command button (btnCmd1) is clicked the new event handler is used. This allows the function doComplexValidation() to be called which determines if we carry out further processing. If control should be passed back to the server then we call the Metastorm function ButtonClick() passing the command button element itself as the argument to invoke the server-side.
This gives the opportunity for full validation, additional opportunities for user feedback and the potential for taking pre-emptive corrective action should you need to.
January 2nd, 2008 at 9:07 am
One solution to the ‘hole’ in the form design problem is to have two buttons, one that is shown and hidden as required, the other is always disabled and is behind the main button so only appears when the other button is hidden, thus giving the impression of disabling the main button. We have a library of functions for disabling buttons that we use for this. This does add to the maintenance issue though.
January 4th, 2008 at 10:17 pm
Why so complicated?? I just create a simple button and a check box. Check box doesn’t even need to be associated with a variable.
Button has both client and server side script
Client Side
Function
set check box to false
do validation
if successful set check box to true
Server Side
If check box = true do server side code
– OR –
If wanting to execute further client side script after server side things
Client Side
Function ButtonPress
set checkbox to false
set timer for 500ms to execute function
Function TimerFunc
if checkbox still false set timer for 500ms to this function
otherwise complete validation
Server Side
Do server side things
set field.chkBox = true
January 4th, 2008 at 10:33 pm
Thanks for this alternative Todd.
I’ve got to admit it is way simpler especially if your server side code is short. If the server side code contains more lines then I’d possibly go with the original version just because I dislike having to wrap many lines with conditionional statements or embedding many commands on one line in the designer. I guess this is preference though.
I’m not totally convinced by the arbitary delay to excute a client side script although I have and do use it in projects where the guaranteed reliability is not a huge issue but there are times when a slow network or a user can block this accidently or cause it not to run. Where 100% reliability isn’t an issue it’s certainly good solution to a gap in the software’s capability.I’ve just re-read the second part of your comment regarding running client side script after a server side event and I like it - next time I need something similar I’ll add it to the tool box of tricks. Thanks!March 12th, 2008 at 2:29 pm
Thanks for the tips. Very Useful for a beginner who is just learning the basics of ASP.NET