1. Go to this page and download the library: Download julienlinard/php-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/ */
// Create validator with a specific locale
$validator = new Validator('en'); // English
$validator = new Validator('fr'); // French (default)
$validator = new Validator('es'); // Spanish
// Or change locale after creation
$validator = new Validator();
$validator->setLocale('en');
// Get current locale
$locale = $validator->getLocale(); // Returns 'en', 'fr', or 'es'
$validator = new Validator();
$validator->setCustomMessages([
'email.email' => 'Please provide a valid email address',
'password.min' => 'Password must be at least :min characters',
'name.
use JulienLinard\Validator\Rules\AbstractRule;
use JulienLinard\Validator\Validator;
class CustomRule extends AbstractRule
{
public function getName(): string
{
return 'custom';
}
public function validate(mixed $value, array $params = []): bool
{
// Your validation logic
return $value === 'expected';
}
protected function getDefaultMessage(): string
{
return 'The :field field is invalid.';
}
}
$validator = new Validator();
$validator->registerRule(new CustomRule());
$rules = ['field' => 'custom'];
use JulienLinard\Core\Controller\Controller;
use JulienLinard\Core\Form\FormResult;
use JulienLinard\Validator\Validator;
class UserController extends Controller
{
public function store()
{
$validator = new Validator();
$result = $validator->validate($_POST, [
'email' => 'r, $field));
}
}
return $this->view('users/create', ['formResult' => $formResult]);
}
// Use validated data
$validated = $result->getValidated();
// ...
}
}