Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A HTML form validator written in PHP.
jridgewell/FormValidator
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
FormValidator allows you to create and validate forms using a simple rule based approach. It uses an API very similar to Rails’ ActiveRecord.
A form file is just a class that extends the \FormValidator\Form class In this example, the form validator checks if name isn’t empty
use \FormValidator\Form; use \FormValidator\Validation; class TestForm extends \FormValidator\Form < public function __construct() < $this->validations = array( // Contains a hash array of form elements "name" => Validation::presence() // name field must contain something ); > > ?>
require_once('test.form.php') $form = new TestForm(); /* Checks if the form has submitted then the form is checked for validation against the rules contained within the $validations array of TestForm returning the validated data if its successful */ if($form->hasPosted() && ($data = $form->validate())) < // Form passes validation, use the $data for validated POST data > else < // Form hasn't posted or hasn't passed validation, so we load our html file require_once('form.html.php'); > ?>
'input' method='POST'> error('name', 'There was an error'); ?> Please Enter your name: input('name'); ?>br/> $form->submit('Submit');?> form>
- If the form fails validation, by using the $form->input method, we preserve whatever value the user typed into that field (except for password fields)
- The form must have a field with the name attribute set to the name of the form class ( name=»TestForm» in our example). Using the $form->submit method takes care of this requirement.
composer require "jridgewell/form-validator:1.*"
Then just add require ‘vendor/autoload.php’; to any code that requires FormValidator.
The $validations array contains all the form fields and rules that need to pass, for the form to be valid. In the example above, it showed a single rule applying to one form element, but you can apply multiple rules to an element by using an array.
class TestForm extends Form< public function __construct() < $this->validations = array( 'name' => Validation::presence(), 'age' => array( //Specifiy multiple rules Validation::presence(), Validation::numericality() ) ); > > ?>
In our html file, if we wanted to show the errors for the validations, we could do the following:
'input' method='POST'> error('name', 'There was an error'); ?> Please Enter your name: input('name'); ?>br/> $form->error('age', 'This is an optional custom message about age'); ?> Please Enter your age: $form->input('age'); ?>br/> $form->submit('Submit');?> form> ?>
Most validations also support passing in an options array. This allows for custom messages, and can allow for a field to be optional (blank). Please see the validation for acceptable parameters.
class TestForm extends Form < public function __construct() < $this->validations = array( 'name' => Validation::length(array( 'minimum' => 0, 'maximum' => 100 )), 'age' => Validation::numericality(array( 'optional' => true, 'only_integer' => true )), 'username' => Validation::exclusion(array( 'admin', 'superuser' ), array( 'message' => 'You are not our master!' )) ); > > ?>
This field is always valid
This field must be accepted (truthy)
This field must be a valid email
This field’s number of characters must be in the supplied range. If no options are passed, this field will always be valid
This field must be a number
This field must not be empty
This field must be a valid url
Advanced Validations (require parameters)
This field must match the return value of $other_field_func. Useful for confirming a password in a second field.
This field must not be equal (==) to a value inside $array.
This field must match against the supplied $regex
This field must be equal (==) to a value inside $array.
This validation allows for a custom function to preform the field validation. It’s return value must be (===) true, or else it will use the return value as the field’s error message
Advanced Validation Examples
// TestForm.php class TestForm extends Form< public function __construct() < $this->validations = array( 'password' => Validation::confirmation(function() < return $_POST['password_confirmation']; >) ); > > ?>
// TestForm.php class TestForm extends Form< public function __construct() < $this->validations = array( 'usernames' => Validation::exclusion(array( 'admin', 'superuser' )) ); > > ?>
// TestForm.php class TestForm extends Form< public function __construct() < $this->validations = array( 'mp3Url' => Validation::format('/\.mp3$/') ); > > ?>
class TestForm extends Form< public function __construct() < $this->validations = array( 'usernames' => Validation::inclusion(array( 'Matt', 'Thor', 'Asa' )) ); > > ?>
This validation requires a (callable) callback. This callback is then provided with the submitted field data as it’s only parameter. The callback can either return true and the validation will pass, or return anything else and the return will be used as the error message for the field.
class TestForm extends Form < public function __construct() < $this->validations = array( 'checkCustom' => Validation::validateWith(function($val) < if ($val === 'supahSecret') < return true; > return (substr($val, 0, 2) == 'st'); >) ); > > ?>
About
A HTML form validator written in PHP.