Witam, ostatnio przeczytałem, że od Laravela 5.5 można dodawać własne reguły walidacji za pomocą klas, a nie tylko przez Validator::extend()
Problem w tym, że trzeba w metodzie message()
określić, co konkretnie ma zostać zwrócone w przypadku niepowodzenia. Chciałbym, aby w w razie błędu była zwracana customowa wiadomość dla danego pola. Mam taką klasę:
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class HasDigit implements Rule
{
private $fieldName;
/**
* Create a new rule instance.
*/
public function __construct($fieldName)
{
$this->fieldName = $fieldName;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return preg_match('/[\d]+/', $value);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return trans("validation.custom.{$this->fieldName}.has_digit");
}
}
Właściwie działa, ale czy tak to należało zrobić?