I have a trouble with the {field} placeholder in a custom validation rule error message. What am I missing?
I have created a custom validation rule and put it into the app/Libraries/MyRules.php file:
PHP Code:
<?php
namespace App\Libraries;
class MyRules
{
public function no_links(string $string, string &$error = null): bool
{
$str = preg_replace('/ /','', $string);
$error = 'Field {field} should not contain links.';
if (strpos($str,'http')) { return false; }
elseif (strpos($str,'www')) { return false; }
elseif (strpos($str,'ftp')) { return false; }
else {return true;}
}
}
Then I added this custom rules library to the list where validation class looks for rules - in app/Config/Validation.php:
PHP Code:
<?php namespace Config;
class Validation
{
public $ruleSets = [
\CodeIgniter\Validation\Rules::class,
\CodeIgniter\Validation\FormatRules::class,
\CodeIgniter\Validation\FileRules::class,
\CodeIgniter\Validation\CreditCardRules::class,
\App\Libraries\MyRules::class, // HERE
];
The rule, when added to validation rules set like this:
PHP Code:
$validation->setRules([
'name' => ['label' => '„Your name“', 'rules' => 'required|min_length[3]|max_length[100]'],
'email' => ['label' => '„Email“', 'rules' => 'required|valid_email'],
'message' => ['label' => '„Message“', 'rules' => 'required|no_links|min_length[10]|max_length[2000]'],
]);
in the controller and invoked, seems to work, the error message is displayed, but the {field} placeholder is not replaced with the field name „Message“ (while it does get replaced in case of all other error messages, even for the same field). What is the problem here?
Would be grateful for any suggestions.
==
Donatas G.