Laravel Vuejs Ajax Validation Tutorial With Example

Ajax Laravel Programming Technology VueJs

Today, We want to share with you Laravel Vuejs Ajax Validation Tutorial.In this post we will show you Simple Laravel Vuejs Form Validation Example, hear for file upload size limit validation in vuejs we will give you demo and example for implement.In this post, we will learn about Image upload and validation using Laravel and VueJs with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Laravel Vuejs Ajax Validation Tutorial

There are the Following The simple About Laravel Vuejs Ajax Validation Tutorial Full Information With Example and source code.

As I will cover this Post with live Working example to develop Vuejs Form Validation Tutorial With Example From Scratch , so the someSimple Laravel Vuejs Form Validation for this example is following below.

Step 1 : Create Vuejs components

resources/assets/js/components/FileUpload.vue

<h2>Image upload and validation using Laravel and VueJs</h2>
<template>
    <div class="row">
        <div class="col-md-12">
            <div class="col-md-2">
                <img :src="image" class="pakainfo img-responsive">
            </div>
            <div class="col-md-8">
                <input type="file" v-on:change="onFileChange" class="pakainfo form-control">
            </div>
            <div class="col-md-2">
                <button class="pakainfo btn btn-success btn-block" @click="upload">Upload</button>
            </div>
        </div>
    </div>
</template>
<style scoped>
    img{
        max-height: 38px;
    }
</style>
<script>
    export default{
        data(){
            return {
                image: ''
            }
        },
        methods: {
            onFileChange(e) {
                let myfiles = e.target.files || e.dataTransfer.files;
                if (!myfiles.length)
                    return;
                this.livepreviewImg(myfiles[0]);
            },
            livepreviewImg(datafile) {
                let filereader = new FileReader();
                let vdatam = this;
                filereader.onload = (e) => {
                    vdatam.image = e.target.result;
                };
                filereader.readAsDataURL(datafile);
            },
            upload(){
                axios.post('Base_URL/api/upload',{image: this.image}).then(response => {

                });
            }
        }
    }
</script>

Step 2: register vuejs

FileUpload component Simple register it in app.js

Vue.component('file-upload',require('./components/FileUpload.vue'));

vuejs registered our component

<file-upload></file-upload>

Step 3: Laravel View Files

welcome.blade.php

@extends('layouts.app')

@section('content')
<h1>Image upload and validation using Laravel and VueJs</h1>
<div class="pakainfo container">
    <div class="pakainfo row">
        <div class="pakainfo col-md-8 col-md-offset-2">
            <div class="pakainfo panel panel-default">
                <div class="pakainfo panel-heading">Upload your Members Profile image!</div>
                <div class="pakainfo panel-body">
                    <file-upload></file-upload>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Setp 4 : Define a Laravel Routes

Laravel Route(axios post method)

Route::post('/upload', function (Request $datarequest) {
    $validator = Validator::make($datarequest->all(), [
        'image' => 'required|image64:jpeg,jpg,png'
    ]);
    if ($validator->fails()) {
        return response()->json(['errors'=>$validator->errors()]);
    } else {
        $allImgData = $datarequest->get('image');
        $myfileName = Carbon::now()->timestamp . '_' . uniqid() . '.' . explode('/', explode(':', substr($allImgData, 0, strpos($allImgData, ';')))[1])[1];
        Image::make($datarequest->get('image'))->save(public_path('uploads/').$myfileName);
        return response()->json(['error'=>false]);
    }
});

AppServiceProvider

public function boot()
{
    Validator::extend('image64', function ($attribute, $value, $allparamval, $validator) {
        $type = explode('/', explode(':', substr($value, 0, strpos($value, ';')))[1])[1];
        if (in_array($type, $allparamval)) {
            return true;
        }
        return false;
    });

    Validator::replacer('image64', function($message, $attribute, $rule, $allparamval) {
        return str_replace(':values',join(",",$allparamval),$message);
    });
}

validation.php

'image64' => 'The :attribute must be a Live file of type: :values.',

'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
    ],

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Vuejs Ajax Validation Tutorial.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Laravel 6 Set Extra where condition Examples
  2.   Generate Secure Random Strings using Laravel
  3.   Laravel Vue JS ajax file upload multipart form data
  4.   Vue Toggle Switch Component ON-OFF
  5.   Vue js Laravel Image Upload Example Tutorial From Scratch
  6.   Send HTML Form Data as JSON via Ajax
  7.   Preview Image before Upload using vuejs
  8.   How to Merge Two Eloquent Collections in Laravel?
  9.   Vue js Axios File Upload using PHP
  10.   authentication login with username or email in laravel

Leave a Reply

Your email address will not be published. Required fields are marked *