Skip to content

Commit 9d535fd

Browse files
authored
Merge pull request #99 from devforth/invalidityandemptinesscheck
Invalidityandemptinesscheck
2 parents 3a32d10 + 6c95742 commit 9d535fd

File tree

2 files changed

+77
-7
lines changed

2 files changed

+77
-7
lines changed

adminforth/documentation/docs/tutorial/03-Customization/02-customFieldRendering.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,67 @@ Now you can use this component in the configuration of the resource:
230230
}
231231
```
232232
233+
### Custom inValidity inside of the custom create/edit components
234+
235+
Custom componets can emit `update:inValidity` event to parent to say that the field is invalid.
236+
237+
You can define this emit as:
238+
239+
```ts
240+
const emit = defineEmits([
241+
"update:value",
242+
//diff-add
243+
"update:inValidity"
244+
]);
245+
```
246+
247+
Every time when state in your component becomes invalid, you can emit this event with error message which will be shown in the UI to the user.
248+
249+
```ts
250+
emit('update:inValidity', "The field has wrong value");
251+
```
252+
253+
Every time when state in your component becomes valid, you can emit this event with `false`
254+
255+
```ts
256+
emit('update:inValidity', false);
257+
```
258+
259+
If component never emits `update:inValidity` event (includign case when you don't use it at all), the field is considered valid.
260+
261+
### Custom emptiness inside of the custom create/edit components
262+
263+
Custom componets can emit `update:emptiness` event to parent to say that the field is empty.
264+
265+
Emptiness is used to prevent user from saving form when `column.required` is true and field is empty.
266+
267+
When `column.required` is false emptiness is not checked.
268+
269+
You can define this emit as:
270+
271+
```ts
272+
const emit = defineEmits([
273+
"update:value",
274+
//diff-add
275+
"update:emptiness"
276+
]);
277+
```
278+
279+
Every time when state in your component becomes empty, you can emit this event with `true`
280+
281+
```ts
282+
emit('update:emptiness', true);
283+
```
284+
285+
Every time when state in your component becomes not empty, you can emit this event with `false`
286+
287+
```ts
288+
emit('update:emptiness', false);
289+
```
290+
291+
Emptiness emit has a higher priority than natural emptiness of the field. For example when actual value under column in record is empty but component emitted `false` for `update:emptiness` (in other words child component said it non-empty), the field is considered as Non-empty.
292+
For another example, if companent is naturally updated some value in record but emited `true` (said that it is empty) the field is considered as empty and error in form will be shown to user.
293+
233294
234295
## Pre-made renderers
235296

adminforth/spa/src/components/ResourceForm.vue

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,24 @@ const columnError = (column) => {
9999
return customComponentsInValidity.value?.value?.[column.name];
100100
}
101101
102-
if (
103-
column.required[mode.value] &&
104-
(currentValues.value[column.name] === undefined || currentValues.value[column.name] === null || currentValues.value[column.name] === '' || (column.isArray?.enabled && !currentValues.value[column.name].length)) &&
102+
if ( column.required[mode.value] ) {
103+
const naturalEmptiness = currentValues.value[column.name] === undefined ||
104+
currentValues.value[column.name] === null ||
105+
currentValues.value[column.name] === '' ||
106+
(column.isArray?.enabled && !currentValues.value[column.name].length);
107+
108+
const emitedEmptiness = customComponentsEmptiness.value?.value?.[column.name];
105109
// if component is custum it might tell other criteria for emptiness by emitting 'update:emptiness'
106110
// components which do not emit 'update:emptiness' will have undefined value in customComponentsEmptiness
107-
(customComponentsEmptiness.value?.value?.[column.name] !== false)
108-
109-
) {
110-
return t('This field is required');
111+
let actualEmptiness;
112+
if (emitedEmptiness !== undefined) {
113+
actualEmptiness = emitedEmptiness;
114+
} else {
115+
actualEmptiness = naturalEmptiness;
116+
}
117+
if (actualEmptiness) {
118+
return t('This field is required');
119+
}
111120
}
112121
if (column.type === 'json' && !column.isArray?.enabled && currentValues.value[column.name]) {
113122
try {

0 commit comments

Comments
 (0)