Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit bc9dcd8

Browse files
authored
add hasError method in InertiaForm (#14)
1 parent 33b8fe4 commit bc9dcd8

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/InertiaForm.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,16 @@ class InertiaForm {
222222
Object.keys(this.inertiaPage().errorBags[this.__options.bag]).length > 0;
223223
}
224224

225+
hasError(field) {
226+
return this.hasErrors() &&
227+
this.inertiaPage().errorBags[this.__options.bag][field] &&
228+
this.inertiaPage().errorBags[this.__options.bag][field].length > 0;
229+
}
230+
225231
error(field) {
226-
if (! this.hasErrors() ||
227-
! this.inertiaPage().errorBags[this.__options.bag][field] ||
228-
this.inertiaPage().errorBags[this.__options.bag][field].length == 0) {
229-
return;
232+
if (this.hasError(field)) {
233+
return this.inertiaPage().errorBags[this.__options.bag][field][0];
230234
}
231-
232-
return this.inertiaPage().errorBags[this.__options.bag][field][0];
233235
}
234236

235237
errors(field = null) {
@@ -240,7 +242,7 @@ class InertiaForm {
240242
: [];
241243
}
242244

243-
return this.error(field)
245+
return this.hasError(field)
244246
? this.inertiaPage().errorBags[this.__options.bag][field]
245247
: [];
246248
}

0 commit comments

Comments
 (0)