1

I want to validate a requested array which it's like :

  "accountType": {
    "admin" : true,
    "advertiser": true,
    "publisher": true,
    "agency": true
  },

I want to check if admin is true, do nothing and pass, but if admin is false and others are true or there is no admin in accountType object validation throw error like : invalid account type.

In Another word I want to check if there is admin in request array pass the validation, if not and there are other types shows error, also vise versa.

This is my validation but It just pass anyway:

 $validator = Validator::make($this->request->all(), [
            'accountType.admin' => 'boolean:true',
            'accountType.advertiser' => 'boolean:false',
            'accountType.publisher' => 'boolean:false',
            'accountType.agency' => 'boolean:false',
        ]);
2
  • The boolean validation rule doesn't accept parameters. Commented Feb 11, 2017 at 9:54
  • @RossWilson So What your suggestion to make it work ? Commented Feb 11, 2017 at 10:10

3 Answers 3

1

Try

$validator = Validator::make($this->request->all(), [
            'accountType.admin' => 'required|boolean:true',
            'accountType.advertiser' => 'boolean:false',
            'accountType.publisher' => 'boolean:false',
            'accountType.agency' => 'boolean:false',
        ]);
Sign up to request clarification or add additional context in comments.

1 Comment

Not working, It passed all attributes. I mean advertiser and other roles will pass, I want to throw error for other roles if they are true
0

Should do the trick, from the documentation: he field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and "0".

$validator = Validator::make($this->request->all(), [
    'accountType.admin' => 'boolean',
    'accountType.advertiser' => 'boolean',
    'accountType.publisher' => 'boolean',
    'accountType.agency' => 'boolean',
]);

5 Comments

You didn't read the question entirely ? I wan it to throw error if any other role except admin is set and their value is true give me error , this way it just pass validation.
Your right, apologies, if your making admin only why not use middleware?
Don't mention it, this validation is for updating users role. I said in my question it need it sometime to check if only admin is true , and vise versa if it contains admin throw error. but my problem is it dose not check,it just pass if it's true or false.
I think you might have to make a custom validation rule cant work out any other way
I agree, I was trying to create one.
0

You could change your values to 1 for true and 0 for false then validate like:

$validator = Validator::make($this->request->all(), [
    'accountType.admin' => 'required|min:1',
    'accountType.advertiser' => 'required|min:1',
    'accountType.publisher' => 'required|min:1',
    'accountType.agency' => 'required|min:1',
]);

2 Comments

i'll check and let you know
It didn't help, it gave me error of 1 character is required. while my request consist of accountType.agency:true

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.