1. Go to this page and download the library: Download hasan-22/form-validator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class Email implements \Validation\ValidationInterface{
private array $formData;
private array $errorMessage;
private string $field;
private string $additional;
public function validate(){
if(!filter_var($this->formData[$this->field], FILTER_VALIDATE_EMAIL)){
return $this->errorMessage;
}
}
public function formData(array $formData){
$this->formData = $formData;
}
public function message(string $errorMessage){
$this->errorMessage = [$this->field => empty($errorMessage) ? "The `{$this->field}` field is not an email" : $errorMessage];
}
public function field(string $field){
$this->field = $field;
}
public function additionalData(string $additional)
{
$this->additional = $additional;
}
}
$formData = ['email'=>'[email protected]'];
$result = \Validation\Validator::validate(
[
'email'=>[
'rules'=>['Required','Email']
],
],$data);
//Output:
//[]
//If all validation passes, you get an empty array