Is there a way to set the required validation dynamicly? #1006
fdverwoerd
started this conversation in
General
Replies: 3 comments 5 replies
|
validation does not accept object notation, it is string or array, with that said this might work: :validation="[
...(cond ? [['required']] : []),
['length', 1, 5]
]" |
4 replies
|
I think I have a working version now :) import type { FormKitNode } from '@formkit/core';
/**
* File: lisy_required.ts
*
* This is a custom validation rule for FormKit. If the current args is required then do required check.
*/
const lisy_required = function (node: FormKitNode, ...args: any[]) {
if (args[0] === 'required') {
console.log(typeof node.value)
if (typeof node.value === 'string') {
console.log('length', node.value.length);
console.log('string type:', typeof node.value === 'string')
console.log('lenght=0:', node.value.length === 0);
}
return typeof node.value === 'string' && node.value.length !== 0;
} else {
// passing is true
return true;
}
}
lisy_required.skipEmpty = false;
export default lisy_required;And Vue template <FormKit v-if="lisyConfig.fieldOptions.fieldOptionsFamilyNumber !== 'hidden'"
type="text"
name="familyNumber"
id="familyNumber"
:validation="[
['lisy_required', lisyConfig.fieldOptions.fieldOptionsFamilyNumber],
['length', 1,5]
]"
label="Gezinsnummer"
outer-class="form-grid-column-12"
></FormKit> |
1 reply
|
Thank you very much! 💯 |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hello there! Love formkit!
I was looking for a way to set required dynamicly based on a setting in an object in Vue.:
The options are
required|optional|hidden.So when option is
requiredi need `validation="required|length:1,5".Is this possible? I tried using an object notation like so: but that wont work.
All reactions