Skip to content

Commit 9cf43d2

Browse files
committed
feat(rules): promote from labs
1 parent e7016b1 commit 9cf43d2

8 files changed

Lines changed: 41 additions & 18 deletions

File tree

packages/docs/src/pages/en/features/rules.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
emphasized: true
23
meta:
34
nav: Validation rules
45
title: Validation rules composable
@@ -8,7 +9,7 @@ related:
89
- /components/forms/
910
- /features/internationalization/
1011
features:
11-
github: /labs/rules/
12+
github: /composables/rules/
1213
label: 'E: rules'
1314
report: true
1415
---
@@ -27,8 +28,7 @@ To use the Rules plugin, you'll need to import and register it with your Vuetify
2728

2829
```js
2930
import { createVue } from 'vue'
30-
import { createVuetify } from 'vuetify'
31-
import { createRulesPlugin } from 'vuetify/labs/rules'
31+
import { createRulesPlugin, createVuetify } from 'vuetify'
3232

3333
const app = createVue()
3434
const vuetify = createVuetify()
@@ -41,7 +41,7 @@ This will make the rules system available throughout your application. The plugi
4141
Inside of components, you can now import and utilize the rules composable:
4242

4343
```js
44-
import { useRules } from 'vuetify/labs/rules'
44+
import { useRules } from 'vuetify'
4545

4646
const rules = useRules()
4747
```
@@ -65,7 +65,7 @@ Existing rules' error messages can also be customized on the fly, to fit specifi
6565
</template>
6666

6767
<script setup>
68-
import { useRules } from 'vuetify/labs/rules'
68+
import { useRules } from 'vuetify'
6969
7070
const rules = useRules()
7171
@@ -135,10 +135,9 @@ In this case, error message can be redefined as second parameter:
135135
</v-form>
136136
```
137137

138-
<!--
139138
## Aliases
140139

141-
Rules can also be used in inputs using the alias names syntax:
140+
Rather than importing `useRules` and calling builders by hand, any rule can be referenced directly in the `rules` prop by its **alias** — a string prefixed with `$` that resolves to the matching builder. This keeps templates terse and lets you skip a component-level `useRules()` call entirely:
142141

143142
```html { resource="src/App.vue" }
144143
<v-form>
@@ -149,29 +148,50 @@ Rules can also be used in inputs using the alias names syntax:
149148
</v-form>
150149
```
151150

152-
RuleBuilders parameters can also be passed using an Array:
151+
The example above is shorthand for `[rules.required()]`.
152+
153+
::: info
154+
155+
Aliases are resolved by the rules plugin, so they only take effect when [`createRulesPlugin`](#installation) is registered. Without it, the string is passed through as-is and treated as a literal error message.
156+
157+
:::
158+
159+
### Passing options
160+
161+
For rules that accept options, use an array where the first item is the alias and the remaining items are the builder's arguments. The last argument can still be a custom error message:
153162

154163
```html { resource="src/App.vue" }
155164
<v-form>
156165
<v-text-field
157166
label="Username"
158167
:rules="[
159168
['$required', 'This field is mandatory'],
160-
['$maxLength', 10, 'You can\'t write over 10 characters']
169+
['$maxLength', 10, 'You can\'t write over 10 characters'],
161170
]"
162171
></v-text-field>
163172
</v-form>
164173
```
165-
-->
174+
175+
Each array maps to its builder call — `['$maxLength', 10, '…']` resolves to `rules.maxLength(10, '…')`.
176+
177+
### Custom aliases
178+
179+
Because aliases reference the same builders registered through `createRulesPlugin`, your own [custom rules](#custom-rules) can be used by name too:
180+
181+
```html { resource="src/App.vue" }
182+
<v-text-field
183+
label="PIN"
184+
:rules="['$pinCode']"
185+
></v-text-field>
186+
```
166187

167188
## Custom rules
168189

169190
Vuetify comes with an existing set of validation rules but you can overwrite them or add yours.
170191

171192
```js { resource="src/plugins/vuetify.js" }
172193
import { createVue } from 'vue'
173-
import { createVuetify } from 'vuetify'
174-
import { createRulesPlugin } from 'vuetify/labs/rules'
194+
import { createRulesPlugin, createVuetify } from 'vuetify'
175195

176196
const app = createVue()
177197
const vuetify = createVuetify()

packages/docs/src/pages/en/introduction/roadmap.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ The following are the already released **minor** and **major** version updates.
7474
- VDataTable - programatic control over groups state with `v-model:opened`
7575
- VSparkline - hover interactivity, markers and tooltips
7676
- VOtpInputs - grouping fields into segments, a11y and emojis
77-
- Introduced [validation rules](/features/rules/) to the main framework from Labs
78-
- Introduced 5 new components to the main framework from Labs:
77+
- Introduced 7 new components to the main framework from Labs:
7978
- [v-icon-btn](/components/icon-buttons/)
8079
- [v-stepper-vertical](/components/vertical-steppers/)
8180
- [v-pull-to-refresh](/components/pull-to-refresh/)

packages/vuetify/src/composables/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export { useLocale, useRtl } from './locale'
1212
export { useTheme } from './theme'
1313
export { useHotkey } from './hotkey'
1414
export { useMask } from './mask'
15+
export { createRulesPlugin, useRules } from './rules'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './rules'
2+
export * from './plugin'
File renamed without changes.
File renamed without changes.

packages/vuetify/src/composables/validation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
import { makeFocusProps } from '@/composables/focus'
33
import { useForm } from '@/composables/form'
44
import { useProxiedModel } from '@/composables/proxiedModel'
5+
import { useRules } from '@/composables/rules'
56
import { useToggleScope } from '@/composables/toggleScope'
6-
import { useRules } from '@/labs/rules'
77

88
// Utilities
99
import { computed, nextTick, onBeforeMount, onBeforeUnmount, onMounted, ref, shallowRef, unref, useId, watch } from 'vue'
1010
import { getCurrentInstance, getCurrentInstanceName, propsFactory, wrapInArray } from '@/util'
1111

1212
// Types
1313
import type { PropType } from 'vue'
14-
import type { ValidationAlias } from '@/labs/rules'
14+
import type { ValidationAlias } from '@/composables/rules'
1515
import type { EventProp, MaybeRef } from '@/util'
1616

1717
export type ValidationResult = string | boolean
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export * from './rules'
2-
export * from './plugin'
1+
// Promoted to the core framework in v4.1.2
2+
// This re-export is kept for backwards compatibility and will be removed in v4.2.0
3+
export * from '@/composables/rules'

0 commit comments

Comments
 (0)