Skip to content

Commit 3d43fc7

Browse files
authored
Merge pull request #5 from kirschbaum-development/feature/update-error-messages
updated error messages
2 parents 754a730 + cfbb0fc commit 3d43fc7

4 files changed

Lines changed: 47 additions & 8 deletions

File tree

dist/js/field.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
800800
//
801801
//
802802
//
803+
//
803804

804805

805806

@@ -17265,7 +17266,7 @@ var render = function() {
1726517266
var _c = _vm._self._c || _h
1726617267
return _c(
1726717268
"default-field",
17268-
{ attrs: { field: _vm.field, errors: _vm.errors } },
17269+
{ attrs: { field: _vm.field, errors: _vm.errors, "show-errors": false } },
1726917270
[
1727017271
_c(
1727117272
"template",

resources/js/components/FormField.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<default-field
33
:field="field"
44
:errors="errors"
5+
:show-errors="false"
56
>
67
<template slot="field">
78
<draggable

src/NovaInlineRelationship.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,29 @@ protected function fillAttributeFromRequest(NovaRequest $request, $requestAttrib
7272
*/
7373
protected function getRelationshipRule($attribute, $properties): RelationshipRule
7474
{
75+
/** @var array $ruleArray */
7576
$ruleArray = [];
7677

78+
/** @var array $messageArray */
79+
$messageArray = [];
80+
81+
/** @var array $attribArray */
82+
$attribArray = [];
83+
7784
foreach ($properties as $attrib => $prop) {
7885
if (! empty($prop['rules'])) {
79-
$ruleArray[sprintf('%s.*.%s', $attribute, $attrib)] = $prop['rules'];
86+
$name = sprintf('%s.*.%s', $attribute, $attrib);
87+
$ruleArray[$name] = $prop['rules'];
88+
$attribArray[$name] = $prop['label'] ?? $attrib;
89+
90+
if (! empty($prop['messages']) && is_array($prop['messages'])) {
91+
foreach ($prop['messages'] as $rule => $message) {
92+
$messageArray[sprintf('%s.%s', $name, $rule)] = $message;
93+
}
94+
}
8095
}
8196
}
8297

83-
return new RelationshipRule($ruleArray);
98+
return new RelationshipRule($ruleArray, $messageArray, $attribArray);
8499
}
85100
}

src/Rules/RelationshipRule.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,59 @@
22

33
namespace KirschbaumDevelopment\NovaInlineRelationship\Rules;
44

5+
use Illuminate\Support\MessageBag;
56
use Illuminate\Contracts\Validation\Rule;
67
use Illuminate\Support\Facades\Validator;
78

89
class RelationshipRule implements Rule
910
{
1011
public $rules = [];
1112

13+
/**
14+
* @var array
15+
*/
16+
protected $messages;
17+
18+
/**
19+
* @var array
20+
*/
21+
protected $attributes;
22+
23+
/**
24+
* @var MessageBag
25+
*/
26+
protected $response;
27+
1228
/**
1329
* Create a new rule instance.
1430
*
15-
* @return void
31+
* @param array $rules
32+
* @param null|mixed $messages
33+
* @param null|mixed $attributes
34+
*
1635
*/
17-
public function __construct(array $rules)
36+
public function __construct(array $rules, $messages = null, $attributes = null)
1837
{
1938
$this->rules = $rules;
39+
$this->messages = $messages;
40+
$this->attributes = $attributes;
2041
}
2142

2243
/**
2344
* Determine if the validation rule passes.
2445
*
2546
* @param string $attribute
2647
* @param mixed $value
48+
*
2749
* @return bool
2850
*/
2951
public function passes($attribute, $value)
3052
{
3153
$input = [$attribute => json_decode($value, true)];
3254

33-
$validator = Validator::make($input, $this->rules);
55+
$validator = Validator::make($input, $this->rules, $this->messages, $this->attributes);
3456

35-
$this->message = $validator->errors();
57+
$this->response = $validator->errors();
3658

3759
return $validator->passes();
3860
}
@@ -44,6 +66,6 @@ public function passes($attribute, $value)
4466
*/
4567
public function message()
4668
{
47-
return $this->message;
69+
return $this->response;
4870
}
4971
}

0 commit comments

Comments
 (0)