1. Go to this page and download the library: Download mewtonium/vanguard 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/ */
mewtonium / vanguard example snippets
use App\Validation\Rules\Uppercase;
use Mewtonium\Vanguard\Rules\In;
use Mewtonium\Vanguard\Rules\MaxLength;
use Mewtonium\Vanguard\Rules\MinLength;
use Mewtonium\Vanguard\Rules\Email;
use Mewtonium\Vanguard\Rules\Between;
use Mewtonium\Vanguard\Rules\Required;
use Mewtonium\Vanguard\Vanguard;
class AccountSignupForm
{
use Vanguard;
public function __construct(
#[Required, MinLength(2), MaxLength(255)]
protected string $firstName,
#[Required(message: 'Please provide your last name.'), MinLength(2), MaxLength(255)]
protected string $lastName,
#[Required, Between(18, 99)]
protected int $age,
#[Required, Email]
protected string $email,
#[Required, In(['GB', 'FR', 'DE', 'ES', 'IT', 'IE', 'JP', 'ZH']), Uppercase]
protected string $country,
#[Required, GreaterOrEqual('2025-01-01')]
protected string $date,
) {
//
}
}
$data = [
'firstName' => 'Joe',
'lastName' => '',
'age' => 17,
'email' => 'joe.bloggs',
'country' => 'ch',
'date' => '2024-01-01',
];
$form = new AccountSignupForm(...$data);
$form->validate();
use Mewtonium\Vanguard\Rules\Alpha;
#[Alpha]
public string $name;
use Mewtonium\Vanguard\Rules\AlphaNum;
#[AlphaNum]
public string $username;
use Mewtonium\Vanguard\Rules\Between;
#[Between(1, 100)]
public int $numbers;
#[Between('2020-01-01', '2030-01-01')]
public string $date;
use Mewtonium\Vanguard\Rules\Boolean;
#[Boolean]
public mixed $active;
use Mewtonium\Vanguard\Rules\Email;
#[Email]
public string $email;
use Mewtonium\Vanguard\Rules\Equal;
#[Equal(30)]
public int $age;
#[Equal('2025-01-01')]
public string $date;
use Mewtonium\Vanguard\Rules\GreaterOrEqual;
#[GreaterOrEqual(30)]
public int $age;
#[GreaterOrEqual('2025-01-01')]
public string $date;
use Mewtonium\Vanguard\Rules\GreaterThan;
#[GreaterThan(30)]
public int $age;
#[GreaterThan('2025-01-01')]
public string $date;
use Mewtonium\Vanguard\Rules\In;
#[In(['GB', 'FR', 'DE'])]
public string $country;
use Mewtonium\Vanguard\Rules\Integer;
#[Integer]
public mixed $count;
use Mewtonium\Vanguard\Rules\LessOrEqual;
#[LessOrEqual(30)]
public int $age;
#[LessOrEqual('2030-01-01')]
public string $date;
use Mewtonium\Vanguard\Rules\LessThan;
#[LessThan(30)]
public int $age;
#[LessThan('2030-01-01')]
public string $date;
use Mewtonium\Vanguard\Rules\MaxLength;
#[MaxLength(255)]
public string $url;
#[MaxLength(10)]
public array $data;
use Mewtonium\Vanguard\Rules\MinLength;
#[MinLength(3)]
public string $name;
#[MinLength(3)]
public array $data;
use Mewtonium\Vanguard\Rules\Numeric;
#[Numeric]
public mixed $amount;
use Mewtonium\Vanguard\Rules\Regex;
#[Regex('/^[A-Z]{2}-\d{3}$/')]
public string $reference;
use Mewtonium\Vanguard\Rules\Required;
#[Required]
public string $name;
use Mewtonium\Vanguard\Rules\Url;
#[Url]
public string $website;
use Mewtonium\Vanguard\Rules\BaseRule;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
final class Uppercase extends BaseRule
{
public function __construct(?string $message = null)
{
parent::__construct($message);
}
public function passes(): bool
{
return is_string($this->value) && mb_strtoupper($this->value) === $this->value;
}
public function message(): string
{
return "The {$this->field} field must be uppercase.";
}
}
#[Uppercase]
public string $code;
#[Uppercase(message: 'Shout it!')]
public string $shout;
use Mewtonium\Vanguard\Rules\BaseRule;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
final class Trimmed extends BaseRule
{
public function __construct(?string $message = null)
{
parent::__construct($message);
}
public function passes(): bool
{
return is_string($this->value) && preg_match('/^\s|\s$/', $this->value) === 0;
}
public function message(): string
{
return "The {$this->field} field must not have leading or trailing whitespace.";
}
}
use Mewtonium\Vanguard\Concerns\PreparesDate;
use Mewtonium\Vanguard\Contracts\PreparesValue;
use Mewtonium\Vanguard\Rules\BaseRule;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
final class FutureDate extends BaseRule implements PreparesValue
{
use PreparesDate;
public function __construct(
protected string|\DateTimeInterface $after,
?string $message = null,
) {
$this->after = $this->prepare($after);
parent::__construct($message);
}
public function passes(): bool
{
return $this->value instanceof \DateTimeInterface
&& $this->value > $this->after;
}
public function message(): string
{
return sprintf(
'The %s field must be a date after %s.',
$this->field,
$this->format($this->after),
);
}
}
#[FutureDate('2025-01-01')]
public string $startsAt;
public function __construct(/* ... */)
{
$this->lenient = true;
// ...
}
#[Required(message: 'Please enter your first name.')]
#[MinLength(2, message: 'Your last name must be at least 2 characters long.')]
if ($form->invalid()) {
$errors = $form->errors(); // Returns an instance of `ErrorBag`
// ...
}
$errors->all(); // Returns all validation errors
$errors->get($field); // Returns all errors for a specific field
$errors->first($field); // Returns the first error for a specific field
$errors->has($field); // Checks if a field has any errors
$errors->count(); // Total number of validation errors
$errors->flush(); // Clears the bag
[
'lastName' => [
'Required' => 'Please provide your last name.',
'MinLength' => 'The lastName field must be a minimum of 2 characters long.',
],
'age' => [
'Between' => 'The age field must be between 18 and 99.',
],
'email' => [
'Email' => 'The email field must be a valid email.',
],
'country' => [
'In' => 'The country field does not have a valid selection.',
'Uppercase' => 'The country field must be uppercase.',
],
'date' => [
'GreaterOrEqual' => 'The date field must be greater than or equal to 2025-01-01.',
],
]
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.