let data2 ;
let datasend ;
input_login.onblur = async () => {
if(input_login.value != "") {
let checkLogin = input_login.value;
let _token = $("input[name='_token']").val();
let response = await fetch("/email-exist", {
method: 'POST',
headers:{"Content-Type":'application/json'},
body:JSON.stringify({
"_token":_token,
"checkLogin":checkLogin
})
})
datasend = await response.json();
console.log(datasend)
}}
Route::post('/email-exist', 'ExistEmailController@CheckingLogin');
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Validator;
class ExistEmailController extends Controller
{
public function CheckingLogin(Request $req) {
$rules = [
'checkLogin' => 'required|min:5|max:50'
];
$validatedData = Validator::make($req->checkLogin, $rules);
if($validatedData->fails()) {
return response()->json($validatedData->messages(), 200);
}
}
}
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'post' => 'required|numeric|exists:blog_posts,id',
'content' => 'required|min:2',
];
}
public function messages(): array
{
return [
'post.required' => 'Ошибка идентификации поста',
'post.exists' => 'Ошибка идентификации поста',
'post.numeric' => 'Ошибка идентификации поста',
'content.required' => 'Текст комментария не может быть пустым',
'content.min' => 'Слишком короткий комментарий',
];
}
public function failedValidation(Validator $validator): void
{
$response = [];
$errors = (new ValidationException($validator))->errors();
foreach ($errors as $element) {
foreach ($element as $message) {
$response[] = $message;
}
}
throw new HttpResponseException(
response()->json($response, JsonResponse::HTTP_UNPROCESSABLE_ENTITY)
);
}