Skip to content

Commit

Permalink
Merge pull request #8 from nodus-it/support-disabled
Browse files Browse the repository at this point in the history
Support disabled
  • Loading branch information
dtvmedia authored Mar 18, 2024
2 parents 84176c5 + 4234c71 commit ed95be6
Show file tree
Hide file tree
Showing 33 changed files with 169 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
coverage
vendor
.phpunit.result.cache
composer.lock
composer.lock
tests/Data/ManualInputTestForm.php
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
},
"autoload-dev": {
"psr-4": {
"Nodus\\Packages\\LivewireForms\\Tests\\": "tests/"
"Nodus\\Packages\\LivewireForms\\Tests\\": "tests/",
"Nodus\\Packages\\LivewireCore\\Tests\\": "vendor/nodus-it/livewire-core/tests/"
}
},
"scripts": {
"test": "vendor/bin/pest",
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/pest --coverage --coverage-html coverage"
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/pest --coverage --coverage-html coverage",
"server": "php vendor/nodus-it/livewire-core/tests/environment/artisan serve"
},
"extra": {
"laravel": {
Expand Down
49 changes: 45 additions & 4 deletions src/Livewire/FormView.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Nodus\Packages\LivewireForms\Services\FormBuilder\FormInput;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsArrayValidations;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsValidations;
use Throwable;

Expand Down Expand Up @@ -393,6 +394,41 @@ protected function loadValuesByArray(array $array): void
$this->setValues($array);
}

/**
* Returns whether validation rules are defined for the given input
*
* @param string $key
*
* @return bool
*/
protected function hasRule(string $key): bool
{
return isset($this->rules[$key]) && !empty($this->rules[$key]);
}

/**
* Sets the validation rules for the given input
*
* @param FormInput $input
* @param string|array $rules
* @param bool $arrayRules
*
* @return $this
*/
protected function setRules(FormInput $input, string|array $rules, bool $arrayRules = false): static
{
$key = $arrayRules ? $input->getViewId() . '.*' : $input->getViewId();

/** @var SupportsDisabling|FormInput $input */
if ($input::supports('disabling') && $input->isDisabled()) {
$this->rules[$key] = [];
} else {
$this->rules[$key] = $rules;
}

return $this;
}

/**
* On input change handler
*
Expand All @@ -407,7 +443,7 @@ public function updated($propertyName): void
$this->validateOnly($propertyName, null, [], $this->getCustomValidationAttributes());

// In case we have added array validations, we check them here separately
if (isset($this->rules[$propertyName . '.*']) && !empty($this->rules[$propertyName . '.*'])) {
if ($this->hasRule($propertyName . '.*')) {
$this->validateOnly($propertyName . '.*', null, [], $this->getCustomValidationAttributes());
}
}
Expand Down Expand Up @@ -816,14 +852,14 @@ protected function prepareInputs(): void

if (in_array(SupportsValidations::class, $inputTraits)) {
/** @var SupportsValidations|FormInput $input */
$this->rules[$input->getViewId()] = $input->rewriteValidationRules($model);
$this->setRules($input, $input->rewriteValidationRules($model));
} else {
$this->rules[$input->getViewId()] = [];
$this->setRules($input, []);
}

if (in_array(SupportsArrayValidations::class, $inputTraits)) {
/** @var SupportsArrayValidations|FormInput $input */
$this->rules[$input->getViewId() . '.*'] = $input->getArrayValidations();
$this->setRules($input, $input->getArrayValidations(), true);
}

if (in_array(SupportsDefaultValue::class, $inputTraits)) {
Expand All @@ -849,6 +885,11 @@ public function getRealInputs(): array
continue;
}

/** @var SupportsDisabling|FormInput $input */
if ($input::supports('disabling') && $input->isDisabled()) {
continue;
}

$inputs[] = $input;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Services/FormBuilder/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nodus\Packages\LivewireForms\Services\FormBuilder;

use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsLabelPosition;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize;
Expand All @@ -20,6 +21,7 @@ class Checkbox extends FormInput
use SupportsValidations;
use SupportsHint;
use SupportsLabelPosition;
use SupportsDisabling;

/**
* Checkbox constructor
Expand Down
1 change: 1 addition & 0 deletions src/Services/FormBuilder/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nodus\Packages\LivewireForms\Services\FormBuilder;

use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsValidations;
Expand Down
2 changes: 2 additions & 0 deletions src/Services/FormBuilder/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nodus\Packages\LivewireForms\Services\FormBuilder;

use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsValidations;
Expand All @@ -18,4 +19,5 @@ class Color extends FormInput
use SupportsValidations;
use SupportsSize;
use SupportsHint;
use SupportsDisabling;
}
2 changes: 2 additions & 0 deletions src/Services/FormBuilder/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Carbon;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsMinMax;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsPlaceholder;
Expand All @@ -24,6 +25,7 @@ class Date extends FormInput
use SupportsHint;
use SupportsPlaceholder;
use SupportsMinMax;
use SupportsDisabling;

/**
* Date constructor.
Expand Down
2 changes: 2 additions & 0 deletions src/Services/FormBuilder/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Carbon;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsValidations;
Expand All @@ -19,6 +20,7 @@ class DateTime extends FormInput
use SupportsValidations;
use SupportsSize;
use SupportsHint;
use SupportsDisabling;

/**
* Pre validation mutator handler
Expand Down
2 changes: 2 additions & 0 deletions src/Services/FormBuilder/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Nodus\Packages\LivewireForms\Services\FormBuilder\Support\Currency;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Support\InputMode;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsInputMode;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsPlaceholder;
Expand All @@ -25,6 +26,7 @@ class Decimal extends FormInput
use SupportsHint;
use SupportsPlaceholder;
use SupportsInputMode;
use SupportsDisabling;

/**
* Number of decimals to be shown of the decimal value
Expand Down
2 changes: 2 additions & 0 deletions src/Services/FormBuilder/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Livewire\TemporaryUploadedFile;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsArrayValidations;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsMultiple;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize;
Expand All @@ -21,6 +22,7 @@ class File extends FormInput
use SupportsArrayValidations;
use SupportsSize;
use SupportsHint;
use SupportsDisabling;

/**
* Accepted file formats
Expand Down
10 changes: 9 additions & 1 deletion src/Services/FormBuilder/FormInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,15 @@ public function getErrorKeys(): array
*/
public static function supports(string $feature): bool
{
$traits = collect(class_uses(static::class))
$checkClasses = array_values(class_parents(static::class));
$checkClasses[] = static::class;

$traits = [];
foreach ($checkClasses as $class) {
$traits = array_merge($traits, class_uses($class));
}

$traits = collect($traits)
->map(fn ($value) => Str::of($value)
->classBasename()
->lower()
Expand Down
2 changes: 2 additions & 0 deletions src/Services/FormBuilder/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nodus\Packages\LivewireForms\Services\FormBuilder;

use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsMinMax;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsPlaceholder;
Expand All @@ -22,4 +23,5 @@ class Number extends FormInput
use SupportsHint;
use SupportsPlaceholder;
use SupportsMinMax;
use SupportsDisabling;
}
2 changes: 2 additions & 0 deletions src/Services/FormBuilder/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nodus\Packages\LivewireForms\Services\FormBuilder;

use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsPlaceholder;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize;
Expand All @@ -20,6 +21,7 @@ class Password extends FormInput
use SupportsSize;
use SupportsHint;
use SupportsPlaceholder;
use SupportsDisabling;

/**
* Secure mode flag
Expand Down
2 changes: 2 additions & 0 deletions src/Services/FormBuilder/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nodus\Packages\LivewireForms\Services\FormBuilder;

use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsOptions;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize;
Expand All @@ -15,4 +16,5 @@ class Radio extends FormInput
use SupportsSize;
use SupportsHint;
use SupportsOptions;
use SupportsDisabling;
}
1 change: 1 addition & 0 deletions src/Services/FormBuilder/RichTextarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nodus\Packages\LivewireForms\Services\FormBuilder;

use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsValidations;
Expand Down
2 changes: 2 additions & 0 deletions src/Services/FormBuilder/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsMultiple;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsOptions;
Expand Down Expand Up @@ -38,6 +39,7 @@ class Select extends FormInput
}
use SupportsHint;
use SupportsTranslations;
use SupportsDisabling;
use SupportsOptions {
getOptions as parentGetOptions;
setOptions as parentSetOptions;
Expand Down
2 changes: 2 additions & 0 deletions src/Services/FormBuilder/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nodus\Packages\LivewireForms\Services\FormBuilder;

use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsInputMode;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsPlaceholder;
Expand All @@ -22,4 +23,5 @@ class Text extends FormInput
use SupportsHint;
use SupportsPlaceholder;
use SupportsInputMode;
use SupportsDisabling;
}
2 changes: 2 additions & 0 deletions src/Services/FormBuilder/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nodus\Packages\LivewireForms\Services\FormBuilder;

use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsPlaceholder;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize;
Expand All @@ -20,6 +21,7 @@ class Textarea extends FormInput
use SupportsSize;
use SupportsHint;
use SupportsPlaceholder;
use SupportsDisabling;

/**
* Rows count
Expand Down
2 changes: 2 additions & 0 deletions src/Services/FormBuilder/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Carbon;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsMinMax;
use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize;
Expand All @@ -21,6 +22,7 @@ class Time extends FormInput
use SupportsSize;
use SupportsHint;
use SupportsMinMax;
use SupportsDisabling;

/**
* Post validation mutator handler
Expand Down
42 changes: 42 additions & 0 deletions src/Services/FormBuilder/Traits/SupportsDisabling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Nodus\Packages\LivewireForms\Services\FormBuilder\Traits;

/**
* Supports disabling form input trait
*
* @package Nodus\Packages\LivewireForms\Services\FormBuilder\Traits
*/
trait SupportsDisabling
{
/**
* Disabling flag
*
* @var bool
*/
protected bool $disabled = false;

/**
* Returns if the input is disabled
*
* @return bool
*/
public function isDisabled(): bool
{
return $this->disabled;
}

/**
* Sets the inputs disabled state
*
* @param bool $disabled
*
* @return $this
*/
public function setDisabled(bool $disabled = true): static
{
$this->disabled = $disabled;

return $this;
}
}
Loading

0 comments on commit ed95be6

Please sign in to comment.