I have considered defining validation rules for different parts of my application in a class as static variables. For example,
$VALIDATION_RULES = [
'signatures' => [
'first_name' => ['required', 'max:255', 'required','string'],
'last_name' => ['required', 'max:255', 'required', 'string'],
'enabled' => [ 'required', Rule::in(['on', 'off']) ],
'signature_file' => ['required', 'mimes:png,jpeg,jpg', 'max:1024'],
'operator_id' => ['required', 'numeric'],
],
];
However, I get this error.
Symfony\Component\ErrorHandler\Error\FatalError
Constant expression contains invalid operations
This is due to
Like any other PHP static variable, static properties may only be initialized using a literal or constant before PHP 5.6; expressions are not allowed. In PHP 5.6 and later, the same rules apply as const expressions: some limited expressions are possible, provided they can be evaluated at compile time.
according to the php official documentation. What is the correct way to do this?
P.S.: This variable is defined inside a class as a public and static member. In the code, wherever I need to validate an input, I can call
$validated_input = $request->validate($VALIDATION_RULES['signatures']);
This way, I can have all the rules centralized in one class.