<?php
use Illuminate\Validation\Validator;
use Illuminate\Translation\Translator;
use Illuminate\Translation\FileLoader;
use Illuminate\Filesystem\Filesystem;
$filesystem = new Filesystem();
$loader = new FileLoader($filesystem, '');
$translator = new Translator($loader, 'us');
// custom validator rule called "starts"
$customRules = [
'starts' => function($attribute, $value, $parameters) {
return 0 === strpos($value, array_shift($parameters));
}
];
$data = [
'name' => 'John',
];
$rules = [
'name' => 'starts:Jo',
];
$validator = new Validator(
$translator,
$data,
$rules
);
$validator->addExtensions($customRules);
// does attribute "name" value starts with "Jo"?
$hasPassed = $validator->passes();
dump($hasPassed);