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

orest's avatar
Level 13

associate prepare data for validation with rule error

I have a FormRequest with the following rules.

 public function rules()
    {
        return [
            'participants' => ['required', 'array', 'min:1'],
            'participants.*' => ['required', 'string', 'exists:users,name'],
        ];
    }

However the value of the attribute participants comes from an input text and is a string which normally it can be a single name or multiple names separated with comma.

'George'
'George, John'

And before i validate the data i have to manipulate it.

public function prepareForValidation()
{
	request()->merge(['partcipants' => explode(",", request('participants')])
}

Does it make sense to assume that the value of the participants might not be a string ?

So in case an object or an array is passed to participants

Then the prepareForValidation will fail and i can't even associate it with the participants attribute.

One way to solve this would be to create a new rule in order to be able to associate any kind of exceptions with the participants attribute.

However, in my application i assume that i receive an array from the participants ( an array of usernames ) parameter and i don't find a good idea to hide the prepareForValidation ( the functionality that splits the string into an array ) inside the new rule.

Any thoughts on how i can prepareDataForValidation, associate any failure with the rule so i can return the corresponding message, and at the same time avoid hiding the split of string to array inside a rule.

0 likes
5 replies
gitwithravish's avatar

So in case an object or an array is passed to participants

Why are you not sure that what will be passed here? If you have an input field then either a string will be passed or it would be empty/null. How do you think an array or object could be passed here ?

Simply,

  • Validate that the input must contain alphanumeric characters and ',' comma symbol.
  • Check if the comma is presented in the input, if yes then assume there are multiple names given and explode it.

[Edit]

  • after the above steps, if there is 9nly one username then convert it to single element array
  • now validate the user array to be presented in users table
2 likes
orest's avatar
Level 13

I wasn’t sure if it makes sense to assume that it might not be string.

Thanks for the suggestion

gitwithravish's avatar

In that way, you could never be sure of anything ! Any form field can be manipulated and sent to the server in different form. But that is what the validations are for. To ensure that the data is coming as you expected :)

orest's avatar
Level 13

Now that i'm reading again your suggestion.

  1. I can't validate that the input must be a string and contain a comma symbol

The reason is that the input consists of usernames which must exist in the database. Therefore, i can't validate a string with multiple usernames.

Another reason is that a comma is included only when multiple usernames are passed.

What i did in the prepareForValidation method is


public function prepareForValidation()
{
     if (!is_string($this->names)) {
            return;
        }

        if (str_contains($this->names, ',')) {
            $this->request->merge(
                [$this->attribute => $this->splitNames($this->names)]
            );
        } else {
            $this->request->merge(
                [$this->attribute => [$this->clean($this->names)]]
            );
        }
}

And the validation rules are

public function rules()
    {
        return [
            'title' => ['required', 'string', 'min:3'],
            'message' => ['required', 'string'],
            'participants' => ['required', "array", 'min:1'],
            'participants.*' => ['required', 'string', 'exists:users,name'],
        ];
    }

So i expect the input to be string. If the input is string then check if comma symbol is included and if yes then explode. Otherwise only one username is in the string and therefore create an array with a single username.

The validation rules expect an array. If the input is not a string then prepareForValidation will not create an array and the validation will fail. If the input is a string, then an array will be created as expected and then the validation rules will check if the content of the array are string and if each element ( username ) exists in the database.

1 like

Please or to participate in this conversation.