Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(validation): Add 'optional' prop #15191

Conversation

carlesalbasboix
Copy link

@carlesalbasboix carlesalbasboix commented Jun 2, 2022

Description

Added a boolean prop called 'optional' to the validation composable. If the model value is empty (undefined, empty string or empty array) and optional is set, then the validation ignores the rules and returns true.

Useful for:

  • Validation rules don't need to take into account an empty input.
  • An optional field will be automatically valid on render if the initial value is empty.

Motivation and Context

Right now when writing validation rules you must take into account whether you accept an empty string. Furthermore, an empty optional field will not be valid by default until the user changes it's value. This solves the problem of having optional fields that still require rules for when they are set.

How Has This Been Tested?

There are no tests yet. I want to wait and see whether the functionality is approved before writing tests.

Markup:

<template>
  <v-app>
    <v-container>
      Hola
      <v-form v-model="formValid">
        <v-text-field v-model="inputOne" optional :rules="[v => 'Rule fails']" />
      </v-form>
      {{ formValid ? "valid" : "invalid" }}
    </v-container>
  </v-app>
</template>

<script setup>
  import { ref } from 'vue'

  const formValid = ref(false)

  const inputOne = ref('')
</script>

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Improvement/refactoring (non-breaking change that doesn't add any features but makes things better)

Checklist:

  • The PR title is no longer than 64 characters.
  • The PR is submitted to the correct branch (master for bug fixes and documentation updates, dev for new features and backwards compatible changes and next for non-backwards compatible changes).
  • My code follows the code style of this project.
  • I've added relevant changes to the documentation (applies to new features and breaking changes in core library)

@johnleider
Copy link
Member

Could you provide markup for your use-case?

@carlesalbasboix
Copy link
Author

I have a form with an optional email. I want the validation to evaluate to true either if the email is valid or the input is left blank. Hence this "optional" prop that automatically evaluates the validation to true if the input is blank.

@MajesticPotatoe
Copy link
Member

@carlesalbasboix by markup he means actual code showing how this is suppose to be used. In the PR template theres a section called Markup: with a spot to stick this code. This helps reviewers to see how your feature is intended to be used and to help validate that it works.

@carlesalbasboix
Copy link
Author

Ah, ok, I resolved the branch conflicts and provided the markup.

@johnleider
Copy link
Member

@KaelWD how does this fit in with our conversation regarding lazy-validation last week?

@Fadiabb
Copy link

Fadiabb commented Sep 19, 2022

Hi,
is there a plan when this will be merged and included in a coming release ?

@johnleider
Copy link
Member

Maybe 3.1. We are pretty much locked for v3 release at this point.

@carlesalbasboix
Copy link
Author

@johnleider Can we consider it now that v3 has been released?

@johnleider johnleider added this to the v3.x.x milestone Dec 2, 2022
@KaelWD KaelWD changed the base branch from next to dev December 7, 2022 15:21
@johnleider
Copy link
Member

Pulse check on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E: validation S: has merge conflicts The pending Pull Request has merge conflicts T: feature A new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants