Custom Validation PHP

1,535 views

Published on

Learn how to write custom validation rules in ProdigyView.

Published in: Technology, Economy & Finance
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,535
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Custom Validation PHP

  1. 1. Custom Validation
  2. 2. OverviewObjectiveLearn how to create custom validation in ProdigyView.Requirements Understanding of validation Understanding of anonymous functionsEstimated Time8 Minutes www.prodigyview.com
  3. 3. Follow Along With Code Example1. 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. 4. What is custom validation?Building off of the tutorial on validation basics, we learnedabout the built in validation rules in ProdigyView. But thereis a plethora of things to validate that the PVValidatordoes not have, such as credit card validation. How do weovercome this problem? The Answer Add our own validation
  5. 5. Checking and AddingThe two methods to understand when writing customvalidation are addRule() and check().addRule()Adds a rule to the validator. The rule has a name andparameters that define a rule. The rules can be writtenusing preg_match or anonymous functions.Check()Checks passed values against and should return true orfalse.
  6. 6. Check ExampleOur first example will be using the method check(). Wewill use one of the built checks, integer, which checks if anumber is an int. The rule to use when checking The value to check Returns false because not an integer
  7. 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 function3. Set the name of 4. Add the rule options 5. Execute rule and valuesthe rule
  8. 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 against3. Set the name of the rule 4. Add the rules 5. Check value against rule options
  9. 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 closure3. Set the name of the 4. Add the rule options 5. Check values againstrule rule
  10. 10. Challenge!Perform this optional challenge to better your understanding ofvalidation 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. 11. API ReferenceFor a better understanding of Validation in ProdigyView, visitthe api by clicking on the link below.PVValidator More Tutorials For more tutorials, please visit: http://www.prodigyview.com/tutorials www.prodigyview.com

×