I am storing an array of strings in my database (db column type is JSON). There is a form that allows users to add a value to this array. I want to make sure there are no duplicates in this array. The notIn validation rule appears be the simplest solution to prevent duplicates but it is case sensitive. So when using notIn I am not able to prevent identical strings that have different capitalization.
$this->validate(request(), [ 'choice' => [ 'required', Rule::notIn($choices) ] ]);
Does anyone have recommendation on how I should fix this validation so that the string comparison is case insensitive?
Advertisement
Answer
You could lowercase your input data as well as your current data like this:
$input = request()->all(); $input['choice'] = array_map("strtolower", $input['choice']); request()->validate($input, [ 'choice' => [ 'required', Rule::notIn(array_map("strtolower", $choices)) ] ]);