diff --git a/FormBuilder.php b/FormBuilder.php index 0e5db8d..d406376 100755 --- a/FormBuilder.php +++ b/FormBuilder.php @@ -69,6 +69,13 @@ class FormBuilder { */ protected $spoofedMethods = array('DELETE', 'PATCH', 'PUT'); + /** + * The types of inputs to not fill values on by default. + * + * @var array + */ + protected $skipValueTypes = array('file', 'password', 'checkbox', 'radio'); + /** * Create a new form builder instance. * @@ -217,7 +224,7 @@ class FormBuilder { // in the model instance if one is set. Otherwise we will just use empty. $id = $this->getIdAttribute($name, $options); - if ($type != 'file' and $type != 'password') + if ( ! in_array($type, $this->skipValueTypes)) { $value = $this->getValueAttribute($name, $value); } @@ -402,6 +409,58 @@ class FormBuilder { return "{$list}"; } + /** + * Create a select range field. + * + * @param string $name + * @param string $begin + * @param string $end + * @param string $selected + * @param array $options + * @return string + */ + public function selectRange($name, $begin, $end, $selected = null, $options = array()) + { + $range = array_combine($range = range($begin, $end), $range); + + return $this->select($name, $range, $selected, $options); + } + + /** + * Create a select year field. + * + * @param string $name + * @param string $begin + * @param string $end + * @param string $selected + * @param array $options + * @return string + */ + public function selectYear() + { + return call_user_func_array(array($this, 'selectRange'), func_get_args()); + } + + /** + * Create a select month field. + * + * @param string $name + * @param string $selected + * @param array $options + * @return string + */ + public function selectMonth($name, $selected = null, $options = array()) + { + $months = array(); + + foreach (range(1, 12) as $month) + { + $months[$month] = strftime('%B', mktime(0, 0, 0, $month)); + } + + return $this->select($name, $months, $selected, $options); + } + /** * Get the select option for the given value. * @@ -516,7 +575,7 @@ class FormBuilder { */ protected function checkable($type, $name, $value, $checked, $options) { - if (is_null($checked)) $checked = $this->getCheckedState($type, $name, $value); + $checked = $this->getCheckedState($type, $name, $value, $checked); if ($checked) $options['checked'] = 'checked'; @@ -529,11 +588,52 @@ class FormBuilder { * @param string $type * @param string $name * @param mixed $value + * @param bool $checked * @return void */ - protected function getCheckedState($type, $name, $value) + protected function getCheckedState($type, $name, $value, $checked) { - if ($type == 'checkbox') return (bool) $this->getValueAttribute($name); + switch ($type) + { + case 'checkbox': + return $this->getCheckboxCheckedState($name, $checked); + + case 'radio': + return $this->getRadioCheckedState($name, $value, $checked); + + default: + return $this->getValueAttribute($name) == $value; + } + } + + /** + * Get the check state for a checkbox input. + * + * @param string $name + * @param mixed $value + * @param bool $checked + * @return bool + */ + protected function getCheckboxCheckedState($name, $checked) + { + if ( ! $this->oldInputIsEmpty() and is_null($this->old($name))) return false; + + if (is_null($this->old($name))) return $checked; + + return (bool) $this->getValueAttribute($name); + } + + /** + * Get the check state for a radio input. + * + * @param string $name + * @param mixed $value + * @param bool $checked + * @return bool + */ + protected function getRadioCheckedState($name, $value, $checked) + { + if (is_null($this->old($name))) return $checked; return $this->getValueAttribute($name) == $value; } @@ -790,6 +890,30 @@ class FormBuilder { } } + /** + * Get a value from the session's old input. + * + * @param string $name + * @return string + */ + public function old($name) + { + if (isset($this->session)) + { + return $this->session->getOldInput($name); + } + } + + /** + * Determine if the old input is empty. + * + * @return bool + */ + public function oldInputIsEmpty() + { + return (isset($this->session) and count($this->session->getOldInput()) == 0); + } + /** * Get the session store implementation. *