Validate non-mandatory fields using Laravel Validation 5.8

Asked

Viewed 331 times

1

I would like to validate a field and make that field not mandatory.

I know that for him to be obligatory I must wear required but I didn’t use it, since I only wanted to validate an email if it was added by the user, so I used validation 'email'.

But it turns out that even I not using the required it by default forces the user to put a value in the field thus leaving the required field. To make it simpler I will put a code below:

namespace App\Http\Requests\Dashboard;

use Illuminate\Foundation\Http\FormRequest;


class FormEditarUsuario extends FormRequest
{

    public function authorize()
    {
        return true;
    }


    public function rules()
    {
        // dd('ok');

        return [
            'email' => 'email'
        ];
    }

    public function messages()
    {

        return [
            'email.email' => 'Campo email inválido'

        ];

    }

}

Summarizing wanted to validate, but in case the user does not put any value in the field it does not return a validation error!

NOTE: I have tried using min:0

1 answer

2


  • obg cara pior q euli the documentation, but as I was programming at dawn and I was no longer reading thing with thing I think q passed beaten, but thanks ai helped a lot hug brother!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.