1. Go to this page and download the library: Download bishalshrestha/fynix 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/ */
bishalshrestha / fynix example snippets
use Fynix\RuleSet;
use Fynix\ValidationHandler;
use Fynix\ValidationRegistry;
final class User
{
public string $firstName = '';
public string $email = '';
}
ValidationRegistry::register(
User::class,
static fn (RuleSet $rules): array => [
$rules->string('firstName')->min(2)->max(50),
$rules->email('email')->max(254),
],
);
$user = new User();
$user->firstName = 'A';
$user->email = 'not-an-email';
$errors = ValidationHandler::validate($user);
[
'firstName' => 'First Name is too short. This field must be at least 2 characters.',
'email' => 'Email must be a valid email address.',
]
use Fynix\Rule;
use Fynix\Rules\AllOf;
use Fynix\Rules\AnyOf;
use Fynix\Rules\Not;
$name = new AllOf([
Rule::string('name')->min(2),
Rule::string('name')->max(80),
]);
$contact = new AnyOf([
Rule::email('contact'),
Rule::phoneNumber('contact'),
]);
$status = new Not(
Rule::string('status')->in(['blocked']),
'This status is not allowed.',
);
use Fynix\ValidationError;
use Fynix\Validators\ValidatorBase;
final class EvenNumberValidator extends ValidatorBase
{
protected function validateValue(mixed $fieldValue): ?ValidationError
{
if (!is_int($fieldValue) || $fieldValue % 2 !== 0) {
return new ValidationError($this, "$this->name must be even.", 'number.even');
}
return null;
}
}