In my form, I have two fields pass and pass_repeat. On the browser side, I validate it using javascript. On the server-side I want to check whether they are the same. How can I do it using request()->validate() method?
I can check the equality with php, however, I'd like to know if it is possible with request()->validate() or not.
Even keywords for further search are appreciated.
P.S. My try:
$result = request()->validate([
'email' => ['required', 'email'],
'password' => ['required', 'string', 'max:30', 'min:8'],
'password_repeat' => ['required', 'string', 'max:30', 'min:8']
]);
if ($result['password' != $result['password_repeat'] || $result['password_repeat'] == '' || $result['password'] == '')
{
abort();
}
Thanks.
just change the name of input 'password_repeat' to 'password_confirmation' in your form and use validator 'confirmed'
$result = request()->validate([
'email' => ['required', 'email'],
'password' => ['required', 'string', 'max:30', 'min:8','confirmed'],
]);
you are doing this in a wrong way, laravel has confirmed rule that can be use for confirmation purposes, you should add pass_confirmation input and pass it to controller via form and use confirmed rule for pass input.
Related
please I am trying to allow users to register with a coupon code, if coupon code is invalid dont register user, but when I tried it users are been registered even though the code is already used or invalid
I am using this package for the code https://github.com/michael-rubel/laravel-couponables
public function store(Request $request)
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
'username' => ['required', 'string', 'max:255', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);
$user = User::create([
'username' => $request->username,
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
$user->redeemCoupon($request->code);
event(new Registered($user));
Auth::login($user);
return redirect(RouteServiceProvider::HOME);
}
You are already registering the user before checking if the coupon is valid. Move this line after the validation.
$user->redeemCouponOr($request->code, function ($e) {
//handle the different exceptions here if not valid.
});
$user = User::create([
'username' => $request->username,
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
Or handle the validations inside the validator method
$request->validate([
...
'code' =>'sometimes|exists:coupon_table,coupon_code_column,coupon_status_column,!used_or_some_other_status'
]);
Make a custom validation rule to verify code and check if the code is already used.
$redeemer->verifyCoupon($code);
$redeemer->isCouponAlreadyUsed($code);
I don't see anything that is not explained in the Laravel Couponables package that you are using it clearly has explanations on the GitHub:
Listeners
If you go event-driven, you can handle package events:
CouponVerified
CouponRedeemed
CouponExpired
CouponIsOverLimit
CouponIsOverQuantity
NotAllowedToRedeem
FailedToRedeemCoupon
All the exceptions are well explained in the documentation
If something's going wrong, methods verifyCoupon and redeemCoupon will throw an exception:
CouponExpiredException // Coupon is expired (`expires_at` column).
InvalidCouponException // Coupon is not found in the database.
NotAllowedToRedeemException // Coupon is assigned to the specific model (`redeemer` morphs).
OverLimitException // Coupon is over the limit for the specific model (`limit` column).
OverQuantityException // Coupon is exhausted (`quantity` column).
CouponException
You can simply replace this line from your code:
$user->redeemCoupon($request->code);
To this:
$user->redeemCouponOr($request->code, function ($exception) {
// Your action with $exception!
print('This coupon is no longer valid'); //
});
Is it possible to add validation if another validation passed for example I have the following validation rules
return [
'name' => ['required', 'string', 'min:3', 'max:30'],
'password' => ['required', 'min:6'],
'photo' => ['imageable'],
'business_uuid' => ['required', 'uuid', 'exists:businesses,uuid'],
];
and I would like if the business_uuid validation passed to add validation for the phone number
'phone' => ['nullable', 'phone:'.$countryCode],
notice that I'm the country code will be brought from the business That's why I'm I'd like to add it if the validation passed
I want to strengthen Laravel's standard password validation: [A-Z] [a-z] [0-9]. I want to accept input with more than eight special characters and I need maximum support for Laravel Hash.
php artisan make:rule StrongPassword
What should I do after this?
You can add wathever validation rules you want in the Laravel RegisterController. Just update the rule for password with the extra limitations:
https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Auth/RegisterController.php#L49
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'], // <-- This one
]);
I am running some basic validation inside a Laravel 5.5 controller like this...
$this->validate($request, [
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'required',
]);
Is there a way to check if 'mykey' matches a php string I have saved? I know I can do an if statement and compare them but wondered if there was a way I could do this inside the validation itself?
You can use in rule, This works for n number of values
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => [
Rule::in([env('MY_KEY'),config('app.another_key')]),
]
]);
Laravel provides a regex option for validation. Depending on the complexity of the string comparison it may be useful:
https://laravel.com/docs/5.5/validation#rule-regex
You can this rule:
$key = "my_saved_key"
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'in:' . $key,
]
]);
i'm new to laravel and i would like to know if there are a way to use or/else conditional in $rules, because I am struggling trying to do the following validation:
If the field is Z the size must be y, else, x.
I tried this, but it always validade data with size:11
'type => 'required',
'field' => ['required', ('type == 1' ? 'size:11' : 'size:14')],
Anyone has a clue? thanks in advance
I assume you use Form request validation so you can do something like this:
$rules = [
'type' => 'required',
'field' => ['required'];
];
$rules['field'][] = ($this->input('type') == 1) ? 'size:11' : 'size:14';
return $rules;
Use Laravel Validator ,
Refer Link: https://laravel.com/docs/5.5/validation
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);