3

i have a custom rule which i made using rule objects and its working fine except for one thing, that it doesn't pick up the custom validation message i created for it in the component and instead picks whatever it is assigned to it in the validation.php file or the equivalent translation of it from the translated validation.php file. other non-custom rules are working as expected with the custom messages for the same field.

the component:

public function rules()
    {
        return[
            'topic' => ['required', 'string', 'max:250', 'min:5', new Profane],
            'name' => ['required', 'string', 'max:250'],
            'email' => ['required', 'email:rfc,dns', 'max:250']
        ];
    }

protected $messages = [
        'topic.required' => 'some message',
        'topic.max' => 'some message',
        'topic.min' => 'some message',
-->     'topic.profane' => 'some message',
        'name.required' => 'some message',
        'name.max' => 'some message.',
        'email.email' => 'some message',
        'email.required' => 'some message',
    ];

the rule object:

public function passes($attribute, $value)
    {
        $profane = ProfaneWord::all();
        $words = $profane->pluck('word');
        foreach ($words as $word)
        {
            if (stripos($value, $word) !== false) return false;
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return trans('validation.profane');
    }
2
  • 1
    I'm not sure if this is possible at all but if it is I would expect it to be more like 'topic.'.Profane::class => 'some message', Commented Oct 22, 2021 at 14:27
  • @apokryfos so it's not possible to use a custom message on run-time for a custom rule? that would be odd if that's not possible. Commented Oct 22, 2021 at 14:43

2 Answers 2

2

This does not currently seem possible when using custom rules. The problem in the source the message is only ever retrieved from the message() method. However since the validation rule is your own class you can always change it:

    private $message; 
    public __construct($message = null) {
       $this->message = $message;
    }

    public function passes($attribute, $value)
    {
        $profane = ProfaneWord::all();
        $words = $profane->pluck('word');
        foreach ($words as $word)
        {
            if (stripos($value, $word) !== false) return false;
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return $this->message??trans('validation.profane');
    }

Sign up to request clarification or add additional context in comments.

Comments

1

As suggested by the comment from @apokryfos and tested on Laravel 9 (v9.30.1) custom validation rule messages can be overridden with the full class name. Example below is from FormRequest usage.

public function messages(): array
{
    return [
        'topic.required' => 'some message',
        // ... other rules
        'topic.'.Profane::class => 'The :attribute can not contain ...',
    ];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.