반응형
어떻게 비동기적으로 Vuetify 텍스트 필드를 확인하기 위해?
Vuetify에서 텍스트 필드고 있다.rules
소품, 돌아오는 기능의 배열한다.true
아니면 오류 문자열입니다.그래서 유효성 검사 서버 측 XHR을 사용하여 만들어질 수 있는 그것들은 얼마나 async을 만드는 법?
같은 거:
<v-text-field :rules="[v => { axios.get('/check?value=' + val).then(() => { return true }) }]">
한가지 해결책은 설립하는 것이다.error-messages
소품:
<v-text-field v-model="input" :error-messages="errors">
그리고 사용하여watch
옵션:
new Vue({
data () {
return {
input: '',
errors: []
}
},
watch: {
input (val) {
axios.get('/check?value=' + val).then(valid => {
this.errors = valid ? [] : ['async error']
})
}
}
});
나는 만약 사용자 이름에 들어갔다 이미 존재하는 빌리기 위해서는 후위 유효성 검사 해야 한다.나는 JSON오픈 APIv3가 사용자 이름 값을 확인합니다는 메서드를 호출할 넘치는 클라이언트 라이브러리를 사용한다.
그래서 나는 이런 식으로 해결...
내 login.js 파일에서 나는:오류 메시지가 포함된 문자열 속성이 정의되어 있다.
import swaggerClient from "../remote-client";
const strict = true;
const state = {
hasError: false,
error: null,
usernameAlredyExists: ""
};
const getters = {
hasError: state => state.hasError,
error: state => state.error,
usernameAlredyExists: state => state.usernameAlredyExists
};
const mutations = {
checkingUsername(state) {
state.hasError = false;
state.error = null;
state.usernameAlredyExists = "";
},
usernameCheckedKO(state) {
state.usernameAlredyExists = "Username already exists";
},
usernameCheckedOK(state) {
state.usernameAlredyExists = "";
},
errorCheckingUsername(state, error) {
state.hasError = true;
state.error = error;
},
};
const actions = {
userLogin({ commit }, { username, password }) {
// not relevant code
},
checkUsername({ commit }, { username }) {
commit("checkingUsername");
swaggerClient.userSwaggerClient
.then(
function(client) {
return client.apis.UserHealthFacility.getHfUserUsernameWithUsername(
{ username: username },
{},
{}
);
},
function(reason) {
// failed to load the JSON specification
commit("errorCheckingUsername", reason);
}
)
.then(
function(callResult) {
if (callResult.body) {
commit("usernameCheckedKO");
} else {
commit("usernameCheckedOK");
}
},
function(reason) {
// failed to call the API method
commit("errorCheckingUsername", reason);
}
);
}
};
export default {
namespaced: true,
strict,
state,
getters,
mutations,
actions
};
그 후 로그인에.나는 이 코드를 가지뷰 파일:.
<v-card-text>
<v-form ref="loginForm" v-model="loginValid" lazy-validation>
<v-text-field
v-model="username"
label="Username"
:rules="[rules.required]"
:error-messages="usernameAlredyExists"
v-on:change="callCheckUsername"
></v-text-field>
<v-text-field
v-model="password"
:label="Password"
:append-icon="showPassword ? 'visibility_off' : 'visibility'"
:type="showPassword ? 'text' : 'password'"
:rules="[rules.required, rules.minLength]"
counter
@click:append="showPassword = !showPassword"
></v-text-field>
</v-form>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn
:disabled="!loginValid"
@click="callUserLogin"
color="primary"
round
>Login</v-btn>
</v-card-actions>
<script>
export default {
data() {
return {
username: "",
password: "",
showPassword: false,
loginValid: true,
rules: {
required: value => !!value || "Questo campo è obbligatorio",
minLength: value =>
value.length >= 8 || "Questo campo deve contenere almeno 8 caratteri"
}
};
},
computed: {
usernameAlredyExists() {
return this.$store.getters["login/usernameAlredyExists"];
}
},
methods: {
callUserLogin() {
if (this.$refs.loginForm.validate()) {
this.$store.dispatch("login/userLogin", {
username: this.username,
password: this.password
});
}
},
callCheckUsername(value) {
if (value) {
this.$store.dispatch("login/checkUsername", {
username: this.username
});
}
}
}
};
</script>
이러한 방법으로 잘 되는 것 같다.
참조URL:https://stackoverflow.com/questions/49132167/how-to-validate-vuetify-text-field-asynchronously
반응형
'IT이야기' 카테고리의 다른 글
C의 주 인수에 대한 인수 (0) | 2022.05.17 |
---|---|
Vuex/Vues의 작업에서 다른 작업을 호출하는 방법 (0) | 2022.05.17 |
Vuex mapState 경로와 경로 매개 변수에 근거한다. (0) | 2022.05.17 |
경고:수 을 왼쪽 시프트 형식의 탭 너비입니다. (0) | 2022.05.17 |
봄에 시작할 때 메서드 실행한다. (0) | 2022.05.17 |