Laravel 4 field validation issues on edit - php

I am using a simple user auth package. It has default validation on user creation, however, it does not have any sort of user edit functionality. No problem, I can write that in. The problem I am having is on update of the username I am failing validation for the following:
The email has already been taken.
The password must be between 4 and 11 characters.
The password confirmation does not match.
When editing, the email is taken because it's the same email. I'm not evenn editing the password.
So my question: How do I turn off validation for updating/editing, or better yet, how do I apply different validation rules for those methods?

You can do that by changing default values, which are provided with package. They can be found on line 50:
public static $rules = array(
'username' => 'required|alpha_dash|unique:users',
'email' => 'required|email|unique:users',
'password' => 'required|between:4,11|confirmed',
'password_confirmation' => 'between:4,11',
);
In a class ConfideUser.
Just replace those rules with you own. In case you dont have some rules e.g. password_confirmation, then delete it. And also adopt your HTML forms and models/db with new validatiorn rules.
I hope it helps.

Related

Laravel Validation: bail rule not working

According to Laravel Documentation, bail rule should stop running validation rules after first validation failure.
I have a file outside App\Http\* namespace with the following code in it:
if(Validator::make($params, [
'email' => 'bail|required|email|max:60|exists:customers,email',
'password' => 'required|max:60|string',
'password' => new CustomerCheckPassword($params['email']),
]) -> fails())
throw new Exception('message here');
Works like a charm, except bail rule from email attribute does not stop the validation when $params['email'] is not contained by customers.email, and goes on to password as well. Do you have any ideas or elegant workarounds for this issue?
In a different section of the same docs page (it won't link directly to it), it explains that bail only applies to the current attribute. So for instance, if email is missing, it will not check anything after required, but password is a new attribute so it will validate it as normal.
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:
One way to accomplish this would be to use the sometimes method
Validator::make($params, [
'email' => 'bail|required|email|max:60|exists:customers,email',
'password' => 'required|max:60|string',
])->sometimes('password', new CustomerCheckPassword($params['email']), function ($input) {
// Check input to decide if rule should execute
});

Laravel customise logic for login/password reset

I need to customise the logic for Laravel's authentication. I found a solution on SO, by overriding the credentials method in the LoginController like so:
return ['email' => $request->{$this->username()}, 'password' => $request->password, 'status_id' => $whatever];
But now I discovered an issue where a user can still reset their password and then they get signed in automatically. So how can I disable password resets for users who should not be allowed to sign in?
There is a method on the ResetsPasswords trait called resetPassword()
Override this in your Auth/ResetPasswordController and replace the line
$this->guard()->login($user);
with whatever functionality you want to achieve after the password reset.
If you want to prevent a disabled user from resetting their password, use a middleware to check if the account is disabled before continuing with the password reset.
You can create a new column to your user table isbanned that excepts only boolean value. And check further of this column value of a user that requests for reset password. If the value is TRUE, don't give the reset link, otherwise, give it.
Below you can see this example:
if (Auth::attempt(array('phone' => $request->input('phone'), 'password' => $request->input('password'), 'isactive' => '1', 'isbannes' => '0'), $remember)){
// your logic
}
Hope this helps you.

How Can I Add a Custom Password Validation in Laravels default Forgot Password System?

I want to change the validation for passwords when using Laravels default Forgot Password System. I need to do this so I can replace the password validation rule with this validation rule:
'password' => 'required|min:6|max:15|confirmed',
Specifically, I need to add the min/max changes.
However, I don't see anywhere in the ForgotPasswordController or ResetPasswordController where I can do this....
I'm using Laravel 5.3.
You can do this in your ResetPasswordController just override the rules method.
protected function rules()
{
return [
'token' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed|min:6',
];
}
Hope this helps
Since you're dealing with password data. You don't consider length of the password but also a valid format like password must not have any strange special characters or even space/blank character.
I want to share with you our basic solution yet effective that we have and been using it for many projects we built.
Create a custom Validator and register it by doing stated below.
Create a custom validator for password format, created a customClass for our customvalidator named "CustomValidator" extends the Illuminate\Validation\Validator class to your customize class.
public function validatePasswordFormat($attribute,$value,$parameters){
return preg_match(("/^(?=.)[A-Za-z\d][A-Za-z\d!##$%^&()_+]{2,25}$/"),$value);}
alphanumeric with special characters !##$%^&*()_+
2-25 range of characters only. you can change this anytime.
Boot your Custom Validator to the app service Providers.
public function boot(){
Validator::resolver(function($translator,$data,$rules,$messages){
return new CustomValidator($translator,$data,$rules,$messages);
});
}
Use is easily by doing in your request like this.
'password' => "required|password_format"
and then in your message response for error.
'password_format' => "Invalid password format. blah blah blah"
I hope this will help you.

Ignore category in update form

Im updating a category name, but i need to be unique and also case is the current record let it update, but it is not working my validation.
Example:
$this->validate($request, array(
'category' => 'required|unique:categories,name,:id|min:2'
));
From unique() rule description:
Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. Of course, you will want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address.
To instruct the validator to ignore the user's ID, we'll use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the | character to delimit the rules:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);

How to use required_unless in laravel

I have gone through the validations in Laravel. I have taken so many validation rules from Laravel Validations Rules
I want to user required_unless rule for following conditions.
$rules = array(
'facebookID' => 'alpha_num',
'googleID' => 'alpha_num',
'email' => 'required_unless:facebookID,null|required_unless:facebookID,null|email|max:32',
'password' => 'required_unless:facebookID,""|required_unless:googleID,""|max:20',
);
I want to add validation rule of email and password is only required if signup with facebook or google is not attempted.
I believe we can use required_unless or required_if
Please help me to solve this problem.
The laravel validation rule only accept value, that's mean required_unless:facebookID,null is evaluated as: the field is required unless facebookID='null'. So it's not a case you would need.
My suggest solution is using require_without_all:
'email' => 'required_without_all:facebookID,googleID',
'password' => 'required_without_all:facebookID,googleID'
The reference link for you: https://laravel.com/docs/5.1/validation#rule-required-without-all
Regards

Categories