moses toh
moses toh

Reputation: 13192

How can I do automatic validation email laravel?

I use laravel 5.3

My view blade like this :

<form id="form-register" role="form" method="POST" action="{{ url('/register') }}">
    ...
    <div class="{{$errors->has('email')?'form-group has-error': 'form-group'}}">
        {{Form::label('email', 'Email'), ['class' => 'control-label'])}}
        {{Form::email('email', old('email'), ['class' => 'form-control', 'id' => 'email')}}
        @if ($errors->has('email'))
            <span class="error error-server help-block">Please Enter Your Valid Email</span>
        @endif
    </div>
    ...
</form>

My validation like this :

public function rules()
{
    return [
        ...
        'email' => 'required|email|max:255|unique:users'
    ];
}

It works

If I enter an email that is already in the database table, then I submit, it will appear message validation email invalid

But I want to change it to automatic validation

So if the user inputs an existing email in the database table, it will automatically appear message validation email invalid

How can I do that?

Upvotes: 0

Views: 220

Answers (2)

Emeka Mbah
Emeka Mbah

Reputation: 17553

If I understand you correctly you want to display the exact error message returned by Validator for email field

You can get the first error message returned for email by call this $errors->first('email')

Here is an example

@if ($errors->has('email'))
  <span class="error error-server help-block">{{$errors->first('email')}}</span>
@endif

Upvotes: 0

Bara&#39; ayyash
Bara&#39; ayyash

Reputation: 1925

on your input changes, you need to send an Ajax request to the backend, this request will reach an API that will check the existing of the requested email.

Upvotes: 1

Related Questions