Skip to content

Commit f4150d4

Browse files
author
phoenix
committed
feature -- validation rules
1 parent 286b5f0 commit f4150d4

File tree

5 files changed

+76
-15
lines changed

5 files changed

+76
-15
lines changed

dist/js/field.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Belongs To Many Field for simple manage Nested relation tree. Enables attaching
99

1010
### RoadMap
1111

12-
- [ ] Validation
12+
- [x] Validation
1313
- [x] Show selected categories on Detail
1414
- [ ] Ability to pass your own tree
1515
- [ ] Ability to `Delayed Loading` data when tree has many records ( example 10k+ ).
@@ -70,6 +70,9 @@ This field also respects policies: ie Role / Permission
7070
- RolePolicy: attachPermission($user, $role, $permission)
7171
- PermissionPolicy: viewAny($user)
7272

73+
### Validation
74+
You can set min, max, size, required or custom rule objects
75+
`->rules('min:5', 'max:10', 'size:10', 'required', new CustomRule)`
7376

7477
### Contributing
7578
Feel free to suggest changes, ask for new features or fix bugs yourself.

resources/js/components/FormField.vue

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
:normalizer="normalizer"
1818
/>
1919
</div>
20-
<help-text class="error-text mt-2 text-danger" v-if="hasErrors">
21-
{{ firstError }}
22-
</help-text>
2320
</template>
2421
</default-field>
2522
</template>
@@ -100,14 +97,6 @@ export default {
10097
{
10198
formData.append( this.field.attribute, JSON.stringify( this.selectedValues ) )
10299
},
103-
},
104-
computed:{
105-
hasErrors: function() {
106-
return this.errors.errors.hasOwnProperty(this.field.attribute);
107-
},
108-
firstError: function() {
109-
return this.errors.errors[this.field.attribute][0]
110-
}
111100
}
112101
}
113102
</script>

src/NestedTreeAttachManyField.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace PhoenixLib\NovaNestedTreeAttachMany;
44

5+
use Illuminate\Contracts\Validation\Rule;
56
use Illuminate\Http\Request;
67
use Illuminate\Support\Facades\App;
78
use Laravel\Nova\Authorizable;
89
use Laravel\Nova\Fields\Field;
910
use Laravel\Nova\Fields\ResourceRelationshipGuesser;
1011
use PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation\RelationHandlerFactory;
12+
use PhoenixLib\NovaNestedTreeAttachMany\Rules\ArrayRules;
1113

1214
class NestedTreeAttachManyField extends Field
1315
{
@@ -21,8 +23,6 @@ class NestedTreeAttachManyField extends Field
2123

2224
public $showOnIndex = false;
2325

24-
private $fireEvents = 0;
25-
2626
public function __construct($name, $attribute = null, $resource = null)
2727
{
2828
parent::__construct($name, $attribute);
@@ -178,4 +178,13 @@ public function authorize(Request $request)
178178
&& $request->newResource()->authorizedToAttachAny($request, $this->resourceClass::newModel())
179179
&& parent::authorize($request);
180180
}
181+
182+
public function rules($rules)
183+
{
184+
$rules = ($rules instanceof Rule || is_string($rules)) ? func_get_args() : (array)$rules;
185+
186+
$this->rules = [ new ArrayRules($rules) ];
187+
188+
return $this;
189+
}
181190
}

src/Rules/ArrayRules.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
namespace PhoenixLib\NovaNestedTreeAttachMany\Rules;
3+
4+
use Illuminate\Contracts\Validation\Rule;
5+
6+
class ArrayRules implements Rule
7+
{
8+
public $rules = [];
9+
10+
private $message;
11+
12+
/**
13+
* Create a new rule instance.
14+
*
15+
* @return void
16+
*/
17+
public function __construct(array $rules)
18+
{
19+
$this->rules = $rules;
20+
}
21+
22+
/**
23+
* Determine if the validation rule passes.
24+
*
25+
* @param string $attribute
26+
* @param mixed $value
27+
* @return bool
28+
*/
29+
public function passes($attribute, $value)
30+
{
31+
$input = [$attribute => json_decode($value, true)];
32+
33+
$this->rules = [$attribute => $this->rules];
34+
35+
$validator = \Validator::make($input, $this->rules, $this->messages($attribute));
36+
37+
$this->message = $validator->errors()->get($attribute);
38+
39+
return $validator->passes();
40+
}
41+
42+
/**
43+
* Get the validation error message.
44+
*
45+
* @return string
46+
*/
47+
public function message()
48+
{
49+
return $this->message;
50+
}
51+
52+
public function messages($attribute)
53+
{
54+
return [
55+
"size" => __('Select exactly') . ' :size',
56+
"min" => __('Select minimum of') . ' :min',
57+
"max" => __('Select maximum of') . ' :max',
58+
];
59+
}
60+
}

0 commit comments

Comments
 (0)