Password updates

pull/6632/head
tugcekucukoglu 2024-10-23 11:17:20 +03:00
parent 58c05c646d
commit 120e2c5051
1 changed files with 102 additions and 30 deletions

View File

@ -3,17 +3,17 @@
<p>InputText is used with the <i>v-model</i> property.</p> <p>InputText is used with the <i>v-model</i> property.</p>
</DocSectionText> </DocSectionText>
<div class="card flex justify-center"> <div class="card flex justify-center">
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4"> <Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-80">
<div class="flex flex-col gap-2"> <div class="flex flex-col gap-2">
<Password name="password" placeholder="Password" :feedback="false" /> <Password name="password" placeholder="Password" :feedback="false" fluid />
<template v-if="$form.password?.invalid"> <template v-if="$form.password?.invalid">
<Message v-for="(error, index) of $form.password.errors" :key="index" severity="error">{{ error.message }}</Message> <Message v-for="(error, index) of $form.password.errors" :key="index" severity="error">{{ error.message }}</Message>
</template> </template>
</div> </div>
<Button type="submit" severity="secondary" class="self-center p-2">Submit</Button> <Button type="submit" severity="secondary" label="Submit" />
</Form> </Form>
</div> </div>
<DocSectionCode :code="code" /> <DocSectionCode :code="code" :dependencies="{ zod: '3.23.8' }" />
</template> </template>
<script> <script>
@ -26,46 +26,84 @@ export default {
initialValues: { initialValues: {
password: '' password: ''
}, },
resolver: null, resolver: zodResolver(
schema: z.object({ z.object({
password: z password: z
.string() .string()
.min(3, { message: 'Password must be at least 3 characters long.' }) .min(3, { message: 'Minimum 3 characters.' })
.max(8, { message: 'Password must not exceed 8 characters.' }) .max(8, { message: 'Maximum 8 characters.' })
.refine((value) => /[a-z]/.test(value), { .refine((value) => /[a-z]/.test(value), {
message: 'Password must contain at least one lowercase letter.' message: 'Must have a lowercase letter.'
}) })
.refine((value) => /[A-Z]/.test(value), { .refine((value) => /[A-Z]/.test(value), {
message: 'Password must contain at least one uppercase letter.' message: 'Must have a uppercase letter.'
}) })
.refine((value) => /\d/.test(value), { .refine((value) => /\d/.test(value), {
message: 'Password must contain at least one number.' message: 'Must have a number.'
}) })
}), })
),
code: { code: {
basic: ` basic: `
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4"> <Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-80">
<div class="flex flex-col gap-2"> <div class="flex flex-col gap-2">
<Password name="password" placeholder="Password" :feedback="false" /> <Password name="password" placeholder="Password" :feedback="false" fluid />
<template v-if="$form.password?.invalid"> <template v-if="$form.password?.invalid">
<Message v-for="(error, index) of $form.password.errors" :key="index" severity="error">{{ error.message }}</Message> <Message v-for="(error, index) of $form.password.errors" :key="index" severity="error">{{ error.message }}</Message>
</template> </template>
</div> </div>
<Button type="submit" severity="secondary" class="self-center p-2">Submit</Button> <Button type="submit" severity="secondary" label="Submit" />
</Form> </Form>
`, `,
options: ` options: `
<template> <template>
<div class="card flex justify-center"> <div class="card flex justify-center">
<InputText type="text" v-model="value" /> <Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-80">
<div class="flex flex-col gap-2">
<Password name="password" placeholder="Password" :feedback="false" fluid />
<template v-if="$form.password?.invalid">
<Message v-for="(error, index) of $form.password.errors" :key="index" severity="error">{{ error.message }}</Message>
</template>
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div> </div>
</template> </template>
<script> <script>
import { zodResolver } from '@primevue/form/resolvers';
import { z } from 'zod';
export default { export default {
data() { data() {
return { return {
value: null initialValues: {
password: ''
},
resolver: zodResolver(
z.object({
password: z
.string()
.min(3, { message: 'Minimum 3 characters.' })
.max(8, { message: 'Maximum 8 characters.' })
.refine((value) => /[a-z]/.test(value), {
message: 'Must have a lowercase letter.'
})
.refine((value) => /[A-Z]/.test(value), {
message: 'Must have a uppercase letter.'
})
.refine((value) => /\d/.test(value), {
message: 'Must have a number.'
})
})
)
}
},
methods: {
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
} }
} }
} }
@ -75,22 +113,56 @@ export default {
composition: ` composition: `
<template> <template>
<div class="card flex justify-center"> <div class="card flex justify-center">
<InputText type="text" v-model="value" /> <Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-80">
<div class="flex flex-col gap-2">
<Password name="password" placeholder="Password" :feedback="false" fluid />
<template v-if="$form.password?.invalid">
<Message v-for="(error, index) of $form.password.errors" :key="index" severity="error">{{ error.message }}</Message>
</template>
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div> </div>
</template> </template>
<script setup> <script setup>
import { ref } from 'vue'; import { ref } from 'vue';
import { zodResolver } from '@primevue/form/resolvers';
import { useToast } from "primevue/usetoast";
import { z } from 'zod';
const value = ref(null); const toast = useToast();
const initialValues = ref({
password: ''
});
const resolver = ref(zodResolver(
z.object({
password: z
.string()
.min(3, { message: 'Minimum 3 characters.' })
.max(8, { message: 'Maximum 8 characters.' })
.refine((value) => /[a-z]/.test(value), {
message: 'Must have a lowercase letter.'
})
.refine((value) => /[A-Z]/.test(value), {
message: 'Must have a uppercase letter.'
})
.refine((value) => /\d/.test(value), {
message: 'Must have a number.'
})
})
));
const onFormSubmit = ({ valid }) => {
if (valid) {
toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
};
<\/script> <\/script>
` `
} }
}; };
}, },
mounted() {
this.resolver = zodResolver(this.schema);
},
methods: { methods: {
onFormSubmit({ valid }) { onFormSubmit({ valid }) {
if (valid) { if (valid) {