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 #78 from JesseLeite/fix-checkbox-array-binding
Browse files Browse the repository at this point in the history
Fix checkbox array binding
  • Loading branch information
adamwathan committed Feb 19, 2016
2 parents 4a26473 + c5a55bb commit a8fc8b9
Show file tree
Hide file tree
Showing 22 changed files with 252 additions and 164 deletions.
5 changes: 2 additions & 3 deletions src/AdamWathan/Form/Elements/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

class Button extends FormControl
{

protected $attributes = array(
protected $attributes = [
'type' => 'button',
);
];

protected $value;

Expand Down
13 changes: 8 additions & 5 deletions src/AdamWathan/Form/Elements/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

class Checkbox extends Input
{
protected $attributes = array(
protected $attributes = [
'type' => 'checkbox',
);
];

private $checked;

Expand Down Expand Up @@ -76,11 +76,14 @@ protected function setChecked($checked = true)

protected function checkBinding()
{
$currentValue = $this->getAttribute('value');
$currentValue = (string) $this->getAttribute('value');

$oldValue = $this->oldValue;
$oldValue = is_array($oldValue) ? $oldValue : [$oldValue];
$oldValue = array_map('strval', $oldValue);

if ($currentValue === $oldValue) {
$this->check();
if (in_array($currentValue, $oldValue)) {
return $this->check();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/AdamWathan/Form/Elements/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

class Date extends Text
{
protected $attributes = array(
protected $attributes = [
'type' => 'date',
);
];

public function value($value)
{
Expand Down
8 changes: 4 additions & 4 deletions src/AdamWathan/Form/Elements/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

abstract class Element
{
protected $attributes = array();
protected $attributes = [];

protected function setAttribute($attribute, $value = null)
{
Expand Down Expand Up @@ -109,9 +109,9 @@ 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);
$params = count($params) ? $params : [$method];
$params = array_merge([$method], $params);
call_user_func_array([$this, 'attribute'], $params);
return $this;
}
}
4 changes: 2 additions & 2 deletions src/AdamWathan/Form/Elements/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Email extends Text
{
protected $attributes = array(
protected $attributes = [
'type' => 'email',
);
];
}
4 changes: 2 additions & 2 deletions src/AdamWathan/Form/Elements/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class File extends Input
{
protected $attributes = array(
protected $attributes = [
'type' => 'file',
);
];
}
4 changes: 2 additions & 2 deletions src/AdamWathan/Form/Elements/FormOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

class FormOpen extends Element
{
protected $attributes = array(
protected $attributes = [
'method' => 'POST',
'action' => '',
);
];

protected $token;
protected $hiddenMethod;
Expand Down
4 changes: 2 additions & 2 deletions src/AdamWathan/Form/Elements/Hidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Hidden extends Input
{
protected $attributes = array(
protected $attributes = [
'type' => 'hidden',
);
];
}
4 changes: 2 additions & 2 deletions src/AdamWathan/Form/Elements/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Password extends Text
{
protected $attributes = array(
protected $attributes = [
'type' => 'password',
);
];
}
4 changes: 2 additions & 2 deletions src/AdamWathan/Form/Elements/RadioButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

class RadioButton extends Checkbox
{
protected $attributes = array(
protected $attributes = [
'type' => 'radio',
);
];

public function __construct($name, $value = null)
{
Expand Down
3 changes: 1 addition & 2 deletions src/AdamWathan/Form/Elements/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

class Select extends FormControl
{

private $options;
private $selected;

public function __construct($name, $options = array())
public function __construct($name, $options = [])
{
$this->setName($name);
$this->setOptions($options);
Expand Down
5 changes: 2 additions & 3 deletions src/AdamWathan/Form/Elements/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

class Text extends Input
{

protected $attributes = array(
protected $attributes = [
'type' => 'text',
);
];

public function placeholder($placeholder)
{
Expand Down
5 changes: 2 additions & 3 deletions src/AdamWathan/Form/Elements/TextArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

class TextArea extends FormControl
{

protected $attributes = array(
protected $attributes = [
'name' => '',
'rows' => 10,
'cols' => 50,
);
];

protected $value;

Expand Down
2 changes: 1 addition & 1 deletion src/AdamWathan/Form/ErrorStore/IlluminateErrorStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ protected function getErrors()

protected function transformKey($key)
{
return str_replace(array('.', '[]', '[', ']'), array('_', '', '.', ''), $key);
return str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $key);
}
}
13 changes: 10 additions & 3 deletions src/AdamWathan/Form/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function submit($value = 'Submit')
return $submit;
}

public function select($name, $options = array())
public function select($name, $options = [])
{
$select = new Select($name, $options);

Expand Down Expand Up @@ -219,6 +219,8 @@ public function bind($model)

public function getValueFor($name)
{
$name = $this->transformKey($name);

if ($this->hasOldInput()) {
return $this->getOldInput($name);
}
Expand Down Expand Up @@ -272,7 +274,7 @@ protected function unbindModel()

public function selectMonth($name)
{
$options = array(
$options = [
"1" => "January",
"2" => "February",
"3" => "March",
Expand All @@ -285,8 +287,13 @@ public function selectMonth($name)
"10" => "October",
"11" => "November",
"12" => "December",
);
];

return $this->select($name, $options);
}

protected function transformKey($key)
{
return str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $key);
}
}
2 changes: 1 addition & 1 deletion src/AdamWathan/Form/FormServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ protected function registerFormBuilder()
*/
public function provides()
{
return array('adamwathan.form');
return ['adamwathan.form'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public function getOldInput($key)

protected function transformKey($key)
{
return str_replace(array('.', '[]', '[', ']'), array('_', '', '.', ''), $key);
return str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $key);
}
}
Loading

0 comments on commit a8fc8b9

Please sign in to comment.