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
-
Does it make sense to create a middleware that manipulates the request parameter ?
-
Should i put that code in the Controller or in the Model that uses the request parameter ?