简体   繁体   中英

Laravel, return json error on duplicated inputs

How can I show message on error duplicates? the form does not complete the create process because of duplicate but its just freezing without showing any message, but if I left the fields empty then it shows that those are required在此处输入图像描述

public function store(Request $request)
{
    $validator = \Validator::make($request->all(), [
        'name'=>'required|unique:countires',
        'code'=>'required|unique:countires'
    ]);

    if ($validator->fails()) {
        return response()->json(['errors' => $validator->errors()->all()]);
    }

    $this->SourceData->storeData($request->all());
    return response()->json(['success'=>'Added successfully']);
}

ajax

$.ajax({
    url: $('#archiveurl').attr('href'),
    method: 'post',
    data: $("#createform").serialize(),

    beforeSend: function( xhr ) {
      Command: toastr["info"]("Uploading Data ...", "Sending Request");
    },

    success: function(result) {
        toastr.clear();
        if(result.errors) {
            $('.alert-danger').html('');
            $.each(result.errors, function(key, value) {
                $('.alert-danger').show();
                $('.alert-danger').append('<strong><li>'+value+'</li></strong>');
            });
        } else {
            $('.alert-danger').hide();
            $('.alert-success').show();
            Command: toastr ["success"] ("Added successfully", "Added status",{ timeOut: 900 });
            $('.clear_this input[type="text"]').val('');        

            $('.datatable').DataTable().ajax.reload();                      
        }
public function store(Request $request)
{
    $validator = Validator::make($request->all(), [
         'name' => 'required|unique:countries,name',
        'code' => 'required|unique:countries,code', 
    ]);

    if ($validator->fails()) {
        $errors = $validator->errors();
            return response()->json(['status' => false, 'errors' => $errors]);
    }

    $this->SourceData->storeData($request->all());
    return response()->json(['success'=>'Added successfully']);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM