{"id":3397,"date":"2011-09-23T09:15:00","date_gmt":"2011-09-23T17:15:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=3397"},"modified":"2011-09-23T09:21:56","modified_gmt":"2011-09-23T17:21:56","slug":"primalforms-2011-validating-the-form-part-4","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2011\/09\/23\/primalforms-2011-validating-the-form-part-4\/","title":{"rendered":"PrimalForms 2011: Validating the Form &#8211; Part 4"},"content":{"rendered":"<p>In <a href=\"http:\/\/www.sapien.com\/blog\/2011\/09\/21\/primalforms-2011-validating-the-form-part-3\/\">Part 3<\/a> we discussed validating techniques. Now in the final part of this series we will discuss how to validate the form as a whole.  <\/p>\n<p><strong>How to Trigger Validation when the Form is Closing:<\/strong><\/p>\n<p>If you have worked with validation in the past, you may have noticed that the unfocused controls do not trigger validation when the form closes. To get around the issue, you can explicitly tell the form to validate its child controls and react accordingly.<\/p>\n<p>A good place to trigger the validation is in the form\u2019s FormClosing event:<\/p>\n<pre><span style=\"color: #8b0000\">$form1_FormClosing<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.FormClosingEventHandler]{\n<\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.FormClosingEventArgs]<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #008000\">#Validate only on OK Button<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$form1<\/span><span style=\"color: #000000\">.DialogResult <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">\"OK\"<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #008000\">#Validate the Child Control and Cancel if any fail<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Cancel <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-not<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$form1<\/span><span style=\"color: #000000\">.ValidateChildren()    \n    }\n}<\/span><\/pre>\n<p>In the FormClosing event, we are checking if the user pressed the OK button, which has its DialogResult property set to \u201cOK\u201d (See <a href=\"http:\/\/www.sapien.com\/blog\/2011\/06\/07\/primalforms-2011-spotlight-on-the-button-control\/\">PrimalForms 2011: Spotlight on the Button Control<\/a> blog article for more details). By checking the DialogResult, we can bypass validation and allow the form to close when the user clicks on the cancel button or the X button in the upper right hand corner of the form\u2019s window. Next we call the form\u2019s ValidateChildren method which will trigger validation for all the controls contained in the form. The ValidateChildren method returns <em>False<\/em> if any of the controls fail validation. In the case where there is a validation failure, the $_.Cancel property is set to <em>True<\/em> and as a result prevents the form from closing.<\/p>\n<p>Note: Preventing the form from closing is important when you are using multiple forms and are dependent on the returned data from the one form to be formatted correctly. <\/p>\n<p><strong>Handing Form Closing Validation without Canceling in the Validation event:<\/strong><\/p>\n<p>In the case where you are not setting the Validating $_.Cancel to <em>True,<\/em> which allows the control to lose focus, it is recommend to set a script scope variable that will flag when a control fails validation. The following is the modified Validation event for the control:<\/p>\n<pre><span style=\"color: #8b0000\">$textboxName_Validating<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.ComponentModel.CancelEventHandler]{\n\n    <\/span><span style=\"color: #008000\">#Check if the Name field is empty<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #8b0000\">$result<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Validate-IsEmptyTrim<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$textboxName<\/span><span style=\"color: #000000\">.Text    \n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$result<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #008000\">#Mark a failure only if the Validation failed<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$script:ValidationFailed<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #008000\">#Display an error message<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$errorprovider1<\/span><span style=\"color: #000000\">.SetError(<\/span><span style=\"color: #8b0000\">$textboxName<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #ff0000\">\"Please enter your name.\"<\/span><span style=\"color: #000000\">);\n    }\n    <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\">\n    {\n        <\/span><span style=\"color: #008000\">#Clear the error message<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$errorprovider1<\/span><span style=\"color: #000000\">.SetError(<\/span><span style=\"color: #8b0000\">$textboxName<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #ff0000\">\"\"<\/span><span style=\"color: #000000\">);\n    }\n}<\/span><\/pre>\n<p>The updated FormClosing event:<\/p>\n<pre><span style=\"color: #8b0000\">$form1_FormClosing<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.FormClosingEventHandler]{\n<\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.FormClosingEventArgs]<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #008000\">#Validate only on OK Button<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$form1<\/span><span style=\"color: #000000\">.DialogResult <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">\"OK\"<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #008000\">#Init the Validation Failed Variable<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$script:ValidationFailed<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$false<\/span><span style=\"color: #000000\"> \n        <\/span><span style=\"color: #008000\">#Validate the Child Control and Cancel if any fail<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$form1<\/span><span style=\"color: #000000\">.ValidateChildren()    \n        <\/span><span style=\"color: #008000\">#Cancel if Validation Failed<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Cancel <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$script:ValidationFailed<\/span><span style=\"color: #000000\">\n    }\n}\n<\/span><\/pre>\n<p>The updated FormClosing event initializes the <span style=\"color: #8b0000\">$script:ValidationFailed <\/span>variable to False. Any control that fails validation should set the variable to True, thus preventing the form from closing.<\/p>\n<p><strong><em>Helpful Validation Functions:<\/em><\/strong><\/p>\n<p>Finally, here are some common validation functions that you may find helpful:<\/p>\n<pre><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Validate-IsEmail<\/span><span style=\"color: #000000\"> ([string]<\/span><span style=\"color: #8b0000\">$Email<\/span><span style=\"color: #000000\">)\n{\n    <\/span><span style=\"color: #0000ff\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$Email<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-match<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">\"^(?(\"\")(\"\".+?\"\"@)|(([0-9a-zA-Z]((\\.(?!\\.))|\"<\/span><span style=\"color: #0000ff\">+<\/span><span style=\"color: #000000\">`\n                        <\/span><span style=\"color: #ff0000\">\"[-!#\\$%&amp;'\\*\\+\/=\\?\\^`\\{\\}\\|~\\w])*)(?&lt;=[0-9a-zA-Z])@))\"<\/span><span style=\"color: #0000ff\">+<\/span><span style=\"color: #000000\">`\n                        <\/span><span style=\"color: #ff0000\">\"(?(\\[)(\\[(\\d{1,3}\\.){3}\\d{1,3}\\])|(([0-9a-zA-Z][-\\w]*\"<\/span><span style=\"color: #0000ff\">+<\/span><span style=\"color: #000000\">`\n                        <\/span><span style=\"color: #ff0000\">\"[0-9a-zA-Z]\\.)+[a-zA-Z]{2,6}))$\"<\/span><span style=\"color: #000000\">\n}\n\n<\/span><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Validate-IsURL<\/span><span style=\"color: #000000\"> ([string]<\/span><span style=\"color: #8b0000\">$Url<\/span><span style=\"color: #000000\">)\n{\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$Url<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$null<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #0000ff\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$false<\/span><span style=\"color: #000000\">    \n    }\n    \n    <\/span><span style=\"color: #0000ff\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$Url<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-match<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">\"^(ht|f)tp(s?)\\:\\\/\\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*\"<\/span><span style=\"color: #0000ff\">+<\/span><span style=\"color: #000000\">`\n                        <\/span><span style=\"color: #ff0000\">\"(:(0-9)*)*(\\\/?)([a-zA-Z0-9\\-\\.\\?\\,\\'\\\/\\\\\\+&amp;amp;%\\$#_]*)?$\"<\/span><span style=\"color: #000000\">\n}\n\n\n<\/span><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Validate-IsIP<\/span><span style=\"color: #000000\"> ([string] <\/span><span style=\"color: #8b0000\">$IP<\/span><span style=\"color: #000000\">)\n{\n    <\/span><span style=\"color: #0000ff\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$IP<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-match<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">\"\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.\"<\/span><span style=\"color: #0000ff\">+<\/span><span style=\"color: #000000\">`\n                    <\/span><span style=\"color: #ff0000\">\"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.\"<\/span><span style=\"color: #0000ff\">+<\/span><span style=\"color: #000000\">`\n                    <\/span><span style=\"color: #ff0000\">\"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.\"<\/span><span style=\"color: #0000ff\">+<\/span><span style=\"color: #000000\">`\n                    <\/span><span style=\"color: #ff0000\">\"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b\"<\/span><span style=\"color: #000000\">\n}\n\n<\/span><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Validate-IsEmptyTrim<\/span><span style=\"color: #000000\"> ([string]<\/span><span style=\"color: #8b0000\">$Text<\/span><span style=\"color: #000000\">)\n{\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$text<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$null<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-or<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$text<\/span><span style=\"color: #000000\">.Trim().Length <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #000000\">0<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #0000ff\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">    \n    }\n    \n    <\/span><span style=\"color: #0000ff\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$false<\/span><span style=\"color: #000000\">\n}\n\n<\/span><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Validate-IsEmpty<\/span><span style=\"color: #000000\"> ([string]<\/span><span style=\"color: #8b0000\">$Text<\/span><span style=\"color: #000000\">)\n{\n    <\/span><span style=\"color: #0000ff\">return<\/span><span style=\"color: #000000\"> [string]<\/span><span style=\"color: #0000ff\">::<\/span><span style=\"color: #000000\">IsNullOrEmpty(<\/span><span style=\"color: #8b0000\">$Text<\/span><span style=\"color: #000000\">)\n}\n\n<\/span><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Validate-IsDate<\/span><span style=\"color: #000000\"> ([string]<\/span><span style=\"color: #8b0000\">$Date<\/span><span style=\"color: #000000\">)\n{\n    <\/span><span style=\"color: #0000ff\">return<\/span><span style=\"color: #000000\"> [DateTime]<\/span><span style=\"color: #0000ff\">::<\/span><span style=\"color: #000000\">TryParse(<\/span><span style=\"color: #8b0000\">$Date<\/span><span style=\"color: #000000\">,[ref](<\/span><span style=\"color: #0000ff; font-weight: bold\">New-Object<\/span><span style=\"color: #000000\"> System.DateTime))    \n}<\/span><\/pre>\n<p>[Note: These functions have been included as snippets since v2.0.13&nbsp; service release]<\/p>\n<p>You can download the <a href=\"http:\/\/www.sapien.com\/downloads#Sample Scripts\/PrimalForms 2011\/ValidationSamples.zip\">Validation Sample Forms<\/a> from our the <a href=\"http:\/\/sapien.com\/downloads\">Downloads<\/a> section.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/08\/Validation-Sample-Form19.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Validation-Sample-Form19\" border=\"0\" alt=\"Validation-Sample-Form19\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/08\/Validation-Sample-Form19_thumb.png\" width=\"304\" height=\"224\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Part 3 we discussed validating techniques. Now in the final part of this series we will discuss how to validate the form as a whole. How to Trigger Validation when the Form is Closing: If you have worked with validation in the past, you may have noticed that the unfocused controls do not trigger [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[509,575,25],"tags":[313,28,633,680,327],"class_list":["post-3397","post","type-post","status-publish","format-standard","hentry","category-primalforms-software-news","category-primalforms-2011-software-news","category-windows-powershell","tag-forms","tag-powershell","tag-primalforms-2011","tag-validation","tag-winform"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=3397"}],"version-history":[{"count":5,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3397\/revisions"}],"predecessor-version":[{"id":3478,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3397\/revisions\/3478"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=3397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=3397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=3397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}