Client-Side validation and Validation types
PHP-Bootstrap-Form validation is achieved in a two step process. The first step is to apply validation rules to form elements via the element's validation attribute. Some elements including Captcha, Color, Date, Email, jQueryUIDate, Month, Number, Url, and Week have validation rules applied by default.
PHP-Bootstrap-Form supports 7 types of validation rules: AlphaNumeric, Captcha, Date, Email, Numeric, RegExp, Required, and Url,
|Validation_AlphaNumeric ()||AlphaNumeric validation class will verify that the element's submitted value contains only letters, numbers, underscores, and/or hyphens|
|Validation_Numeric ()||Number element applies the Numeric validation rule by default. If supported, HTML5 validation will also be provided client-side.|
|Validation_Email ()||Email element applies the Email validation rule by default. If supported, HTML5 validation will also be provided client-side.|
|Validation_Date ()||Date element applies the RegExp validation rule by default - ensuring the following date format YYYY-MM-DD is adhered to.|
|Validation_RegExp ($regExp, $errorMessage)||RegExp validation class provides the means to apply custom validation to an element. Its constructor includes two parameters: the regular expression pattern to test and the error message to display if the pattern is not matched.|
|Validation_Captcha ()||Captcha element applies the Captcha validation, which uses reCaptcha's anti-bot service to reduce spam submissions.|
|Validation_Url ()||The Url element applies the Url validation rule by default. If supported, HTML5 validation will also be provided client-side.|
Secondly, you need to call Form::isValid() static method once the form's data has been submitted. This function will return true/false. If false is returned, it indicates that one or more errors occurred. You will then need to redirect users back to the form to correct and resubmit.
The validation process for an ajax submission also differs slightly from that of a standard submission. If the form's isValid method returns false, you will need to invoke the renderAjaxErrorResponse method, which returns a json response containing the appropriate error messages. These errors will then be displayed in the form so the user can correct and resubmit.
Below is an example of the isValid use.
The isValid method has a second, optional parameter that controls whether or not the form's submitted data is cleared from the PHP session if the form validates without errors. In the example above, false is passed allowing us to authenticate the potential user with the fictional isValidUser function. If the user's credentials are valid, the session data is cleared manually with the clearValues method, and we redirect the user to their profile page. If invalid credentials were supplied, we use the setError method to manually set a custom error message and redirect back to the login form so the user can resubmit.