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
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'); //
});
I am trying to make a single method which can be used to update a user profile, to make it easier for a user I want the validation to only work if there is data in the field, however when trying to use the 'sometimes' property with the 'present' property, the validation for string runs for example. in this project I am using vue.js and submit the form data using the inertia.put method, I am also using the vue dev tools chrome extension to view the errors in the inertia prop.
The form object can be seen below
form: {
name: '',
username: '',
email: '',
password: '',
password_confirmation: '',
_token : this.$page.props.csrf
}
below is the method that will run the put request
Inertia.put(route('user.edit', this.user.id), this.form, {
onSuccess: () => {
this.user.username = this.form.username;
this.form.username = '';
this.sweetAlertSuccess('username');
}
});
this is the validation method in Laravel which is a custom request
public function rules(): array
{
return [
'name' => ['sometimes', 'present', 'string', 'max:255'],
'username' => ['sometimes', 'present', 'max:255', 'string', 'unique:users,username'],
'email' => ['sometimes', 'present', 'email', 'unique:users,email', 'max:255'],
'password' => ['sometimes', 'present', 'min:8', 'max:255', 'confirmed']
];
}
here is the error message I am seeing, I only want the message to validate the field which has data in it and fails the rules, if the field is empty then I want it to skip the validation for that particular field in the keyed array.
replacing 'present' with nullable fixed the issue.
public function rules(): array
{
return [
'name' => ['sometimes', 'nullable', 'string', 'max:255'],
'username' => ['sometimes','nullable', 'max:255', 'string', 'unique:users,username'],
'email' => ['sometimes', 'nullable', 'email', 'unique:users,email', 'max:255'],
'password' => ['sometimes', 'nullable','min:8', 'max:255', 'confirmed']
];
}
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.
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 want to do "unique" validation on multiple fields. I have written below validation rule but not sure how to include brand_id and company_id in it
$request->validate([
'name' => 'required|string|unique:products',
'company_id' => 'required',
'brand_id' => 'required',
]);
So what I am trying to do is, ADD PRODUCT but check if the name is unique for the given company_id and brand_id. How can I do that?
We can use Rule::unique() function to add custom conditions to unique validation.
$request->validate([
'company_id' => 'required',
'brand_id' => 'required',
'name' => [
'required',
'string',
Rule::unique('products')->where(function($query) {
$query->where('company_id', request('company_id'));
$query->where('brand_id', request('brand_id'));
}),
],
]);
Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute:
$request->validate([
'name' => 'bail|required|string|unique:products',
'company_id' => 'bail|required',
'brand_id' => 'required',
]);
You can add additional wheres after the ignore options (the NULL,id below), e.g.:
$request->validate([
'company_id' => 'required',
'brand_id' => 'required',
'name' => 'required|string|unique:products,name,NULL,id,company_id,'.request('company_id').',brand_id,'.request('brand_id'), '
]);