You can build your own validation files for that are compatible with sFire Form Validator or sFire Uploaded File Validator . The advantage is that validation files can be used all over your application without writing duplicate validation rules.
All the validation files are saved into your module validation directory which is by default "modules/{YOUR-MODULE-NAME}/validators/". In this folder you can create other folder to group validation files.
Let's create an example validation file. Create a folder named "custom" and a file called "ValidationExample.php" within that folder.
namespace App\Validators\custom;
use sFire\Validator\RuleInterface;
use sFire\Validator\MatchRule;
class ValidatorExample implements RuleInterface {
use MatchRule;
public function __construct() {
$this -> setMessage('Invalid value');
}
//The isValid method should always return a boolean
public function isValid() {
if($this -> getValue() === 'John Smith') {
return true;
}
return false;
}
}
Note: The validation file is prefixed with "Validator". You can change this in the app.php
With the constructor you can set the error message to display when validation fails. A validation file always has a "isValid" method which should return a boolean true if validation passes and boolean false for validation failure.
Within the validation files, there are a couple of handy methods you can use to retrieve data, show the prefix, return the current value, etc.
Lets say we have a form like:
<form>
<input name="register[name]" value="John Smith">
<input type="submit">
</form>
Our validator would look like:
$validator = new Validator('register');
$validator -> load('custom.example');
//Apply the validation rule on a field
$validator -> field('name') -> required(true) -> example(5, 20);
To return the current value, you can use the "getValue" method. This method only works if you use the Form Validator. To return a uploaded file, you can use the "getFile" method which is described below.
public function isValid() {
if($this -> getValue() === 'John Smith') {
return true;
}
return false;
}
You can retrieve the prefix of a field name if a prefix has been set in the form or file validator constructor.
public function isValid() {
$prefix = $this -> getPrefix();
echo $prefix; //Outputs "register"
}
Retrieve all the parameters that have been set. For example, if you want to check if a posted field is between two numbers, you can retrieve them by calling the "getParameters" method.
public function isValid() {
$parameters = $this -> getParameters();
print_r($parameters); //Outputs "[5, 20]"
}
You should set the default error message in the constructor by calling the "setMessage" method. This can also be done in the "isValid" method to overwrite the error message.
public function __construct() {
$this -> setMessage('Invalid value');
}
public function isValid() {
$this -> setMessage('Other error message'); //Overwrites default error message set in the constructor
}
When you want to know if a field have been set as required or not, you can use the "getRequired" method. This method will return a boolean true or false.
public function isValid() {
$required = $this -> getRequired();
var_dump($required); //Outputs "true"
}
To retrieve the field name you can use the "getField" method.
public function isValid() {
$fieldname = $this -> getField();
echo $fieldname; //Outputs "register[name]"
}
Using the File Upload Validator, you can retrieve the current uploaded file by calling the "getFile" method.
public function isValid() {
$file = $this -> getFile();
print_r($file);
//Ouput similar to:
Array
(
[name] => 194613.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpYbu3tR
[error] => 0
[size] => 996876
)
}
To load a validation file, you should use the "load" method of the Validator. By default the Validator will check the "validation" folder. Folders are separated by a dot. In the case below the validator will load "modules/{YOUR-MODULE-NAME}/validators/custom/ValidatorExample.php".
$validator = new Validator();
$validator -> load('custom.example');
//Apply the validation rule on a field
$validator -> field('fieldname') -> example();
//If your validation rule requires parameters, you can set them like:
$validator -> field('fieldname') -> example(10, 20);
It is also possible to overwrite error messages that have been set in the validation file by calling the "setMessage" method of the validator.
//Set new error message for specific field name with "example" validation rule
$validator -> setMessage('fieldname') -> example('This is an example error message');
//Set new error message for all fieldnames with "example" validation rule
$validator -> setMessage() -> example('This is an example error message');
Sometimes you need to use data from for example a controller or mapper into your validation file. You can use the "store" method of the validator to temporary store data. Then you can use the "retrieve" method from within the validation file to return that value.
$validator = new Validator();
$validator -> load('custom.example');
$validator -> field('fieldname') -> example();
$validator -> store('name', 'John Smith');
Wihtin the validation file:
public function isValid() {
$name = $this -> retrieve('name')
echo $name; //Outputs "John Smith"
}