I am trying to make a vue component that checks if a given input is available or not in the database. The calls to the server i am doing with axios inside the vue component. for some reason, when i type the @
symbol in the email, it breaks.here is my code so far:
this is the backend.
class TestController extends Controller
{
public function verifyName($name){
if (request()->wantsJson()){
$names=User::where('name', $name)->count();
if ($names>0)
return 1;
if ($names==0)
return 0;
}
}
public function verifyEmail($email){
if (request()->wantsJson()){
$emails=User::where('email', $email)->count();
if ($emails>0)
return 1;
if ($emails==0)
return 0;
}
}
}
here is the vue script:
<script>
export default {
data(){
return{
name: '',
email: '',
nameTaken: false,
emailTaken: false,
nameAvailable:false,
emailAvailable:false,
nameIsTooShort:false
}
},
methods:{
verifyName(){
axios.get('/verify/'+this.name)
.then((response) => {
if (response.data){
this.nameTaken=true;
this.nameAvailable=false
}
if(!response.data){
this.nameTaken = false;
this.nameAvailable=true
}
});
},
verifyEmail(){
axios.get('/verify/'+this.email)
.then((response) => {
if (response.data){
this.emailTaken=true;
this.emailAvailable=false
}
if(!response.data){
this.emailTaken = false;
this.emailAvailable=true
}
});
}
}
}
</script>
and here are my routes:
Route::get('/verify/{name}','TestController@verifyName');
Route::get('/verify/{email}','TestController@verifyEmail');
and here is a small gif to show it.
as requested, here is the html part where i show the message.
<div class="field-body">
<div class="field">
<p class="control">
<input class="input" id="email" type="email" name="email" value="{{ old('email') }}" v-model="email" @change="verifyEmail">
<div class="help is-danger" v-show="emailTaken" v-cloak>This e mail is taken</div>
<div class="help is-success" v-show="emailAvailable" v-cloak>Great. this email is available</div>
</p>
</div>
</div>
Thanks.