Skip to content

Commit

Permalink
Allow setting required via boolean attribute per #571
Browse files Browse the repository at this point in the history
  • Loading branch information
claar committed Jul 24, 2018
1 parent 9c906e0 commit 95818c5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Former/Traits/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,23 @@ public function isRequired()
return isset($this->attributes['required']);
}

/**
* Set required live validation attribute
*
* @param boolean $isRequired
* @return $this
*/
public function required($isRequired=true)
{
if ($isRequired) {
$this->attributes['required'] = true;
} else {
unset($this->attributes['required']);
}

return $this;
}

/**
* Check if a field is unwrappable (no label)
*
Expand Down
27 changes: 27 additions & 0 deletions tests/LiveValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,33 @@ public function testCanSetFieldAsRequired()
$this->assertHTML($this->matchControlGroup(), $input);
}

public function testCanSetFieldAsRequiredUsingMethod()
{
$input = $this->former->text('foo')->required()->__toString();

$this->assertHTML($this->matchField(array('required' => null)), $input);
$this->assertLabel($input, 'foo', true);
$this->assertHTML($this->matchControlGroup(), $input);
}

public function testMatchFieldWithoutRequired()
{
$input = $this->former->text('foo')->__toString();

$this->assertHTML($this->matchField([]), $input);
$this->assertLabel($input, 'foo', false);
$this->assertHTML($this->matchControlGroup(), $input);
}

public function testCanSetFieldAsNotRequired()
{
$input = $this->former->text('foo')->required()->required(false)->__toString();

$this->assertHTML($this->matchField([]), $input);
$this->assertLabel($input, 'foo', false);
$this->assertHTML($this->matchControlGroup(), $input);
}

public function testCanSetNestedBracketFieldAsRequired()
{
$this->former->withRules(array('foo[bar]' => 'required'));
Expand Down

0 comments on commit 95818c5

Please sign in to comment.