Skip to content

Commit 12434e3

Browse files
author
Lionel Bijaoui
committed
Custom errors
- Possibility to use scoped-slot to customise fully how errors are build - Expose `errors` object, `field` object and `getValueFromOption` function
1 parent a7a1bd3 commit 12434e3

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

dev/projects/full/app.vue

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,34 @@
2323
</div>
2424
</div>
2525
<vue-form-generator :schema="schema" :model="model" :options="formOptions" :multiple="selected.length > 1" ref="form" :is-new-model="isNewModel" @model-updated="modelUpdated" @validated="onValidated">
26+
2627
<template slot="label" slot-scope="{ field }">
2728
<h1>Custom label : {{ field.label }}</h1>
2829
</template>
30+
2931
<template slot="help" slot-scope="{ field }">
3032
<span v-if='field.help' class="help">
3133
<span @click.prevent="testClick(field.help, $event)">Custom help</span>
3234
<i class="icon"></i>
3335
<!-- <div class="helpText-" v-html='field.help'></div> -->
3436
</span>
3537
</template>
38+
3639
<template slot="hint" slot-scope="{ field, getValueFromOption }">
3740
<span>Custom hint</span>
3841
<div class="hint" v-html="getValueFromOption(field, 'hint', undefined)"></div>
3942
</template>
43+
44+
<template slot="errors" slot-scope="{ errors, field, getValueFromOption }">
45+
<table class="errors help-block">
46+
<tbody>
47+
<tr>
48+
<td v-for="(error, index) in errors" :key="index" v-html="error"></td>
49+
</tr>
50+
</tbody>
51+
</table>
52+
</template>
53+
4054
</vue-form-generator>
4155
</div>
4256
<div class="col-md-6">

src/formElement.vue

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
<slot name="hint" :field="field" :getValueFromOption="getValueFromOption"></slot>
1717
</template>
1818

19-
<div v-if="childErrors.length > 0" class="errors help-block">
20-
<span v-for="(error, index) in childErrors" :key="index" v-html="error"></span>
21-
</div>
19+
<template v-if="fieldHasErrors">
20+
<slot name="errors" :childErrors="childErrors" :field="field" :getValueFromOption="getValueFromOption" ></slot>
21+
</template>
2222
</div>
2323
</template>
2424
<script>
@@ -87,11 +87,13 @@ export default {
8787
fieldHasHint() {
8888
return !isNil(this.field.hint);
8989
},
90+
fieldHasErrors() {
91+
return this.childErrors.length > 0;
92+
},
9093
fieldRowClasses() {
91-
const hasErrors = this.childErrors.length > 0;
9294
let baseClasses = {
93-
[objGet(this.options, "validationErrorClass", "error")]: hasErrors,
94-
[objGet(this.options, "validationSuccessClass", "valid")]: !hasErrors,
95+
[objGet(this.options, "validationErrorClass", "error")]: this.fieldHasErrors,
96+
[objGet(this.options, "validationSuccessClass", "valid")]: !this.fieldHasErrors,
9597
disabled: this.getValueFromOption(this.field, "disabled"),
9698
readonly: this.getValueFromOption(this.field, "readonly"),
9799
featured: this.getValueFromOption(this.field, "featured"),

src/formGenerator.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
<form-group :tag="tag" :fields="fields" :model="model" :options="options" :errors="errors" :eventBus="eventBus">
66
<template slot="element" slot-scope="slotProps">
77
<form-element :field="slotProps.field" :model="slotProps.model" :options="slotProps.options" :errors="slotProps.errors" :eventBus="eventBus">
8+
89
<template slot="label" slot-scope="{ field, getValueFromOption }">
910
<slot name="label" :field="field" :getValueFromOption="getValueFromOption">
1011
<span v-html="field.label"></span>
1112
</slot>
1213
</template>
14+
1315
<template slot="help" slot-scope="{ field, getValueFromOption }">
1416
<slot name="help" :field="field" :getValueFromOption="getValueFromOption">
1517
<span v-if='field.help' class="help">
@@ -18,11 +20,21 @@
1820
</span>
1921
</slot>
2022
</template>
23+
2124
<template slot="hint" slot-scope="{ field, getValueFromOption }">
2225
<slot name="hint" :field="field" :getValueFromOption="getValueFromOption">
2326
<div class="hint" v-html="getValueFromOption(field, 'hint', undefined)"></div>
2427
</slot>
2528
</template>
29+
30+
<template slot="errors" slot-scope="{ childErrors, field, getValueFromOption }">
31+
<slot name="errors" :errors="childErrors" :field="field" :getValueFromOption="getValueFromOption">
32+
<div class="errors help-block">
33+
<span v-for="(error, index) in childErrors" :key="index" v-html="error"></span>
34+
</div>
35+
</slot>
36+
</template>
37+
2638
</form-element>
2739
</template>
2840
</form-group>

0 commit comments

Comments
 (0)