As a beginner in Vue, I'm writing code to connect to an application using Vue/Quasar/C#. However, I am struggling to understand how rules are executed. The specific code snippet I have written is aimed at checking if the input fields for passwords are not empty.
<q-form v-bind:submit="createUser"
v-bind:reset="resetCreateUser"
class="q-gutter-md"
v-if="status==2"
ref="frmCreateUser"
autofocus>
<q-input filled
v-model="loginData.password"
label="Votre mot de passe"
hint="Saisissez votre mot de passe"
v-bind:type="isPwd ? 'password' : ''"
lazy-rules
v-bind:rules="[ val => val && val.length > 0 || 'Saisissez votre mot de passe']"
ref="fldPasswordCreateUser"
data-vv-name="fldPasswordCreateUser">
<template v-slot:append>
<q-icon :name="isPwd ? 'visibility_off' : 'visibility'"
class="cursor-pointer"
v-on:click="isPwd = !isPwd"></q-icon>
</template>
</q-input>
<q-input filled
v-model="loginData.passwordConfirm"
label="Confirmez votre mot de passe"
v-bind:type="isPwd ? 'password' : ''"
lazy-rules
v-bind:rules="[ val => val && val.length > 0 || 'Saisissez votre mot de passe']">
<template v-slot:append>
<q-icon :name="isPwd ? 'visibility_off' : 'visibility'"
class="cursor-pointer"
v-on:click="isPwd = !isPwd"></q-icon>
</template>
</q-input>
...
</q-form>
In my implementation, I am checking the password equality using the method
if (this.loginData.password == this.loginData.passwordConfirm) {...
. My intention is to modify the v-bind:rules attribute to display error messages like 'empty field' and 'Password not match', but I am encountering syntax errors consistently...
UPDATE
I attempted the following:
v-bind:rules="[val => val && val.length > 0 || 'saisissez quelque chose :)', val => val != $refs.fldPasswordChange || 'Mots de passe différents']"
Although I tried to implement two controls, it seems that the second one does not trigger as expected.
UPDATE2
Upon Dean's suggestion, I made further adjustments to the code:
<q-input filled
v-model="loginData.password"
label="Votre mot de passe"
hint="Saisissez votre mot de passe"
v-bind:type="isPwd ? 'password' : ''"
lazy-rules
v-bind:rules="Required"
ref="fldPasswordChange">
<template v-slot:append>
<q-icon :name="isPwd ? 'visibility_off' : 'visibility'"
class="cursor-pointer"
v-on:click="isPwd = !isPwd"></q-icon>
</template>
</q-input>
<q-input filled
v-model="loginData.passwordConfirm"
label="Confirmez votre mot de passe"
v-bind:type="isPwd ? 'password' : ''"
lazy-rules
v-bind:rules="ConfirmPWD"
ref="fldPasswordChangeConfirm">
<template v-slot:append>
<q-icon :name="isPwd ? 'visibility_off' : 'visibility'"
class="cursor-pointer"
v-on:click="isPwd = !isPwd"></q-icon>
</template>
</q-input>
Additionally, I adjusted the computed properties:
computed: {
ConfirmPWD() {
return [
(v) => !!v || "Saisissez quelquechose :-)",
(v) => v != this.$refs.fldPasswordChange.value || "Mots de passe différents"
]
},
Required() {
return [(v) => !!v || 'Saisissez quelque chose :-)']
}
},
However, the issue persists where the second control does not seem to trigger. If I leave the confirmPassword field empty, I receive an error message, but when I enter two different passwords, nothing happens. After debugging, I suspect there might be a syntax problem with my condition block when I include:
(v) => v != this.$refs.fldPasswordChange.value || "Mots de passe différents"
When this condition is present without (v) => !!v || "Saisissez quelquechose :-)", no error message is displayed.