Skip to content

Commit

Permalink
feature -- validation rules
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix committed Jan 15, 2021
1 parent 286b5f0 commit f4150d4
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 15 deletions.
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Belongs To Many Field for simple manage Nested relation tree. Enables attaching

### RoadMap

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

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

### Contributing
Feel free to suggest changes, ask for new features or fix bugs yourself.
Expand Down
11 changes: 0 additions & 11 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
:normalizer="normalizer"
/>
</div>
<help-text class="error-text mt-2 text-danger" v-if="hasErrors">
{{ firstError }}
</help-text>
</template>
</default-field>
</template>
Expand Down Expand Up @@ -100,14 +97,6 @@ export default {
{
formData.append( this.field.attribute, JSON.stringify( this.selectedValues ) )
},
},
computed:{
hasErrors: function() {
return this.errors.errors.hasOwnProperty(this.field.attribute);
},
firstError: function() {
return this.errors.errors[this.field.attribute][0]
}
}
}
</script>
13 changes: 11 additions & 2 deletions src/NestedTreeAttachManyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace PhoenixLib\NovaNestedTreeAttachMany;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Laravel\Nova\Authorizable;
use Laravel\Nova\Fields\Field;
use Laravel\Nova\Fields\ResourceRelationshipGuesser;
use PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation\RelationHandlerFactory;
use PhoenixLib\NovaNestedTreeAttachMany\Rules\ArrayRules;

class NestedTreeAttachManyField extends Field
{
Expand All @@ -21,8 +23,6 @@ class NestedTreeAttachManyField extends Field

public $showOnIndex = false;

private $fireEvents = 0;

public function __construct($name, $attribute = null, $resource = null)
{
parent::__construct($name, $attribute);
Expand Down Expand Up @@ -178,4 +178,13 @@ public function authorize(Request $request)
&& $request->newResource()->authorizedToAttachAny($request, $this->resourceClass::newModel())
&& parent::authorize($request);
}

public function rules($rules)
{
$rules = ($rules instanceof Rule || is_string($rules)) ? func_get_args() : (array)$rules;

$this->rules = [ new ArrayRules($rules) ];

return $this;
}
}
60 changes: 60 additions & 0 deletions src/Rules/ArrayRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace PhoenixLib\NovaNestedTreeAttachMany\Rules;

use Illuminate\Contracts\Validation\Rule;

class ArrayRules implements Rule
{
public $rules = [];

private $message;

/**
* Create a new rule instance.
*
* @return void
*/
public function __construct(array $rules)
{
$this->rules = $rules;
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$input = [$attribute => json_decode($value, true)];

$this->rules = [$attribute => $this->rules];

$validator = \Validator::make($input, $this->rules, $this->messages($attribute));

$this->message = $validator->errors()->get($attribute);

return $validator->passes();
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return $this->message;
}

public function messages($attribute)
{
return [
"size" => __('Select exactly') . ' :size',
"min" => __('Select minimum of') . ' :min',
"max" => __('Select maximum of') . ' :max',
];
}
}

0 comments on commit f4150d4

Please sign in to comment.