Skip to content

Commit

Permalink
refactor: adjust validator in login page
Browse files Browse the repository at this point in the history
  • Loading branch information
lbwa committed Oct 5, 2018
1 parent 0a0b6ca commit 7ca4ab7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/view/Login/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</template>

<script>
import { validateUsername, validatePassword } from './validator'
import { validator } from './validator'
import { userLogin } from 'SERVICES'
export default {
Expand All @@ -55,16 +55,16 @@ export default {
username: [
{
// Set `this` value into vueComponent
validator: validateUsername.bind(this),
validator: validator.bind(this),
message: '请输入用户名',
trigger: 'blur'
}
],
password: [
{
validator: validatePassword.bind(this),
validator: validator.bind(this),
message: '请输入密码',
trigger: 'change'
trigger: 'blur'
}
]
}
Expand Down
24 changes: 3 additions & 21 deletions src/view/Login/validator.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
function validateUsername (rule, value, cb) {
const login = this.$refs.login
if (!value) {
cb(new TypeError(rule.message))
return
}
// Invoke vueComponent inner methods
if (login.password !== '') {
login.validateField('password')
}
cb()
}

function validatePassword (rule, value, cb) {
if (!value) {
cb(new TypeError(rule.message))
return
}
cb()
function validator (rule, value, cb) {
value ? setTimeout(() => cb(), 0) : cb(new Error(rule.message))
}

export {
validateUsername,
validatePassword
validator
}

0 comments on commit 7ca4ab7

Please sign in to comment.