Neel
Neel

Reputation: 9880

Laravel 5.3 Validation Regex check for True or False fails

In Laravel 5.1, I used to do a validation check to see if a posted value is set as true or false using Regex Match. The posted value is something like this:

acceptTerms   true

I then check the value from Laravel validation like this:

$validator = Validator::make($postData, [
  'acceptTerms'   => ["regex:(true|false)"],
]);

Since updating to Laravel 5.3, the above validation rules fails and always throws the error: acceptTerms format is invalid..

Why is it failing and how do I check if a value is set as true or false in Laravel 5.3?

EDIT 1:

I forgot to add, the data posted is sent as JSON.stringify(data) and in Laravel I receive it as Input::json('data'). Would that change anything?

EDIT 2:

dd($postData) returns this:

array:31 [
  "id" => "8"
  "appId" => "1"
  "name" => "Asdsad"
  "acceptTerms" => true
  ]

Upvotes: 1

Views: 2160

Answers (2)

Alexey Mezenin
Alexey Mezenin

Reputation: 163758

If it's boolean, you can use boolean.

I guess it's more readable than regex.

The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and "0".

Upvotes: 2

Amit Gupta
Amit Gupta

Reputation: 17658

You can try this with in() as:

'acceptTerms'   => 'in:true,false',

Upvotes: 1

Related Questions