Skip to content

Commit

Permalink
Finished disabling support for most inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
dtvmedia committed Mar 18, 2024
1 parent beeffd7 commit 7ba7b42
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 10 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
51 changes: 44 additions & 7 deletions src/Livewire/FormView.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,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 @@ -408,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 @@ -815,19 +850,16 @@ protected function prepareInputs(): void

$inputTraits = class_uses($input);

// TODO
// - extra addRuleForInput($input) method?
// - check if input supports disabling, is disabled and only add rules if not
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 @@ -853,6 +885,11 @@ public function getRealInputs(): array
continue;
}

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

$inputs[] = $input;
}

Expand Down
1 change: 0 additions & 1 deletion src/Services/FormBuilder/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Code extends FormInput
use SupportsValidations;
use SupportsSize;
use SupportsHint;
use SupportsDisabling;

/**
* Syntax Highlight mode
Expand Down
1 change: 0 additions & 1 deletion src/Services/FormBuilder/RichTextarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ class RichTextarea extends FormInput
use SupportsValidations;
use SupportsSize;
use SupportsHint;
use SupportsDisabling;
}
3 changes: 3 additions & 0 deletions tests/Data/InputTestForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function inputs(): void
->setValidations('required');
$this->addText('min_input')
->setValidations('required|min:5');
$this->addText('disabled_input')
->setValidations('required|min:5')
->setDisabled();
}

public function submit(array $values): void
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/FormViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function testMacroableSupport()
FormView::macro(
'addNewCustomInput',
function (string $name, string $label = null) {
/** @var FormView $this */
return $this->addInput(Text::class, $name, $label);
}
);
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/InputTests/TraitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ public function testSupportLabelPosition()
$this->assertInstanceOf(Checkbox::class, $input->setLabelTop());
$this->assertSame(LabelPosition::Top, $input->getLabelPosition());
}

public function testSupportDisabling()
{
$input = new Text('text_input');
$this->assertSame(false, $input->isDisabled());
$this->assertInstanceOf(Text::class, $input->setDisabled());
$this->assertSame(true, $input->isDisabled());
}
}

class TestModel extends Model
Expand Down

0 comments on commit 7ba7b42

Please sign in to comment.