0
0
Fork 0
This commit is contained in:
Javier Segarra 2024-06-21 15:04:38 +02:00
parent 0d718448e2
commit ccbfe7e0d9
3 changed files with 23 additions and 6 deletions

View File

@ -25,6 +25,7 @@ const $props = defineProps({
}); });
const { t } = useI18n(); const { t } = useI18n();
const requiredFieldRule = (val) => !!val || t('globals.fieldRequired');
const vnInputRef = ref(null); const vnInputRef = ref(null);
const value = computed({ const value = computed({
get() { get() {
@ -53,7 +54,8 @@ defineExpose({
focus, focus,
}); });
const inputRules = [ const mixinRules = [
requiredFieldRule,
(val) => { (val) => {
const { min } = vnInputRef.value.$attrs; const { min } = vnInputRef.value.$attrs;
if (min >= 0) if (Math.floor(val) < min) return t('inputMin', { value: min }); if (min >= 0) if (Math.floor(val) < min) return t('inputMin', { value: min });
@ -71,6 +73,7 @@ const inputRules = [
:class="{ required: $attrs.required }" :class="{ required: $attrs.required }"
@keyup.enter="emit('keyup.enter')" @keyup.enter="emit('keyup.enter')"
:clearable="false" :clearable="false"
:rules="$attrs.required || $attrs.min ? mixinRules : null"
:lazy-rules="true" :lazy-rules="true"
hide-bottom-space hide-bottom-space
> >

View File

@ -1,10 +1,10 @@
<script setup> <script setup>
import { ref, toRefs, computed, watch } from 'vue'; import { ref, toRefs, computed, watch } from 'vue';
import { onMounted } from 'vue'; import { onMounted } from 'vue';
import { useI18n } from 'vue-i18n';
import FetchData from 'src/components/FetchData.vue'; import FetchData from 'src/components/FetchData.vue';
import { useValidator } from 'src/composables/useValidator';
const { validations } = useValidator();
const emit = defineEmits(['update:modelValue', 'update:options']); const emit = defineEmits(['update:modelValue', 'update:options']);
const $props = defineProps({ const $props = defineProps({
modelValue: { modelValue: {
type: [String, Number, Object], type: [String, Number, Object],
@ -60,8 +60,7 @@ const $props = defineProps({
}, },
}); });
const { t } = useI18n(); const requiredFieldRule = (val) => validations({}).presence(val);
const requiredFieldRule = (val) => val ?? t('globals.fieldRequired');
const { optionLabel, optionValue, optionFilter, options, modelValue } = toRefs($props); const { optionLabel, optionValue, optionFilter, options, modelValue } = toRefs($props);
const myOptions = ref([]); const myOptions = ref([]);
@ -82,7 +81,9 @@ function setOptions(data) {
myOptions.value = JSON.parse(JSON.stringify(data)); myOptions.value = JSON.parse(JSON.stringify(data));
myOptionsOriginal.value = JSON.parse(JSON.stringify(data)); myOptionsOriginal.value = JSON.parse(JSON.stringify(data));
} }
onMounted(() => { onMounted(() => {
console.log(vnSelectRef.value.rules);
setOptions(options.value); setOptions(options.value);
if ($props.url && $props.modelValue) fetchFilter($props.modelValue); if ($props.url && $props.modelValue) fetchFilter($props.modelValue);
}); });
@ -147,6 +148,11 @@ watch(modelValue, (newValue) => {
if (!myOptions.value.some((option) => option[optionValue.value] == newValue)) if (!myOptions.value.some((option) => option[optionValue.value] == newValue))
fetchFilter(newValue); fetchFilter(newValue);
}); });
import { useAttrs } from 'vue';
const $attrs = useAttrs();
const mixinRules = () =>
$attrs.required ? [$attrs?.rules ?? [], requiredFieldRule] : null;
</script> </script>
<template> <template>
@ -173,6 +179,7 @@ watch(modelValue, (newValue) => {
fill-input fill-input
ref="vnSelectRef" ref="vnSelectRef"
lazy-rules lazy-rules
:rules="mixinRules()"
:class="{ required: $attrs.required }" :class="{ required: $attrs.required }"
virtual-scroll-slice-size="options.length" virtual-scroll-slice-size="options.length"
> >

View File

@ -28,7 +28,7 @@ export function useValidator() {
} }
const { t } = useI18n(); const { t } = useI18n();
const validations = function (validation) { const validations = function (validation = {}) {
return { return {
format: (value) => { format: (value) => {
const { allowNull, with: format, allowBlank } = validation; const { allowNull, with: format, allowBlank } = validation;
@ -71,12 +71,19 @@ export function useValidator() {
return validator.isInt(value) || 'Value should be integer'; return validator.isInt(value) || 'Value should be integer';
return validator.isNumeric(value) || 'Value should be a number'; return validator.isNumeric(value) || 'Value should be a number';
}, },
min: (value, min) => {
if (min >= 0)
if (Math.floor(value) < min) return t('inputMin', { value: min });
},
custom: (value) => validation.bindedFunction(value) || 'Invalid value', custom: (value) => validation.bindedFunction(value) || 'Invalid value',
}; };
}; };
// const requiredFieldRule = [(val) => validations({}).presence(val)];
return { return {
validate, validate,
// requiredFieldRule,
validations,
models, models,
}; };
} }