PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 19, 2022

[FIXED] How can I show the validation messages in vform?

 April 19, 2022     axios, forms, laravel, validation, vue.js     No comments   

Issue

I have here a vue laravel spa that uses axios vform for displaying error validation messages. I currently use the axios vform 1.0.1, and I think got the syntax used correctly. However, I'm receiving errors in console after clicking submit, instead of displaying the validation messages.

Here's my markup and vue:

<script>
    import Form from 'vform'
    export default {
        data: () => ({
            form: new Form({
                name: '',
                description: ''
            }),
      }),
        methods: {
            addDesignation() {
                this.form
                    .post('/api/designations', this.form)
                    .then(response => (
                        this.$router.push({ name: 'designations' })
                    ))
                    .catch(err => console.log(err))
                    .finally(() => this.loading = false)
            }
        }
    }
</script>
<template>
    <main>
        <h3 class="text-2xl text-gray-800 font-bold leading-none mb-6">Create Designation</h3>
        <div class="px-5 py-6 shadow-sm rounded-md bg-white">
            <div class="row">
                <div class="col-md-6">
                    <form @submit.prevent="addDesignation">
                        <div class="mb-4">
                            <label class="block text-gray-700 text-sm font-bold mb-2 required" for="name">Name</label>
                            <input class="shadow appearance-none border rounded w-2/4 py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" name="name" id="name" type="text" placeholder="Name" tabindex="1" v-model="form.name" :class="{ 'invalid': form.errors.has('name') || form.errors.has('name') }" />
                        </div>
                        <div class="mb-4">
                            <label class="block text-gray-700 text-sm font-bold mb-2 required" for="description">Description</label>
                            <textarea rows="4" class="shadow appearance-none border rounded w-2/4 py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" name="description" id="description" placeholder="Description" tabindex="2" v-model="form.description" :class="{ 'invalid': form.errors.has('description') || form.errors.has('description') }"></textarea>
                        </div>
                        <button type="submit" class="sm:hidden md:flex bg-blue-500 hover:bg-blue-400 text-white font-bold py-2 px-4 border-b-4 border-blue-700 hover:border-blue-500 rounded outline-none focus:outline-none">Create</button>
                    </form>
                    <vue-progress-bar></vue-progress-bar>
                </div>
            </div>
        </div>
    </main>
</template>

Then this is the error message in console:

VM559:1 POST http://localhost:8000/api/designations 422 (Unprocessable Content)

Network was showing this:

message: "The given data was invalid.", errors: {description: ["The description field is required."]}}
errors: {description: ["The description field is required."]}
message: "The given data was invalid."


Solution

you should catch errors like this:

.catch(error => {
                this.errors = error.response.data.errors;
                // console.log(this.errors);
            })

and also

data(){
errors: {}
 }

And the error in network was sent by laravel. It is not there cause you log this

.catch(err => console.log(err))

To show the error of description below text area :

  <span
                    v-if="errors['description']"
                    role="alert"
                >{{ errors["description"]}}
               </span>


Answered By - Who Do You think am i
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing