Vue implements small form validation function

Vue implements small form validation function

This article example shares the specific code of Vue to implement form validation for your reference. The specific content is as follows

1. Route jump

First, open the src directory in the Vue project to configure the router file, then use import to expose your form page name and register the routing table in your Router instance. The code is as follows

import Create from "@/views/create/create.vue";

//The first letter of the exposed name should be capitalized. The following is the directory where your form page is located @ is the abbreviation of .., which means returning to the previous level const router = new Router ({
mode:"history"//This is the route mode. routes:[
{
      path: "/create", //Default is /. If there are multiple paths, it is / plus the path name: "create",
      component: Create,
      title: "Form",
    },
]
})

After the routing table is configured, remember to configure the to option of your router-link tag in the home page.

<router-link :to="{ name: 'create' }" class="collection">Form</router-link>

Then the form page

Rendering

The function implementation code is as follows

The plugin uses element.ui. You can use npm i element-ui in the terminal. After successful installation, check it in package.json and reference it in main.js

After the installation is complete, you can use it.

<template>
  <div class="create">
    <h2>Welcome to publish a new recipe, please introduce your masterpiece first! </h2>
    <section class="create-introduce">
      <h5>Title</h5>
 
      <el-input
        v-model="backData.title"
        class="create-input"
        placeholder="Please enter content"
      ></el-input>
      <h5>Attributes</h5>
      <div>
        <el-select
          v-for="item in propertyies"
          :key="item.parent_name"
          :placeholder="item.parent_name"
          v-model="backData.property[item.title]"
        >
          <el-option
            v-for="option in item.list"
            :key="option.type"
            :label="option.name"
            :value="option.type"
          >
          </el-option>
        </el-select>
      </div>
      <h5>Recipe Categories</h5>
      <div>
        <el-select placeholder="Please select the recipe category" v-model="backData.classify">
          <el-option-group
            v-for="group in classifies"
            :key="group.parent_type"
            :label="group.parent_name"
          >
            <el-option
              v-for="item in group.list"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-option-group>
        </el-select>
      </div>
      <h5>Finished product picture (328*440)</h5>
      <div class="upload-img-box clearfix">
        <div class="upload-img">
          <upload-img
            action="/api/upload?type=product"
            :img-url="backData.product_pic_url"
            @res-url="
              (data) => {
                backData, (product_pic_url = data.res);
              }
            "
          ></upload-img>
        </div>
        <el-input
          class="introduce-text"
          type="textarea"
          :rows="10"
          placeholder="Please enter content"
        >
        </el-input>
      </div>
    </section>
 
    <h2>Record all raw materials</h2>
    <section class="create-introduce">
      <h5>Main ingredients</h5>
      <!--[ { "name": "", "specs": "" }, { "name": "", "specs": "" }, { "name": "", "specs": "" } ]-->
      <Stuff v-model="backData.raw_material.main_material"></Stuff>
      <h5>Accessories</h5>
      <Stuff v-model="backData.raw_material.accessories_material"></Stuff>
    </section>
 
    <h2>Start writing the steps! Whether it is easy to learn depends on how you write it. Good luck! </h2>
    <section class="create-introduce">
      <Upload v-for="(item, index) in 3" :key="index"></Upload>
      <el-button
        class="eaeaea add-step-button"
        type="primary"
        size="medium"
        icon="el-icon-plus"
        @click="add"
        >Add one step</el-button
      >
      <h5>Cooking tips</h5>
      <el-input
        class="introduce-text"
        type="textarea"
        :rows="8"
        placeholder="Share your experience and tips in making this dish!"
      >
      </el-input>
    </section>
 
    <el-button class="send" type="primary" size="medium" :icon="icon"
      >Done, submit for review</el-button
    >
  </div>
</template>
<script>
import Stuff from "./stuff";
import Upload from "./step-upload";
import UploadImg from "@/components/upload-img";
import { getProperty, getClassify, publish } from "@/service/api";
 
const raw_materia_struct = {
  name: "",
  specs: "",
};
export default {
  name: "create",
  components: { Stuff, Upload, UploadImg },
  data() {
    return {
      backData: {
        title: "",
        property: {},
        classify: "",
        product_pic_url: "",
        product_story: "",
        raw_material: {
          raw_material: Array(3)
            .fill(1)
            .map(() => ({ ...raw_materia_struct })),
          accessories_material: Array(3)
            .fill(1)
            .map(() => ({ ...raw_materia_struct })),
        },
      },
      propertyies: [],
      classifies: [],
    };
  },
  mounted() {
    getProperty().then(({ data }) => {
      console.log(data);
      this.propertyies = data;
      this.backData.property = data.reduce((o, item) => {
        o[item.title] = "";
        return o;
      }, {});
      // console.log(data);
      // console.log(this.backData.property)
    });
    getClassify().then(({ data }) => {
      console.log(data);
      this.classifies = data;
    });
  },
  methods: {
    add() {
      console.log(1);
    },
  },
};
</script>
<style lang="stylus">
 
 
.create-introduce
  background-color #fff
  padding 20px
 
 
  .add-step-button
    margin-left 100px
 
 
.create
  width 100%
  h2
    text-align center
    margin 20px 0
  .send
    // ff3232()
    height: 70px;
    width: 220px;
    background #ff3232
    color #fff
    border none
    margin 20px auto
    display block
 
  h5
    margin 20px 0
 
 
.create-input input
  width 446px
  line-height 22px
.upload-img-box
  .upload-img
    float left
  .introduce-text
    float left
  .el-textarea
    width 60%
    margin-left 10px
</style>

The above is all about the Vue form.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Do you really know how to do Vue form validation? vue form validation (form) validate
  • Detailed explanation of how to use Vue Validator, a Vue form validation plugin
  • Problems encountered in Form validation of Vue ElementUI
  • Vue+elementUI implements form and image upload and verification function example
  • Form item validation method in v-for loop when using Element component in Vue
  • Vue quickly implements universal form validation function
  • Implementation of Vue elementui form validation
  • Vue uses element-ui to implement form validation
  • Vue implements form validation function
  • Vue implements form to remove a field validation separately

<<:  How to deploy a simple c/c++ program using docker

>>:  mysql implements importing only a specified table from the sql file of exported data

Recommend

How to install Docker on Windows Server 2016

Recently Microsoft released Windows Server 2016, ...

HTML table tag tutorial (31): cell width and height attributes WIDTH, HEIGHT

By default, the width and height of the cell are ...

In-depth understanding of javascript prototype and prototype chain

Table of contents 1. What is a prototype? 2. Prot...

mysql delete multi-table connection deletion function

Deleting a single table: DELETE FROM tableName WH...

Solutions to invalid is Null segment judgment and IFNULL() failure in MySql

MySql Null field judgment and IFNULL failure proc...

How to use lodop print control in Vue to achieve browser compatible printing

Preface This control will have a watermark at the...

View MySQL installation information under Linux server

View the installation information of mysql: #ps -...

Installing MySQL 8.0.12 based on Windows

This tutorial is only applicable to Windows syste...

Example of how to create and run multiple MySQL containers in Docker

1. Use the mysql/mysql-server:latest image to qui...

MySQL 8.0.20 winx64 installation and configuration method graphic tutorial

This article shares with you the installation and...

MySQL Database Basics SQL Window Function Example Analysis Tutorial

Table of contents Introduction Introduction Aggre...

WeChat applet tab left and right sliding switch function implementation code

Effect picture: 1. Introduction Your own applet n...