element UI input 验证

这是一个基于 Vue.js 的后台管理系统界面模板,包含新增、搜索、编辑管理员功能。通过表格展示管理员账号、名称、登录IP、权限组、添加时间和状态。同时,提供了密码验证、角色选择和状态切换等交互操作,便于进行后台管理。
摘要由CSDN通过智能技术生成
<template>
  <div class="group">
    <div class="center gtop">
      <el-button type="primary" icon="el-icon-plus" @click="act('add')">新增管理员</el-button>
      <div class="center rtright">
        账号:
        <el-input style="width: 180px;margin: 0 15px 0 5px;"></el-input>
        <el-button type="danger" icon="el-icon-search">搜索</el-button>
      </div>
    </div>
    <el-table :data="list">
      <el-table-column
        prop="phone"
        label="账号">
      </el-table-column>
      <el-table-column
        prop="name"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="ip"
        label="登录IP">
      </el-table-column>
      <el-table-column
        label="权限组">
        <template slot-scope="scope">
          <div v-for="item in scope.row.role" :key="item.role_id">{{item.role_name}}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="created_at"
        label="添加时间">
      </el-table-column>
      <el-table-column
        label="状态">
        <template slot-scope="scope">{{scope.row.status == 1 ? '可用' : '不可用'}}</template>
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button @click="act('edit',scope.row)" type="text">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="40%">
      <el-form ref="form" :model="actParams" label-width="120px" :rules="rules">
        <el-form-item label="名称:" prop="name">
          <el-input clearable style="width: 70%;" v-model="actParams.name"></el-input>
        </el-form-item>
        <el-form-item label="电话:" prop="phone">
          <el-input style="width: 70%;" type="text" auto-complete="on" autofocus placeholder="请输入您的手机号" v-model="actParams.phone"></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input clearable style="width: 70%;" v-model="actParams.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码:" prop="password_confirmation">
          <el-input clearable style="width: 70%;" v-model="actParams.password_confirmation" show-password @blur="change_password_confirmation"></el-input>
        </el-form-item>
        <el-form-item label="所属分组:">
          <el-checkbox-group v-model="actParams.role">
            <el-checkbox :label="item.name" v-for="item in roleList" :key="item.id"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="状态:">
          <el-radio-group v-model="actParams.status">
            <el-radio :label="0">不可用</el-radio>
            <el-radio :label="1">可用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.actParams.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
    };
    return {
      list: [],
      roleParams: {
        name: '',
        status: '',
      },
      roleList: [],
      dialogVisible: false,
      actParams: {
        id: '',
        name: '',
        phone: '',
        password: '',
        password_confirmation: '',
        role: [],
        status: 0,
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入电话', trigger: 'blur' },{ min: 11, max: 11, message: '请输入11为电话号码', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' },{ min: 6, message: '密码长度不能少于6位', trigger: 'blur' }],
        password_confirmation: [{ required: true, message: '请确认密码', trigger: 'blur' },{ validator: validatePass2, trigger: 'blur' }],
      }
    }
  },
  methods: {
    change_password_confirmation() {
      console.log('event',this.actParams.password_confirmation)
    },
    confirm() {
      console.log('ddss', this.actParams)
    },
    act(panduan, item) {
      this.dialogVisible = true
    },
    getRoleList() {
      this.$get('/role/list', this.roleParams, res => {
        if(res.code == 200) {
          this.roleList = res.data.list
          console.log('管理员组', this.roleList)
        } else {
          this.$message({type: 'error', message: res.mgs || '获取管理员组失败!'})
        }
      })
    },
    getData() {
      this.$get('/merchant/list', {}, res => {
        console.log('管理员列表', res)
        if(res.code == 200) {
          this.list = res.data.data
        }
      })
    }
  },
  created() {
    this.getData()
    this.getRoleList()
  }
}
</script>
<style scoped>
.group {padding: 30px 20px; box-sizing: border-box;font-size: 14px;color: #2f2f2f;background-color: #fff;}
.center {display: flex;justify-content: space-between;align-items: center;}
.gtop {width: 100%;}
</style>
Element UI是一个基于Vue.js的组件库,提供了丰富的UI组件和交互效果。在Element UI中,表单的输入验证是非常重要的一部分。 要实现Element UI的表单输入验证,可以通过使用表单组件提供的规则属性来进行设置。我们可以通过定义各种校验规则,如required(必填)、min(最小输入长度)、max(最大输入长度)、email(邮箱格式)、pattern(自定义正则表达式)等来实现表单输入验证。 脱离表单输入验证,意味着我们不再使用Element UI提供的表单组件的规则属性来实现验证。在这种情况下,我们需要自己编写逻辑代码来验证用户的输入。 可以通过监听表单的提交事件,在提交前对用户的输入进行验证。例如,可以使用JavaScript的正则表达式来验证输入的格式是否符合要求。如果验证不通过,可以阻止表单的提交,给出相应的提示信息。 另一种方法是使用自定义指令来实现验证。在这种情况下,我们可以通过在输入框上应用自定义指令,在指令中编写验证的逻辑。自定义指令可以监听元素的事件,如输入、失去焦点等,并在相应事件发生时进行验证。 总的来说,虽然Element UI提供了方便的验证规则属性,但是我们也可以通过编写逻辑代码或自定义指令来实现脱离表单输入验证。通过这种方式,我们可以实现更加灵活和个性化的验证方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值