{"id":787,"date":"2013-05-21T15:30:05","date_gmt":"2013-05-21T20:30:05","guid":{"rendered":"http:\/\/xlinesoft.com\/blog\/?p=787"},"modified":"2026-08-22T17:03:01","modified_gmt":"2026-08-22T22:03:01","slug":"validation-in-phprunner-and-asprunnerpro-applications","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2013\/05\/21\/validation-in-phprunner-and-asprunnerpro-applications\/","title":{"rendered":"Data Validation in PHPRunner: Client-Side and Server-Side Options"},"content":{"rendered":"<p>Data validation helps prevent incomplete, incorrectly formatted or otherwise invalid data from being saved. PHPRunner provides several ways to validate user input, from built-in field rules to custom JavaScript and server-side validation.<\/p>\n<p>The right approach depends on what you need to check. Simple requirements such as mandatory fields can usually be handled with built-in validation. More complex business rules may require custom code.<\/p>\n<p>A useful rule is to distinguish between <strong>user experience<\/strong> and <strong>data integrity<\/strong>. Client-side validation gives users immediate feedback, but important rules should also be enforced on the server before data is saved.<\/p>\n<p><!--more--><\/p>\n<h2>1. Built-in validation<\/h2>\n<p>For common validation requirements, start with PHPRunner&#8217;s built-in field validation settings. They require no custom code and provide immediate feedback while the user is entering data.<\/p>\n<p>Depending on the field and its configuration, you can use built-in validation for requirements such as:<\/p>\n<ul>\n<li>making a field required;<\/li>\n<li>validating numbers and other expected data types;<\/li>\n<li>checking email addresses and other common formats;<\/li>\n<li>limiting acceptable values according to the field configuration.<\/li>\n<\/ul>\n<p>Built-in validation should normally be your first choice when it already covers the requirement. Custom code is most useful when the rule is specific to your application.<\/p>\n<h2>2. Validation with regular expressions<\/h2>\n<p>Regular expressions are useful when a value must follow a particular format.<\/p>\n<p>For example, suppose an internal code must contain exactly three uppercase letters followed by four digits:<\/p>\n<pre>^[A-Z]{3}[0-9]{4}$<\/pre>\n<p>This would accept values such as:<\/p>\n<pre>ABC1234<\/pre>\n<p>and reject values that do not follow the required structure.<\/p>\n<p>Regular expressions work well for structured identifiers, account numbers, reference codes and similar values where the format can be described precisely.<\/p>\n<p>Avoid making a regular expression unnecessarily complicated. If the validation rule depends on other fields, database values or application logic, custom validation is usually easier to understand and maintain.<\/p>\n<h2>3. Custom client-side validation<\/h2>\n<p>For rules that are not covered by the standard validation options, you can add JavaScript validation to the Add or Edit page.<\/p>\n<p>For example, suppose an order has <strong>StartDate<\/strong> and <strong>EndDate<\/strong> fields and the end date cannot be earlier than the start date.<\/p>\n<p>A custom client-side check can compare the two values and display an error before the form is submitted.<\/p>\n<p>The exact code depends on the controls and data types used on your page, but the important point is that this validation runs in the browser. The user receives feedback immediately and can correct the value without submitting the form first.<\/p>\n<p>Client-side validation is especially useful for:<\/p>\n<ul>\n<li>comparing two fields;<\/li>\n<li>checking combinations of values;<\/li>\n<li>providing immediate feedback;<\/li>\n<li>preventing obviously invalid forms from being submitted.<\/li>\n<\/ul>\n<p>However, client-side validation should not be the only place where an important business rule is enforced. Browser-side code can be bypassed, so critical checks should also be performed on the server.<\/p>\n<h2>4. Server-side validation before saving<\/h2>\n<p>Use the <strong>Before record added<\/strong> and <strong>Before record updated<\/strong> events when a rule must be enforced before data reaches the database.<\/p>\n<p>For example, suppose the <strong>Quantity<\/strong> field must always contain a positive value.<\/p>\n<p><strong>Before record added:<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"php\" name=\"mshighlighter\" >\r\nif ($values[\"Quantity\"] <= 0) {\r\n    $message = \"Quantity must be greater than zero.\";\r\n    return false;\r\n}\r\n\r\nreturn true;\r\n[\/php]\r\n\r\nThe same validation can be added to <strong>Before record updated<\/strong> if the rule must also apply when existing records are edited.\r\n\r\nReturning <strong>false<\/strong> prevents the record from being saved. The message explains what the user needs to correct.\r\n\r\nThis type of validation is appropriate for rules that must always be enforced regardless of what happens in the browser.\r\n\r\n<h2>5. Validate against existing database data<\/h2>\r\n\r\nSome rules require checking other records in the database.\r\n\r\nFor example, suppose an application stores an employee code and that code must be unique. Before saving the record, you can query the database to see whether the value already exists.\r\n\r\n[php]\r\n$sql = \"select EmployeeID from Employees where EmployeeCode = \" .\r\n    db_prepare_string($values[\"EmployeeCode\"]);\r\n\r\n$rs = DB::Query($sql);\r\n$data = $rs->fetchAssoc();\r\n\r\nif ($data) {\r\n    $message = \"This employee code is already in use.\";\r\n    return false;\r\n}\r\n\r\nreturn true;<\/textarea><\/pre>\n<\/div>\n<p>For an Edit page, remember that a uniqueness check normally needs to exclude the record currently being edited. Otherwise, the existing value can appear to be a duplicate of itself.<\/p>\n<p>Also consider enforcing truly unique values with a <strong>unique constraint or unique index in the database<\/strong>. Application validation can provide a better error message, while the database constraint provides the final protection against duplicates.<\/p>\n<h2>6. Validate relationships between fields<\/h2>\n<p>Server-side validation is also useful when several fields must be considered together.<\/p>\n<p>For example, suppose a discount cannot exceed the order total:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"php\" name=\"mshighlighter\" >\r\nif ($values[\"Discount\"] > $values[\"OrderTotal\"]) {\r\n    $message = \"Discount cannot be greater than the order total.\";\r\n    return false;\r\n}\r\n\r\nreturn true;<\/textarea><\/pre>\n<\/div>\n<p>Rules like this are usually easier to understand as normal code than as complex regular expressions.<\/p>\n<p>The same principle applies to date ranges, minimum and maximum values, status transitions and other business rules involving multiple fields.<\/p>\n<h2>7. Client-side or server-side?<\/h2>\n<p>In many applications, the best solution is to use both.<\/p>\n<p><strong>Client-side validation<\/strong> is best for immediate feedback. Users can see a problem before submitting the form, which makes the application faster and easier to use.<\/p>\n<p><strong>Server-side validation<\/strong> is best for enforcing rules that must never be bypassed. It is also required when validation depends on database data or other information that is only available on the server.<\/p>\n<p>A typical application might therefore:<\/p>\n<ol>\n<li>mark required fields using PHPRunner&#8217;s built-in validation;<\/li>\n<li>use client-side validation to provide immediate feedback for more complex input;<\/li>\n<li>repeat critical business-rule checks in Before record added or Before record updated;<\/li>\n<li>use database constraints where the database itself should guarantee data integrity.<\/li>\n<\/ol>\n<h2>Keep validation close to the rule it protects<\/h2>\n<p>Not every validation requirement needs custom code. Start with PHPRunner&#8217;s built-in options and add custom validation only when the application&#8217;s rules require it.<\/p>\n<p>For important data, think in layers. Browser-side validation improves the user experience, server-side validation protects the application logic, and database constraints can provide an additional level of protection for rules that must always hold.<\/p>\n<p>This approach keeps validation easier to understand while making it much harder for invalid data to reach the database.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data validation helps prevent incomplete, incorrectly formatted or otherwise invalid data from being saved. PHPRunner provides several ways to validate user input, from built-in field rules to custom JavaScript and server-side validation. The right approach depends on what you need to check. Simple requirements such as mandatory fields can usually be handled with built-in validation. More complex business rules may require custom code. A useful rule is to distinguish between user experience and data integrity. Client-side validation gives users immediate feedback, but important rules should&#8230;<span class=\"clearfix clearfix-post\"><\/span><a href=\"https:\/\/xlinesoft.com\/blog\/2013\/05\/21\/validation-in-phprunner-and-asprunnerpro-applications\/\" class=\"more-link\">Continue Reading <span class=\"screen-reader-text\">&#8220;Data Validation in PHPRunner: Client-Side and Server-Side Options&#8221;<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,3,8],"tags":[],"class_list":["post-787","post","type-post","status-publish","format-standard","hentry","category-php-category","category-php-code-generator","category-php-form-generator","category-tutorials"],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/comments?post=787"}],"version-history":[{"count":35,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/787\/revisions"}],"predecessor-version":[{"id":3462,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/787\/revisions\/3462"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}