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

orest's avatar
Level 13

manipulate request parameters

I have an input text in which a user can enter a single username or comma separated usernames.

I have a rule that validates whether the usernames exist. In that rule if the string consists of comma separated usernames i create an array of usernames and then validate the array.

class CommaSeparatedUsernames implements Rule
{
 	public function passes($attribute, $value)
    	{
        	if (str_contains($value, ',')) {
            	$this->validateMultipleUsernames($attribute, $value);
       	 }
        return $this->validateUsername($attribute, $value);
    }
}

However, the value in the request parameter remains the same ( a string which consists of comma separated usernames ) and then i have to split again the string into an array in order to use the usernames.

Right now i have this function within the rule

class CommaSeparatedUsernames implements Rule
{
 	public function passes($attribute, $value)
    	{
        	if (str_contains($value, ',')) {
		    $this->validateMultipleUsernames($attribute, $value);
		    $this->replaceParticipantsParameter($value);
            
                 }
                 return $this->validateUsername($attribute, $value);
    	}

	 public function replaceParticipantsParameter($commaSeparatedNames)
    	{
        	request()->merge(
            		['participants' => $this->getNames($commaSeparatedNames)]
        	);
    	}
	
	public function getNames($commaSeparatedNames)
    	{
        	return array_map(
            	    fn($name) => trim($name, ' '),
            	    explode(',', $commaSeparatedNames)
        	    );
         }

}

My question is where should i put the code for manipulation the request parameter

  1. Does it make sense to create a middleware that manipulates the request parameter ?

  2. Should i put that code in the Controller or in the Model that uses the request parameter ?

0 likes
4 replies
GeordieJackson's avatar

I would use a FormRequest class. See: https://laravel.com/docs/8.x/validation#form-request-validation

and in the form request class, use the prepareForValidation() method to manipulate the request fields (e.g. exploding the names field) and then let the validation run as normal - with the validation in the rules() method.

e.g.

    protected function prepareForValidation()
    {
        // Manipulate request fields here
    }

    public function rules()
    {
        return [
            // Set validation rules here
        ];
    }

    public function validated()
    {
        // Post validation manipulation can be done here if required
    }
2 likes
orest's avatar
Level 13

thanks for the suggestion.

if i wanted to apply this particular manipulation to different routes though, how should i approach it ?

Because the user can enter comma separated names in different forms where each form consists of different request parameters.

Therefore if i manipulate the request parameter in the prepareForValidation method, i would end up repeating the code for manipulating se same request parameter.

orest's avatar
Level 13

exactly.

i want to change the value of a request parameter and validate it independently.

thanks again, i will check this out

Please or to participate in this conversation.