From 4c668c88a636dc82befa4a8ab1709386da871d93 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 27 Feb 2013 21:49:30 -0600 Subject: [PATCH] working on forms. --- FormBuilder.php | 75 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 18 deletions(-) diff --git a/FormBuilder.php b/FormBuilder.php index 65e525f..1de4f80 100644 --- a/FormBuilder.php +++ b/FormBuilder.php @@ -20,6 +20,13 @@ class FormBuilder { */ protected $tokenProvider; + /** + * The current model instance for the form. + * + * @var mixed + */ + protected $model; + /** * An array of label names we've created. * @@ -83,6 +90,32 @@ class FormBuilder { return ''.$append; } + /** + * Create a new model based form builder. + * + * @param mixed $model + * @param array $attributes + * @return string + */ + public function model($model, array $attributes) + { + $this->model = $model; + + return $this->open($attributes); + } + + /** + * Close the current form. + * + * @return string + */ + public function close() + { + $this->model = null; + + return ''; + } + /** * Generate a hidden field with the current CSRF token. * @@ -106,15 +139,18 @@ class FormBuilder { { $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. + $id = $this->getIdAttribute($name, $attributes); + $value = $this->getValueAttribute($name, $value); $merge = compact('type', 'value', 'id'); + // Once we have the type, value, and ID we can marge them into the rest of the + // attributes array so we can convert them into their HTML attribute format + // when creating the HTML element. Then, we will return the entire input. $attributes = array_merge($attributes, $merge); return ''; @@ -187,15 +223,31 @@ class FormBuilder { // Next we will look for the rows and cols attributes, as each of these are put // on the textarea element definition. If they are not present, we will just // assume some sane default values for these attributes for the developer. - if ( ! isset($attributes['rows'])) $attributes['rows'] = 10; - - if ( ! isset($attributes['cols'])) $attributes['cols'] = 50; + $attributes = $this->setTextAreaSize($attributes); $value = (string) $this->getValueAttribute($value); return ''.e($value).''; } + /** + * Set the text area size on the attributes. + * + * @param array $attributes + * @return array + */ + protected function setTextAreaSize($attributes) + { + if (isset($attributes['size'])) + { + $segments = explode('x', $attributes['size']); + + return array_merge($attributes, array('cols' => $segments[0], 'rows' => $segments[1])); + } + + return array_merge($attributes, array('cols' => 50, 'rows' => 10)); + } + /** * Parse the form action method. * @@ -290,17 +342,4 @@ class FormBuilder { } } - /** - * 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