laravel how to validate as equal to a variable [duplicate]
Asked Answered
P

2

10

in laravel validation (registering) i want to compare one of the fields with a php variable (it should be equal with that)

how can i do this?

protected function validator(array $data)
{

    return Validator::make($data, [
        'name' => 'required|max:255',
        'phone' => 'required|min:10|max:11|unique:users',
        'email' => 'required|email|max:255',
        'password' => 'required',
        'password_confirmation' => 'required',
        'user_captcha' => 'required'
    ]);
}
Pritchett answered 18/8, 2017 at 18:17 Comment(1)
you mean something like this? 'name' => 'equalto:$varable', it just an example for understandingMunoz
G
36

You can do it for example for name field like this:

$variable = "something"
return Validator::make($data, [
    'name' => [
        'required',
        Rule::in([$variable]),
    ],
    'phone' => 'required|min:10|max:11|unique:users',
    'email' => 'required|email|max:255',
    'password' => 'required',
    'password_confirmation' => 'required',
    'user_captcha' => 'required'
]);

Remember to import Rule Class (use Illuminate\Validation\Rule;)

You can get more info in: https://laravel.com/docs/5.4/validation#rule-in

EDIT

As suggested by @patricus, you can also concatenate the variable

$variable = "something"
return Validator::make($data, [
    'name' => 'required|in:'.$variable,
    'phone' => 'required|min:10|max:11|unique:users',
    'email' => 'required|email|max:255',
    'password' => 'required',
    'password_confirmation' => 'required',
    'user_captcha' => 'required'
]);

EDIT2

If you have a variable that is an array:

$variable = ['one','two'];
return Validator::make($data, [
    'name' => 'required|in:'.implode(",", $variable),
    'phone' => 'required|min:10|max:11|unique:users',
    'email' => 'required|email|max:255',
    'password' => 'required',
    'password_confirmation' => 'required',
    'user_captcha' => 'required'
]);

Or

$variable = ['one','two']
return Validator::make($data, [
    'name' => [
        'required',
        Rule::in($variable),
    ],
    'phone' => 'required|min:10|max:11|unique:users',
    'email' => 'required|email|max:255',
    'password' => 'required',
    'password_confirmation' => 'required',
    'user_captcha' => 'required'
]);
Gladine answered 18/8, 2017 at 18:37 Comment(10)
Given the simple use of a variable, it would probably look better just to do: 'name' => 'required|in:'.$variable,Odessaodetta
@leonardo and patricus my fiels is captcha i test your two ways and i will fill whole form correctly but i have just one error : user_captcha : ["validation.required"]Pritchett
print the $data variable pleaseMunoz
its ok. its an array and everything is right. just the captcha validation (in:) not working (just required will working)Pritchett
the problem was from sending my data from form and ajax. sovled with in: . thank you. voted upPritchett
for those who have the error 'App\Http\Controllers\Validator' not found, try: "use Illuminate\Support\Facades\Validator;"Teteak
that edit version didn't work for me though. 1st is okPosy
Is it right, that $variable will never be showed to user ?Recover
Or even 'name' => "required|in:$variable"Safar
Rule::in($variable) is sufficient. Laravel will automatically wrap the value in an array if needed. String concatenation is ugly and outdated.Heyward
W
0

if you don't want to create an array of possibilities but you have an integer that must be equal to given value, you can use size validation rule, for example if "some_integer" is an integer and must be equal to 10, then in your request class you would do the following:

$rules = ['some_integer' => 'size:10']

or if you have a "some_string" that is a string and it's length should be equal to 10, you would do:

$rules = ['some_string' => 'size:10']

Edit:
Also, you can say grater than or less than like so (symbol "|" used as or operator, gt and lt takes one argument only)
Greater than: gt:value|field_name
Less than: ls:value|field_name

Warning:
Greater than and less than (gt and lt) checks the given value by size validation rule, that means if you will specify a field, and not a plain value, you need to be sure that two fields have the same type.

Washington answered 21/5 at 0:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.