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

Armani's avatar
Level 17

Conditionally set $errorBag property

I want to conditionally set $errorBag property inside Form Request:

protected $errorBag = $this->isMethod('patch') ? 'visit' . $this->visit->id : 'new';

but it gives me this error:

Constant expression contains invalid operations

Is there any way to do this?

0 likes
1 reply
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You cannot set a property dynamically outside of a method.

public function foo()
{
    $this->errorBag = $this->isMethod('patch') ? 'visit' . $this->visit->id : 'new';

}
1 like

Please or to participate in this conversation.