Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel how to validate as equal to a variable

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'
    ]);
}
like image 810
K1-Aria Avatar asked Aug 18 '17 18:08

K1-Aria


People also ask

How to check two value must not be same validation in Laravel?

In this post, we will learn how to check two value must not be same validation in laravel. we can check using laravel different validation rule. you can use different validation rule for field value should not same in form. You can use that validation with form fields. i will give you controller method with form validation.

How do I translate validation errors in Laravel?

Laravel's built-in validation rules each has an error message that is located in your application's lang/en/validation.php file. Within this file, you will find a translation entry for each validation rule. You are free to change or modify these messages based on the needs of your application.

What are $rules and $errors in Laravel?

$rules is an array of key value pairs which represent the form fields and the corresponding validations. To display the error messages in the view laravel will share a special variable called $errors that will be available after you submit the form:

What are the different email validation styles available in Laravel?

Here's a full list of validation styles you can apply: The filter validator, which uses PHP's filter_var function, ships with Laravel and was Laravel's default email validation behavior prior to Laravel version 5.8. The dns and spoof validators require the PHP intl extension.


1 Answers

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'
]);
like image 150
Leonardo Cabré Avatar answered Oct 20 '22 22:10

Leonardo Cabré