From 8e79407d9d5dfa39ba06712b2e62df260afd416e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 17 Feb 2013 22:40:51 -0600 Subject: [PATCH] refactoring the form builder. --- FormBuilder.php | 297 ++++++++++++++++++++++++++++++++++++++++++++++++ HtmlBuilder.php | 37 ++++++ 2 files changed, 334 insertions(+) create mode 100644 FormBuilder.php create mode 100644 HtmlBuilder.php diff --git a/FormBuilder.php b/FormBuilder.php new file mode 100644 index 0000000..f6df6d7 --- /dev/null +++ b/FormBuilder.php @@ -0,0 +1,297 @@ +url = $url; + $this->tokenProvider = $tokenProvider; + } + + /** + * Open up a new HTML form. + * + * @param array $options + * @return string + */ + public function open(array $options = array()) + { + $method = strtoupper(array_get($options, 'method', 'post')); + + // We need to extract the proper method from the attributes. If the method is + // something other than GET or POST we'll use POST since we will spoof the + // actual method since forms don't support PUT or DELETE as native HTML. + $attributes['method'] = $this->getMethod($method); + + $attributes['action'] = $this->getAction($options); + + $attributes['accept-charset'] = 'UTF-8'; + + // If the method is PUT or DELETE, we will need to add a spoofer hidden field + // that will instruct this Symfony request to pretend that the method is a + // different method than it actually is, for convenience from the forms. + $append = $this->getAppendage($method); + + $attributes = array_merge( + + $attributes, array_except($options, $this->reserved) + + ); + + // Finally we're ready to create the final form HTML field. We will attribute + // format the array of attributes. We will also add on the appendage which + // is used to spoof the requests for PUT and DELETE requests to the app. + $attributes = Html::attributes($attributes); + + return ''.$append; + } + + /** + * Generate a hidden field with the current CSRF token. + * + * @return string + */ + public function token() + { + return $this->hidden('csrf_token', $this->tokenProvider->getToken()); + } + + /** + * Create a form input field. + * + * @param string $type + * @param string $name + * @param string $value + * @param array $attributes + * @return string + */ + public function input($type, $name, $value = null, $attributes = array()) + { + $attributes['name'] = $name; + + $id = $this->getIdAttribute($name, $attributes); + + // We will get the appropriate value for the given field. We will look for the + // value in the session for the value in the old input data then we'll look + // in the model instance if one is set. Otherwise we will just use empty. + $value = $this->getValueAttribute($name, $value); + + $merge = compact('type', 'value', 'id'); + + $attributes = array_merge($attributes, $merge); + + return ''; + } + + /** + * Create a text input field. + * + * @param string $name + * @param string $value + * @param array $attributes + * @return string + */ + public function text($name, $value = null, $attributes = array()) + { + return $this->input('text', $name, $value, $attributes); + } + + /** + * Create a password input field. + * + * @param string $name + * @param array $attributes + * @return string + */ + public function password($name, $attributes = array()) + { + return $this->input('password', $name, '', $attributes); + } + + /** + * Create a hidden input field. + * + * @param string $name + * @param string $value + * @param array $attributes + * @return string + */ + public function hidden($name, $value, $attributes = array()) + { + return $this->input('hidden', $name, $value, $attributes); + } + + /** + * Create a file input field. + * + * @param string $name + * @param array $attributes + * @return string + */ + public function file($name, $attributes = array()) + { + return $this->input('file', $name, null, $attributes); + } + + /** + * Create a textarea input field. + * + * @param string $name + * @param string $value + * @param array $attributes + * @return string + */ + public function textarea($name, $value = null, $attributes = array()) + { + $attributes['name'] = $name; + + $attributes['id'] = $this->getIdAttribute($name, $attributes); + + if ( ! isset($attributes['rows'])) $attributes['rows'] = 10; + + if ( ! isset($attributes['cols'])) $attributes['cols'] = 50; + + $value = (string) $this->getValueAttribute($value); + + return ''.e($value).''; + } + + /** + * Parse the form action method. + * + * @param string $method + * @return string + */ + protected function getMethod($method) + { + return $method != 'GET' ? 'POST' : $method; + } + + /** + * Get the form action from the options. + * + * @param array $options + * @return string + */ + protected function getAction(array $options) + { + if (isset($options['url'])) + { + return $options['url']; + } + elseif (isset($options['route'])) + { + return $this->url->route($options['route']); + } + elseif (isset($options['action'])) + { + return $this->url->action($options['action']); + } + + return $this->url->current(); + } + + /** + * Get the form appendage for the given method. + * + * @param string $method + * @return string + */ + protected function getAppendage($method) + { + if ($method == 'PUT' or $method == 'DELETE') + { + $append = $this->hidden('_method', $method); + } + + return ''; + } + + /** + * Get the ID attribute for a field name. + * + * @param string $name + * @param array $attributes + * @return string + */ + protected function getIdAttribute($name, $attributes) + { + if (array_key_exists('id', $attributes)) + { + return $attributes['id']; + } + + if (in_array($name, $this->labels)) + { + return $name; + } + } + + /** + * Get the value that should be assigned to the field. + * + * @param string $name + * @param string $value + * @return string + */ + protected function getValueAttribute($name, $value) + { + if ( ! is_null($value)) return $value; + + if ($this->session->hasOldInput($name)) + { + return $this->session->getOldInput($name); + } + + if (isset($this->model) and isset($this->model[$name])) + { + return $this->model[$name]; + } + } + + /** + * Create a new model based form builder. + * + * @param mixed $model + * @param array $attributes + * @param Closure $callback + * @return void + */ + public function model($model, array $attributes, Closure $callback) + { + // + } + +} \ No newline at end of file diff --git a/HtmlBuilder.php b/HtmlBuilder.php new file mode 100644 index 0000000..305ef06 --- /dev/null +++ b/HtmlBuilder.php @@ -0,0 +1,37 @@ + $value) + { + // For numeric keys we will assume that the key and the value are the same + // as this will convert HTML attributes such as "required" to a correct + // form like required="required" instead of using incorrect numerics. + if (is_numeric($key)) + { + $key = $value; + } + + // Before we add the attribute, we will make sure the value actually has a + // value so we don't append any null valued attributes onto the list of + // our attributes. We will just skip them over so we do not add them. + if ( ! is_null($value)) + { + $html[] = $key.'="'.e($value).'"'; + } + } + + return count($html) > 0 ? ' '.implode(' ', $html) : ''; + } + +} \ No newline at end of file