Home > OS >  vuejs pass extra parameter to function
vuejs pass extra parameter to function

Time:01-15

Is it possible within Quasar Form rules to pass extra parameter? This had to do with the following template code:

<q-field outlined v-else-if="prop.component === 'checkbox'" 
   
  v-model="dataRef[key]"
  :label="prop.label"
  :rules="[isCheckLimit]"
  >
    <q-option-group
    style="margin-top:20px;"
      :options="prop.options"
      type="checkbox"
      v-model="dataRef[key]"
    />

</q-field>

The function:

  const isCheckLimit = (v) => {
    return v.length > 2 || t('checked-to-much')
  }

I want that number 2 to be dynamic instead of static, is it possible to pass for example a number to that function? I cant find any clear information about that?

Thank you in advance.

CodePudding user response:

You have to define your rule as function in your rules array, first lets update your validation function to accept second argument, I will also set it to default value of 2:

    const isCheckLimit = (v, minLength = 2) => {
      return v.length > minLength || t('checked-to-much');
    };

Now if you use this for your rules:

  :rules="[(value) => isCheckLimit(value, 4)]"

Your validation function will use 4 for minLength instead of default 2.

Original way will also work:

  :rules="[isCheckLimit]"

But will use the default value of 2 for minLength

  • Related