> For the complete documentation index, see [llms.txt](https://devworkshops.gitbook.io/masteringvuejs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devworkshops.gitbook.io/masteringvuejs/dynamic-forms-and-validation/simple-validation.md).

# Simple Validation

## Add validation checks

The first step is to create a `validate` method that will run validation checks against a given category. Any failed validation checks will result in user-friendly messages being added to an `errors` object. These messages can be displayed, and then corrected by the user.

Within **CategoryList.vue**, add a new `errors` object to the `data` property:

{% code title="CategoryList.vue" %}

```javascript
...
data() {
        return {
            errors: null,
...
```

{% endcode %}

Then add a `validate` method as follows:

{% code title="CategoryList.vue" %}

```javascript
...
methods: {
    validate(category) {
        this.errors = {}

        if (!category.name || !category.name.trim()) {
            this.errors.name = 'Name is a required field'
        }
        if (!category.description || !category.description.trim()) {
            this.errors.description = 'Description is a required field'
        }

        // If no errors added, set errors to null
        if (Object.keys(this.errors).length === 0) {
            this.errors = null
        }
    },
...
```

{% endcode %}

Run the validation checks when adding a new category:

{% code title="CategoryList.vue" %}

```javascript
...
add() {
    this.validate(this.addingCategory)
    if (this.errors) {
        return
    }
...
```

{% endcode %}

## Display validation messages

With validation checks completed, you simply need to update the template to display validation error messages. Update template as follows:

{% code title="CategoryList.vue" %}

```markup
...
<template slot="bottom-row">
  <td>
    <input type="text" v-model="addingCategory.name" 
      placeholder="Name..." class="form-control"
      :class="{ 'is-invalid': errors && errors.name }">
    <div class="invalid-feedback" v-if="errors && errors.name">
      {{ errors.name }}
    </div>
  </td>
  <td>
    <input type="text" v-model="addingCategory.description" 
      placeholder="Description..." class="form-control"
      :class="{ 'is-invalid': errors && errors.description }">
    <div class="invalid-feedback" v-if="errors && errors.description">
      {{ errors.description }}
    </div>
  </td>
...
```

{% endcode %}

Save changes and verify that invalid categories cannot be added to the list:

![](/files/-Lg50MPmJQpkKJOpgKQM)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://devworkshops.gitbook.io/masteringvuejs/dynamic-forms-and-validation/simple-validation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
