Skip to content

Commit

Permalink
Can set class attribute in array. (#521)
Browse files Browse the repository at this point in the history
In case of you want to add class by condition.

```
Form::text('name', null, ['class' => [
    'form-control-,
    $error ? 'is-invalid' : '',
]]);
```
  • Loading branch information
ThunderBirdsX3 authored and tshafer committed May 30, 2018
1 parent 6d1cceb commit fda9d3d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/HtmlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ protected function attributeElement($key, $value)
return $value ? $key : '';
}

if (is_array($value) && $key === 'class') {
return 'class="' . implode(' ', $value) . '"';
}

if (! is_null($value)) {
return $key . '="' . e($value, false) . '"';
}
Expand Down
12 changes: 12 additions & 0 deletions tests/FormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,18 @@ public function testBooleanAttributes()
$this->assertEquals('<textarea readonly name="test" cols="50" rows="10"></textarea>', $input);
}

public function testArrayClassAttributes()
{
$input = $this->formBuilder->text('test', null, ['class' => ['class-a', 'class-b']]);
$this->assertEquals('<input class="class-a class-b" name="test" type="text">', $input);

$input = $this->formBuilder->text('test', null, ['class' => [
'class-a',
false ? 'class-b' : 'class-c'
]]);
$this->assertEquals('<input class="class-a class-c" name="test" type="text">', $input);
}

protected function setModel(array $data, $object = true)
{
if ($object) {
Expand Down
14 changes: 14 additions & 0 deletions tests/HtmlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,18 @@ public function testBooleanAttributes()

$this->assertEquals('', trim($result2));
}

public function testArrayClassAttributes()
{
$result = $this->htmlBuilder->attributes(['class' => ['class-a', 'class-b']]);

$this->assertEquals('class="class-a class-b"', trim($result));

$result = $this->htmlBuilder->attributes(['class' => [
'class-a',
false ? 'class-b' : 'class-c'
]]);

$this->assertEquals('class="class-a class-c"', trim($result));
}
}

0 comments on commit fda9d3d

Please sign in to comment.