Download .zip |
Info | Example | View files (5) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2017-09-19 (24 days ago) | Not yet rated by the users | Total: Not yet counted | Not yet ranked |
Version | License | PHP version | Categories | |||
simpleformvalidator 1.0 | GNU General Publi... | 5 | HTML, PHP 5, Validation, Parsers |
Description | Author | ||||||||||||||
This class can validate forms with rules defined in the HTML. |
|
The purpose of SimpleFormValidator is to make user input validation secure, simple and fast.
No external rule declarations are necessary, or even possible.
SimpleFormValidator will instead extract the rules from the HTML using PHP's native DOMDocument class.
Just make sure you have valid HTML5 code, and it will work.
Three ingredients are needed to make it work:
The smallest example imaginable:
<?php
$form = new SimpleFormValidator('
<input type="email" name="your_email" required>
<input type="text" name="repeat_email" data-match="your_email">
');
if(!empty($_POST) && $form->validate($_POST)) {
/do stuff; send e-mail, log in, redirect, etc./
} ?>
<form method="post" action="example.php">
<?php $form->html(); ?>
</form>
>$_POST['your_email'] will be validated as an e-mail address. $_POST['your_email'] cannot be left empty. $_POST['repeat_email'] must match $_POST['your_email'], whether empty or not.
So, the user has entered a valid username and password, but they aren't correct.
That's when custom errors (triggerCustomError()) come in handy.
<?php
if(!empty($_POST) && $form->validate($_POST)) {
if($user = User::fetch($_POST["username"])) {
if($user->password == $_POST["password"]) {
$_SESSION["user"] = $user->getAttributes();
header("Location: welcome.php"); exit;
} else {
$form->triggerCustomError("password", "Password incorrect.");
}
} else {
$form->triggerCustomError("username", "User not found.");
}
} ?>
<form method="post" action="login.php">
<?php $form->html(); ?>
</form>
If for instance a user is logged in and wants to send an e-mail, you can pre-populate the form. (This saves a lot of time during development.)
<?php
if(!empty($_POST){
if($form->validate($_POST))){/.../}
}else{
$form->setValue("email",$_SESSION["user"]["email"]);
$form->setValue("first_name",$_SESSION["user"]["first_name"]);
} ?>
<form method="post" action="contact.php">
<?php $form->html(); ?>
</form>
The base class does not have that many validators, just the ones I've had use for lately, but that is not the end of the story.
Enter: Inheritance.
Create your own class, extending SimpleFormValidator, and add the validators you want. The existing ones can all be found in the original's constructor, if you need hints.
Like so:
class MyValidator extends SimpleFormValidator {
function __construct($html){
parent::__construct($html);
$this-> validators["number"]=function($name,$value,$node){};
$this-> validators["zip"]=function($name,$value,$node){};
/also add error messages to the sfv.texts_xx.php file/
}
}
I do not recommend editing SimpleFormValidator directly, since there may be updates in the future.
Files |
File | Role | Description |
---|---|---|
class.SimpleFormValidator.php | Class | Class source |
example.form.php | Aux. | Auxiliary script |
example.php | Example | Example script |
README.md | Doc. | Documentation |
sfv.texts_en.php | Aux. | Example script |
Version Control | Unique User Downloads | |||||||
100% |
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.