Overview

ActiveWAFL provides functionality on the client and server side to help deal with forms processing.

Client side Validation

Validate user-submitted data before it gets sent to your server using the FormValidator control. This practice not only helps prevent useless data being sent and processed, but it also saves the user time and hassle.

The FormValidator control is both a programattic tool that developers can use to determine the validity of user input and a visual interface that provides feedback to the user.
It can validate many different formats including those below.

STRING
Regular string
ALPHA
Alphabetic string
ALPHANUMERIC
Alphanumeric string
NUMERIC
Numeric
INTEGER
Whole number
EMAILADDRESS
Email address
IPV4ADDRESS
IP Address
URI
Uniform Resource Identifier
WEBURL
Uniform Resource Locator
PHONENUMBER
Any phone number
REGEX
Define a custom validation

You can add the validator anywhere you want it to appear visually.

Example: FormValidator control on an email address field
  1. <label>Email Address</label>
  2. <input type="text" id="EmailAddress" value="{$MODEL->GetFieldValue("DemoModel")->Get_EmailAddress()}" />
  3. {validator ElementId="EmailAddress" Type="EmailAddress" DataLabel="email address"}

FormValidators work good with property inputs

Example: The same example as above, but using a PropertyInput control to generate the HTML for us
  1. {property_input model=$MODEL->GetFieldValue("DemoModel") property="EmailAddress" label=true}
  2. {validator ElementId="EmailAddress" Type="EmailAddress" DataLabel="email address"}

Form validator demonstration.
To see all of the functionality:
Type some invalid data and then press submit.
Then type some valid data. (if you press submit with valid data, the page will reload)

Code used in the above demonstration
  1. <form>
  2.    <div class="Row">
  3.       <div class="Spans4">
  4.          <label for="TestInputFormFieldEmailAddress">Type an Email Address</label>
  5.          <input type="text" id="TestInputFormFieldEmailAddress" required />
  6.       </div>
  7.       <div class="Spans2">
  8.          {validator ElementId="TestInputFormFieldEmailAddress" Type="EmailAddress" DataLabel="email address"}
  9.       </div>
  10.    </div>
  11.    <div class="Row">
  12.       <div class="Spans4">
  13.          <label for="TestInputFormFieldIpAddress">Type an IP Address</label>
  14.          <input type="text" id="TestInputFormFieldIpAddress" required />
  15.       </div>
  16.       <div class="Spans2">
  17.          {validator ElementId="TestInputFormFieldIpAddress" Type="IPV4Address" DataLabel="IP address"}
  18.       </div>
  19.    </div>
  20.    <button>Submit</button>
  21. </form>
Validate a form with an email address input and an ip address input.
  1.    <div class="Auto Layout Grid Panel">
  2.       <div class="Row">
  3.          <div class="Spans6">
  4.             <label for="ValidatorExampleEmailInput">
  5.                Enter an email address&nbsp;
  6.                {validator ElementId="ValidatorExampleEmailInput" Type="EmailAddress" DataLabel="email address"}
  7.             </label>
  8.             <input type="text" name="ValidatorExampleEmailInput" id="ValidatorExampleEmailInput" required />
  9.          </div>
  10.          <div class="Spans6">
  11.             <label for="ValidatorExampleIpInput">
  12.                Enter an ip address&nbsp;
  13.                {validator ElementId="ValidatorExampleIpInput" Type="IpV4Address" DataLabel="IP address"}
  14.             </label>
  15.             <input type="text" name="ValidatorExampleIpInput" id="ValidatorExampleIpInput" required />
  16.          </div>
  17.       </div>
  18.       <div style="text-align: center;">
  19.          <button>Submit Form</button>
  20.       </div>
  21.    </div>
  22. </form>
Demonstration of the Validator control on a form with email address and ip address fields

All arguments for the validator control

Hypothetical full usage example
  1. {validator ElementId="SomeInputId" Type="Regex" DataLabel="Blip Pattern" MinLength="1" MaxLength="40" DecimalPrecision="4" Regex="[0-9]*-[a-z]*" PassCssClass="Passed" FailCssClass="Failed" MinValue="10" MaxValue="20" PassCaption="Looks good" FailCaption="Looks bad"}
This example is non-sensical because many of these arguments only apply to specific validation types and would never actually be used together
PassCssClass
FailCssClass
MinValue
MaxValue
MinLength
MaxLength
DecimalPrecision
ClientValidator
ServerValidator
DataLabel
PassCaption
FailCaption
Regex

Retreieving Submitted Form Data

The request object that is passed to server-side controller actions contains methods for getting form input.

For example:
Simple Html form
  1. <form>
  2.    <label>First Name</label> <input name="FirstName" /><br>
  3.    <label>Last Name</label> <input name="LastName" />
  4. </form>
Server side code to get the form inputs
  1. public function DefaultAction(Request $request, IMvcWebApplication $app)
  2. {
  3.    $firstName = $request->GetInputString("FirstName");
  4.    $lastName = $request->GetInputString("LastName");
  5.    ...
  6. }
Other methods provided by the request object for getting user input
  • Typed (Built-in validation)
    • GetInputBoolean
    • GetInputDecimal
    • GetInputEmailAddress
    • GetInputInteger
    • GetInputIpAddress
    • GetInputMacAdress
    • GetInputRegex
    • GetInputString
    • GetInputUrl
  • General
    • GetAllInputs
    • GetInput
    • GetInputArray

Server side Validation and Sanitization

Validate and sanitize HTTP input on the server-side to ensure data-integrity and security.
  • Http Request object GetInput* methods
  • \DblEj\Data\Validator static helper methods

Validating form data in a Controller (or any Request Handler)

The Request object contains several methods for validating and sanitizing input from the current HTTP request.

Controller action getting valid input from a form submitted via HTTP POST variables and passing it on to the template renderer.
  1. public function DefaultAction(Request $request, IMvcWebApplication $app) {
  2.    $boolVal    = $request->GetInputBoolean("StayLoggedIn", Request::INPUT_POST, false);
  3.    $decVal     = $request->GetInputDecimal("Opacity", Request::INPUT_POST, .5);
  4.    $ipVal      = $request->GetInputIpAddress("RemoteAddress", Request::INPUT_POST, null);
  5.    $intVal     = $request->GetInputInteger("Age", Request::INPUT_POST, null);
  6.    $macVal     = $request->GetInputMacAdress("RouterMacAddress", Request::INPUT_POST, null);
  7.    $regVal     = $request->GetInputRegex("SerialNumber", ".....-.....-.....", Request::INPUT_POST, null);
  8.    $strVal     = $request->GetInputString("FirstName", Request::INPUT_POST, $sanitize, null);
  9.    $urlVal     = $request->GetInputUrl("FavoriteWebsite", Request::INPUT_POST, "http://activewafl.com");
  10.    $arrVal     = $request->GetInputArray("ContactOptions", Request::INPUT_POST, Validator::VALIDATE_NONE, Validator::SANITIZE_NONE, null, null, false);
  11.  
  12.    //send the valid data to the view (as an ArrayModel) to be displayed in a page.
  13.    $model = new ArrayModel([
  14.       "BoolVal"   =>$boolVal,
  15.       "DecVal"    =>$decVal,
  16.       "IpVal"     =>$ipVal,
  17.       "IntVal"    =>$intVal,
  18.       "MacVal"    =>$macVal,
  19.       "RegVal"    =>$regVal,
  20.       "StrVal"    =>$strVal,
  21.       "UrlVal"    =>$urlVal,
  22.       "ArrVal"    =>$arrVal
  23.    ]);
  24.  
  25.    return $this->createResponseFromRequest($request, $app, $model);
  26. }