Skip to content

Commit

Permalink
Fix errors when using rules in validation array
Browse files Browse the repository at this point in the history
Normally with multiple rules the following syntax is used with multiple rules:

```php
->withRules([
    'foo' => ['required', new Rule()]
]);
```

But this is also valid syntax and will crash former when a Rule object is passed:

```php
->withRules([
    'foo' => new Rule()
]);
```
  • Loading branch information
stayallive committed Dec 10, 2019
1 parent a52f5a9 commit cc2f3c4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Former/Former.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ public function withRules()
foreach ($rules as $name => $fieldRules) {
$expFieldRules = $fieldRules;
if (!is_array($expFieldRules)) {
if (is_object($expFieldRules)) {
continue;
}

$expFieldRules = explode('|', $expFieldRules);
$expFieldRules = array_map('trim', $expFieldRules);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/LiveValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,4 +637,15 @@ public function testCanIgnoreValidationRuleClasses()
$this->assertHTML($matcher, $input);
$this->assertControlGroup($input);
}

public function testCanIgnoreValidationRuleClassesWithoutArray()
{
$this->former->withRules(array('foo' => new \stdClass()));

$input = $this->former->text('foo')->__toString();
$matcher = $this->matchField();

$this->assertHTML($matcher, $input);
$this->assertControlGroup($input);
}
}

0 comments on commit cc2f3c4

Please sign in to comment.