Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Commit

Permalink
Merge pull request #47 from JerseyMilker/select-multiple
Browse files Browse the repository at this point in the history
New method for setting multiple on selects.
  • Loading branch information
adamwathan committed May 30, 2015
2 parents 23671d5 + 8e5968d commit d4ac77d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/AdamWathan/Form/Elements/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ protected function renderAttributes()

public function __call($method, $params)
{
$params = count($params) ? $params : array($method);
$params = array_merge(array($method), $params);
call_user_func_array(array($this, 'attribute'), $params);
return $this;
Expand Down
12 changes: 12 additions & 0 deletions src/AdamWathan/Form/Elements/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,16 @@ public function defaultValue($value)
$this->select($value);
return $this;
}

public function multiple()
{
$name = $this->attributes['name'];
if (substr($name, -2) != '[]') {
$name .= '[]';
}

$this->setName($name);
$this->setAttribute('multiple', 'multiple');
return $this;
}
}
15 changes: 15 additions & 0 deletions tests/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,19 @@ public function testSelectCanBeCreatedWithIntegerKeyValueOptions()
$result = $select->render();
$this->assertEquals($expected, $result);
}

public function testSelectCanBeMultiple()
{
$select = new Select('people');
$expected = '<select name="people[]" multiple="multiple"></select>';
$result = $select->multiple()->render();

$this->assertEquals($expected, $result);

$select = new Select('people[]');
$expected = '<select name="people[]" multiple="multiple"></select>';
$result = $select->multiple()->render();

$this->assertEquals($expected, $result);
}
}
10 changes: 10 additions & 0 deletions tests/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,14 @@ public function testCanAddAttributesThroughMagicMethods()
$result = $text->render();
$this->assertEquals($expected, $result);
}

public function testCanAddAttributesThroughMagicMethodsWithOptionalParameter()
{
$text = new Text('cow');
$text = $text->moo();

$expected = '<input type="text" name="cow" moo="moo">';
$result = $text->render();
$this->assertEquals($expected, $result);
}
}

0 comments on commit d4ac77d

Please sign in to comment.