Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kevinwakhisi's avatar

Overrides required rule (FormRequest)

Recently I discovered when I use the prepareForValidation function in my form request, the rule (required) does not adhere

/**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => ['required', 'unique:permissions'],
        ];
    }

and the protected function

protected function prepareForValidation()
    {
        $this->merge([
            'name' => Str::of($this->name)->title()->plural(),
        ]);
    }

in other words I would like my inputs have a standard fromart

0 likes
4 replies
SilenceBringer's avatar
Level 55

@kevinwakhisi I think it's because Str::of($this->name)->title()->plural() returns Stringable object (so param is an object, and required rule passes). try to add __toString() at the end

'name' => Str::of($this->name)->title()->plural()->__toString(),
1 like

Please or to participate in this conversation.