2. Overview
Objective
Learn how to create custom validation in ProdigyView.
Requirements
Understanding of validation
Understanding of anonymous functions
Estimated Time
8 Minutes
www.prodigyview.com
3. Follow Along With Code Example
1. Download a copy of the example code at
www.prodigyview.com/source.
2. Install the system in an environment you feel comfortable testing in.
3. Proceed to examples/util/Validation_Custom.php
4. What is custom validation?
Building off of the tutorial on validation basics, we learned
about the built in validation rules in ProdigyView. But there
is a plethora of things to validate that the PVValidator
does not have, such as credit card validation. How do we
overcome this problem?
The Answer
Add our own validation
5. Checking and Adding
The two methods to understand when writing custom
validation are addRule() and check().
addRule()
Adds a rule to the validator. The rule has a name and
parameters that define a rule. The rules can be written
using preg_match or anonymous functions.
Check()
Checks passed values against and should return true or
false.
6. Check Example
Our first example will be using the method check(). We
will use one of the built checks, integer, which checks if a
number is an int.
The rule to use when checking The value to check
Returns false because not an integer
7. Create Function
Here is a method in php you might not see very often,
create_function. This function is similar to anonymous
functions in that it can create a function on the fly. We are
also adding our first custom rule here.
1. Create the function 2. Set the type to function and set the function
3. Set the name of 4. Add the rule options 5. Execute rule and values
the rule
8. Preg_match
Preg_match is a easy way of validating data based on
formats. If you are new to preg_match, I suggest you
brush up on your regular expressions.
1. Set the type to preg_match 2. Set the regular expression to check
against
3. Set the name of the rule 4. Add the rules
5. Check value against rule
options
9. Anonymous Functions
Our last check can be accomplished using the
anonymous function/closure.
1. Create the closure 2. Set the type to ‘closure’ 3. Set the closure
3. Set the name of the 4. Add the rule options 5. Check values against
rule rule
10. Challenge!
Perform this optional challenge to better your understanding of
validation in ProdigyVIew.
1. Create a class with a static method.
2. Have that static method accept a check string and check if
that string contains the letters ‘pv_’
3. Create an anonymous function that takes the value passed
to it and checks it against the static method and returns the
result.
4. Add the anonymous function to the validator, and assign it
name.
5. Pass in string values to test.
11. API Reference
For a better understanding of Validation in ProdigyView, visit
the api by clicking on the link below.
PVValidator
More Tutorials
For more tutorials, please visit:
http://www.prodigyview.com/tutorials
www.prodigyview.com