Skip to content

Commit

Permalink
Allow nullable field (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdpoulter authored Oct 24, 2021
1 parent 548faf7 commit 1cbc0d0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ function (NovaRequest $request, $model, $attribute, $requestAttribute) {
public function attach($class)
{
return $this->options($class::asSelectArray())
->rules('required', new EnumValue($class, false));
->rules($this->nullable ? 'nullable' : 'required', new EnumValue($class, false));
}

public function nullable($nullable = true, $values = null)
{
$this->rules = str_replace('required', 'nullable', $this->rules);

return parent::nullable($nullable, $values);
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/FlaggedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@

class FlaggedEnum extends BooleanGroup
{
public function __construct($name, $attribute = null, $resolveCallback = null)
{
parent::__construct($name, $attribute, $resolveCallback);

$this->noValueText('None');
}
public $noValueText = 'None';

public function attach($class)
{
Expand Down
11 changes: 11 additions & 0 deletions tests/Fields/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SimpleSquid\Nova\Fields\Enum\Tests\Fields;

use BenSampo\Enum\Rules\EnumValue;
use Laravel\Nova\Http\Requests\NovaRequest;
use SimpleSquid\Nova\Fields\Enum\Enum;
use SimpleSquid\Nova\Fields\Enum\Tests\Examples\IntegerEnum;
use SimpleSquid\Nova\Fields\Enum\Tests\TestCase;
Expand Down Expand Up @@ -56,4 +57,14 @@ public function it_displays_enum_options()
], $this->field->meta['options']);
}
}

/** @test */
public function it_can_be_nullable()
{
$this->field->nullable();

$this->assertNotContains('required', $this->field->rules);
$this->assertContains('nullable', $this->field->rules);
$this->assertFalse($this->field->isRequired(new NovaRequest()));
}
}

0 comments on commit 1cbc0d0

Please sign in to comment.