Skip to content
Advertisement

how to exclude a rule in laravel validation

I am building a user’s resignation inside the dashboard with Form Request Validation. it works well in store resource, but I need to exclude the password role in updating resource, so if the user leaves it empty I will store the old password

my validation file

namespace AppHttpRequests;
use IlluminateFoundationHttpFormRequest;

class UsersRequest extends FormRequest
{
    public function rules()
    {
        return [
            'avatar' => ['image', 'mimes:jpeg,jpg,png', 'max:700'],
            'second_name' => ['required', 'max:60'],
            'password' => ['required', 'confirmed', 'max:255'],

Advertisement

Answer

In this case just use 'sometimes' for updating password and not 'required'.

public function rules()
    {
        return [
            'avatar' => ['image', 'mimes:jpeg,jpg,png', 'max:700'],
            'second_name' => ['required', 'max:60'],
            'password' => ['sometimes', 'confirmed', 'max:255'],

Validating when present see docs here: sometimes rule laravel

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement