Код: Выделить всё
namespace App\Validation;
class CustomRules
{
public function noSpecialChars(string $str, string $fields, array $data): bool
{
return preg_match('/^[a-zA-Z0-9 ]+$/', $str) === 1;
}
}
public $ruleSets = [
\CodeIgniter\Validation\Rules::class,
\App\Validation\CustomRules::class,
];
< /code>
Метод контроллера: < /p>
public function submit()
{
$validation = \Config\Services::validation();
$rules = [
'username' => 'required|noSpecialChars'
];
if (!$this->validate($rules)) {
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
}
// proceed with saving
}
< /code>
Даже когда я ввожу что -то вроде John@doe, он все еще проходит проверку. Чего мне здесь не хватает?
Подробнее здесь: https://stackoverflow.com/questions/796 ... validation
Мобильная версия