1

I have two checkbox fields tech and active that I want to be nullable but I also want the values that can be entered to be restricted to a single value for one field and one of two values for the other field.

I came across the answer stackoverflow but I don't seem to implement it properly. Heres my code:

 $validator = Validator::make($request->all(), [
    'name' => 'required|string|max:150',
    'email' => 'required|string|max:200',
    'phone' => 'required|digits:11',
    'branch' => 'required|string',
    'department' => 'required|string',
    'tech' => ['nullable','digits:1', Rule::in([1, 2])],
    'role' => 'required|string',
    'active' => ['nullable', 'string', 'max:6', Rule::in(['active'])]
 ]);

If I do this:

'tech' => 'nullable|digits:1',
'active' => 'nullable|string|max:6'

It works, but I need to validate the values that these fields can allow.

10
  • What is not working? Commented Jul 15, 2019 at 14:28
  • i have editted it @ChinLeung Commented Jul 15, 2019 at 14:50
  • 2
    Can you show how you are sending the data/request to the server? I have tried a simple request with 'tech' => 'nullable|digits:1|in:1,2', 'active' => 'nullable|string|max:6|in:active' and it is working just fine. Commented Jul 15, 2019 at 15:06
  • oh okay i'm using ['nullable', 'string', 'max:6', Rule::in(['active'])] instead. Let me implement yours Commented Jul 15, 2019 at 15:09
  • I don't think it's related to the validation rule as it works fine the way you have it too. I think it's in the data that you are sending that is wrong. Can you show me what the output of dd($request->all()) gives you? Or maybe dd($request->tech). Commented Jul 15, 2019 at 15:11

2 Answers 2

1

You can pass closures to validators, even in the validator class.

$validator = Validator::make($request->all(), [
    'name' => 'required|string|max:150',
    'email' => 'required|string|max:200',
    'phone' => 'required|digits:11',
    'branch' => 'required|string',
    'department' => 'required|string',
    'tech' => function($attribute, $value, $fail){
        if (isset($value)) {
            sizeof($value) > 1 ?? return $fail($attribute." is invalid");
            in_array($value, [1,2]) ?? return $fail($attribute." is invalid");
        }
    },
    'role' => 'required|string',
    'active' => ['nullable', 'string', 'max:6', Rule::in(['active'])]
 ]);
Sign up to request clarification or add additional context in comments.

Comments

1

Chin Leung's implementation via the comments section solved the problem for me.

$validator = Validator::make($request->all(), [
   'name' => 'required|string|max:150',
   'email' => 'required|string|max:200',
   'phone' => 'required|digits:11',
   'branch' => 'required|string',
   'department' => 'required|string',
   'tech' => 'nullable|digits:1|in:1,2',
   'role' => 'required|string',
   'activate' => 'nullable|string|max:6|in:active'
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.