charlieBrown's avatar

Rule::in not working when you have a null value

I have been browsing and I found out that I could filter vlues beeing present in array x. I'm using the Rule::into implode the array as the documentation suggests. I'm trying to use it to filter a pagination (I don't know if this is the correct method of doing it).

Search controller

$validator = Validator::make($request->all(), [
            'profissao'     => 'required|present|max:50|string|Alpha',
            'distrito'      => 'required|max:50|string|Alpha',
            'orderBy'       => [
                'present', // It may not be present since the user may not filter any value
                'nullable', // If the value is not present then it's going to be null
                Rule::in(['preco_hora', 'stars', 'fora_regiao']), 

            ]
        ]);
        if($validator->fails()){
            return redirect('error')->withInput();
        }

But if I try to simplely ask for the results without passing orderBy it throws me to error page. I have made dd($request->oderBy) and it return null as expected. I have tried adding Rule::in(['preco_hora', 'stars', 'fora_regiao', 'null']) but without any lucky.

Any thoughts?

0 likes
3 replies
Snapey's avatar

it throws me to error page.

With what error?

charlieBrown's avatar

@Snapey order by must be present. From the docummentation: The field under validation must be present in the input data but can be empty. It makes sense since the orderBy doesn't exist (null). It's only works if I comments out 'present' validation. Is there any problem in just removing 'present' since I'll verify the values in Rule::in?

Snapey's avatar
Snapey
Best Answer
Level 122

what about sometimes instead of present?

1 like

Please or to participate in this conversation.