From fc6e66838a7a3cacb8e240272df68852f2720b29 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 21 May 2013 13:12:32 +0200 Subject: [PATCH 1/8] added typeahead --- yii/bootstrap/TypeAhead.php | 114 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 yii/bootstrap/TypeAhead.php diff --git a/yii/bootstrap/TypeAhead.php b/yii/bootstrap/TypeAhead.php new file mode 100644 index 0000000..c087cca --- /dev/null +++ b/yii/bootstrap/TypeAhead.php @@ -0,0 +1,114 @@ + $form, + * 'model' => $model, + * 'attribute' => 'country', + * 'pluginOptions' => array( + * 'source' => array('USA', 'ESP'), + * ), + * )); + * ~~~ + * + * The following example will use the name property instead + * + * ~~~php + * echo TypeAhead::widget(array( + * 'name' => 'country', + * 'pluginOptions' => array( + * 'source' => array('USA', 'ESP'), + * ), + * )); + * + * + * @see http://twitter.github.io/bootstrap/javascript.html#typeahead + * @author Antonio Ramirez + * @since 2.0 + */ +class TypeAhead extends Widget +{ + /** + * @var ActiveForm the form that the TypeAhead field is associated with. If no form is associated with the widget + * then the id will be used instead + */ + public $form; + /** + * @var \yii\base\Model the data model that this field is associated with + */ + public $model; + /** + * @var string the model attribute that this field is associated with + */ + public $attribute; + + /** + * @var string the input name. This must be set if [[TypeAhead::$form]] is not set. + */ + public $name; + + /** + * Initializes the widget. + * Renders the input field. + */ + public function init() + { + parent::init(); + echo "\n" . $this->renderField(); + } + + /** + * Registers the plugin. + */ + public function run() + { + $this->registerPlugin('typeahead'); + } + + /** + * Renders the TypeAhead field. If [[TypeAhead::form]] has been specified then it will render an active field. + * Please, note that function will only check whether the form has been set, model and attributes will not. + * If [[TypeAhead::form]] is null not from an [[ActiveForm]] instance, then the field will be rendered according to + * the `name` key setting of [[TypeAhead::options]] array attribute. + * @return string the rendering result + */ + public function renderField() + { + if ($this->form instanceof ActiveForm) { + + $this->options['id'] = $this->id = Html::getInputId($this->model, $this->attribute); + + return Yii::createObject( + array( + 'class' => 'yii\widgets\ActiveField', + 'model' => $this->model, + 'attribute' => $this->attribute, + 'form' => $this->form + ) + )->textInput(); + } + + if (null === $this->name) + throw new InvalidParamException( + get_class($this) . ' must specify "form", "model" and "attribute" or "name" property values' + ); + + return Html::textInput($this->name, '', $this->options); + } +} \ No newline at end of file From 6c25c8fca972614fef62addb39a3a75282da5917 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 21 May 2013 13:27:20 +0200 Subject: [PATCH 2/8] minor fixes --- yii/bootstrap/TypeAhead.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/yii/bootstrap/TypeAhead.php b/yii/bootstrap/TypeAhead.php index c087cca..a907d6a 100644 --- a/yii/bootstrap/TypeAhead.php +++ b/yii/bootstrap/TypeAhead.php @@ -12,31 +12,31 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; /** - * Modal renders a modal window that can be toggled by clicking on a button. + * TypeAhead renders a typehead bootstrap javascript component. * * For example, * - * ~~~php + * ```php * echo TypeAhead::widget(array( * 'form' => $form, * 'model' => $model, - * 'attribute' => 'country', + * 'attribute' => 'country', * 'pluginOptions' => array( * 'source' => array('USA', 'ESP'), * ), * )); - * ~~~ + * ``` * * The following example will use the name property instead * - * ~~~php + * ```php * echo TypeAhead::widget(array( * 'name' => 'country', - * 'pluginOptions' => array( + * 'pluginOptions' => array( * 'source' => array('USA', 'ESP'), * ), * )); - * + *``` * * @see http://twitter.github.io/bootstrap/javascript.html#typeahead * @author Antonio Ramirez @@ -87,6 +87,7 @@ class TypeAhead extends Widget * If [[TypeAhead::form]] is null not from an [[ActiveForm]] instance, then the field will be rendered according to * the `name` key setting of [[TypeAhead::options]] array attribute. * @return string the rendering result + * @throws InvalidParamException */ public function renderField() { From f0f19e338aa175c49712a3a036355cc902a2357f Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 21 May 2013 13:28:08 +0200 Subject: [PATCH 3/8] comparison swapped --- yii/bootstrap/TypeAhead.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yii/bootstrap/TypeAhead.php b/yii/bootstrap/TypeAhead.php index a907d6a..6450ca1 100644 --- a/yii/bootstrap/TypeAhead.php +++ b/yii/bootstrap/TypeAhead.php @@ -105,7 +105,7 @@ class TypeAhead extends Widget )->textInput(); } - if (null === $this->name) + if ($this->name === null) throw new InvalidParamException( get_class($this) . ' must specify "form", "model" and "attribute" or "name" property values' ); From 8d1a745192cdd51e0b31d27cc3651a6319ba92fa Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 21 May 2013 16:08:54 +0200 Subject: [PATCH 4/8] fixed comments and code style --- yii/bootstrap/TypeAhead.php | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/yii/bootstrap/TypeAhead.php b/yii/bootstrap/TypeAhead.php index 6450ca1..bc2dd05 100644 --- a/yii/bootstrap/TypeAhead.php +++ b/yii/bootstrap/TypeAhead.php @@ -18,12 +18,12 @@ use yii\widgets\ActiveForm; * * ```php * echo TypeAhead::widget(array( - * 'form' => $form, - * 'model' => $model, - * 'attribute' => 'country', - * 'pluginOptions' => array( - * 'source' => array('USA', 'ESP'), - * ), + * 'form' => $form, + * 'model' => $model, + * 'attribute' => 'country', + * 'pluginOptions' => array( + * 'source' => array('USA', 'ESP'), + * ), * )); * ``` * @@ -31,10 +31,10 @@ use yii\widgets\ActiveForm; * * ```php * echo TypeAhead::widget(array( - * 'name' => 'country', - * 'pluginOptions' => array( - * 'source' => array('USA', 'ESP'), - * ), + * 'name' => 'country', + * 'pluginOptions' => array( + * 'source' => array('USA', 'ESP'), + * ), * )); *``` * @@ -59,7 +59,7 @@ class TypeAhead extends Widget public $attribute; /** - * @var string the input name. This must be set if [[TypeAhead::$form]] is not set. + * @var string the input name. This must be set if [[form]] is not set. */ public $name; @@ -70,7 +70,7 @@ class TypeAhead extends Widget public function init() { parent::init(); - echo "\n" . $this->renderField(); + echo "\n" . $this->renderField() . "\n"; } /** @@ -82,12 +82,13 @@ class TypeAhead extends Widget } /** - * Renders the TypeAhead field. If [[TypeAhead::form]] has been specified then it will render an active field. + * Renders the TypeAhead field. If [[form]] has been specified then it will render an active field. * Please, note that function will only check whether the form has been set, model and attributes will not. - * If [[TypeAhead::form]] is null not from an [[ActiveForm]] instance, then the field will be rendered according to - * the `name` key setting of [[TypeAhead::options]] array attribute. + * If [[form]] is null not from an [[ActiveForm]] instance, then the field will be rendered according to + * the `name` key setting of [[options]] array attribute. * @return string the rendering result - * @throws InvalidParamException + * @throws InvalidParamException when none of the required attributes are set to render the textInput. That is, + * if [[form]], [[model]] and [[attribute]] are not set, then [[name]] is required. */ public function renderField() { @@ -100,7 +101,7 @@ class TypeAhead extends Widget 'class' => 'yii\widgets\ActiveField', 'model' => $this->model, 'attribute' => $this->attribute, - 'form' => $this->form + 'form' => $this->form, ) )->textInput(); } @@ -112,4 +113,4 @@ class TypeAhead extends Widget return Html::textInput($this->name, '', $this->options); } -} \ No newline at end of file +} From fc78398447fdcae58791dd3d8c53c3dad6152e6d Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 21 May 2013 16:17:45 +0200 Subject: [PATCH 5/8] fixed code style --- yii/bootstrap/TypeAhead.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/yii/bootstrap/TypeAhead.php b/yii/bootstrap/TypeAhead.php index bc2dd05..757a76e 100644 --- a/yii/bootstrap/TypeAhead.php +++ b/yii/bootstrap/TypeAhead.php @@ -107,9 +107,11 @@ class TypeAhead extends Widget } if ($this->name === null) + { throw new InvalidParamException( get_class($this) . ' must specify "form", "model" and "attribute" or "name" property values' ); + } return Html::textInput($this->name, '', $this->options); } From 74aaa4f00d787af49acc6b0b57ce92e46a443e01 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 21 May 2013 16:45:21 +0200 Subject: [PATCH 6/8] code style fixes and refactored as stated on comments --- yii/bootstrap/TypeAhead.php | 63 ++++++++++++++------------------------------- 1 file changed, 20 insertions(+), 43 deletions(-) diff --git a/yii/bootstrap/TypeAhead.php b/yii/bootstrap/TypeAhead.php index 757a76e..06c6bb5 100644 --- a/yii/bootstrap/TypeAhead.php +++ b/yii/bootstrap/TypeAhead.php @@ -8,8 +8,8 @@ namespace yii\bootstrap; use Yii; +use yii\base\Model; use yii\helpers\Html; -use yii\widgets\ActiveForm; /** * TypeAhead renders a typehead bootstrap javascript component. @@ -18,12 +18,12 @@ use yii\widgets\ActiveForm; * * ```php * echo TypeAhead::widget(array( - * 'form' => $form, - * 'model' => $model, - * 'attribute' => 'country', - * 'pluginOptions' => array( - * 'source' => array('USA', 'ESP'), - * ), + * 'form' => $form, + * 'model' => $model, + * 'attribute' => 'country', + * 'pluginOptions' => array( + * 'source' => array('USA', 'ESP'), + * ), * )); * ``` * @@ -31,10 +31,10 @@ use yii\widgets\ActiveForm; * * ```php * echo TypeAhead::widget(array( - * 'name' => 'country', - * 'pluginOptions' => array( - * 'source' => array('USA', 'ESP'), - * ), + * 'name' => 'country', + * 'pluginOptions' => array( + * 'source' => array('USA', 'ESP'), + * ), * )); *``` * @@ -45,11 +45,6 @@ use yii\widgets\ActiveForm; class TypeAhead extends Widget { /** - * @var ActiveForm the form that the TypeAhead field is associated with. If no form is associated with the widget - * then the id will be used instead - */ - public $form; - /** * @var \yii\base\Model the data model that this field is associated with */ public $model; @@ -64,50 +59,32 @@ class TypeAhead extends Widget public $name; /** - * Initializes the widget. - * Renders the input field. - */ - public function init() - { - parent::init(); - echo "\n" . $this->renderField() . "\n"; - } - - /** - * Registers the plugin. + * Renders the widget */ public function run() { + echo "\n" . $this->renderField() . "\n"; $this->registerPlugin('typeahead'); } /** - * Renders the TypeAhead field. If [[form]] has been specified then it will render an active field. - * Please, note that function will only check whether the form has been set, model and attributes will not. - * If [[form]] is null not from an [[ActiveForm]] instance, then the field will be rendered according to - * the `name` key setting of [[options]] array attribute. + * Renders the TypeAhead field. If [[model]] has been specified then it will render an active field. + * If [[model]] is null or not from an [[Model]] instance, then the field will be rendered according to + * the [[name]] attribute. * @return string the rendering result * @throws InvalidParamException when none of the required attributes are set to render the textInput. That is, - * if [[form]], [[model]] and [[attribute]] are not set, then [[name]] is required. + * if [[model]] and [[attribute]] are not set, then [[name]] is required. */ public function renderField() { - if ($this->form instanceof ActiveForm) { + if ($this->model instanceof Model && $this->attribute !== null) { $this->options['id'] = $this->id = Html::getInputId($this->model, $this->attribute); - return Yii::createObject( - array( - 'class' => 'yii\widgets\ActiveField', - 'model' => $this->model, - 'attribute' => $this->attribute, - 'form' => $this->form, - ) - )->textInput(); + return Html::activeTextInput($this->model, $this->attribute, $this->options); } - if ($this->name === null) - { + if ($this->name === null) { throw new InvalidParamException( get_class($this) . ' must specify "form", "model" and "attribute" or "name" property values' ); From 51a4a9ee5151c305c4cb5b6c43e828db7d270449 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 21 May 2013 16:53:55 +0200 Subject: [PATCH 7/8] Apply latest feedback --- yii/bootstrap/TypeAhead.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/yii/bootstrap/TypeAhead.php b/yii/bootstrap/TypeAhead.php index 06c6bb5..a2dce0e 100644 --- a/yii/bootstrap/TypeAhead.php +++ b/yii/bootstrap/TypeAhead.php @@ -8,6 +8,7 @@ namespace yii\bootstrap; use Yii; +use yii\base\InvalidConfigException; use yii\base\Model; use yii\helpers\Html; @@ -52,11 +53,15 @@ class TypeAhead extends Widget * @var string the model attribute that this field is associated with */ public $attribute; - /** * @var string the input name. This must be set if [[form]] is not set. */ public $name; + /** + * @var string the input value. + */ + public $value; + /** * Renders the widget @@ -72,20 +77,20 @@ class TypeAhead extends Widget * If [[model]] is null or not from an [[Model]] instance, then the field will be rendered according to * the [[name]] attribute. * @return string the rendering result - * @throws InvalidParamException when none of the required attributes are set to render the textInput. That is, + * @throws InvalidConfigException when none of the required attributes are set to render the textInput. That is, * if [[model]] and [[attribute]] are not set, then [[name]] is required. */ public function renderField() { if ($this->model instanceof Model && $this->attribute !== null) { - $this->options['id'] = $this->id = Html::getInputId($this->model, $this->attribute); + $this->options['id'] = Html::getInputId($this->model, $this->attribute); return Html::activeTextInput($this->model, $this->attribute, $this->options); } if ($this->name === null) { - throw new InvalidParamException( + throw new InvalidConfigException( get_class($this) . ' must specify "form", "model" and "attribute" or "name" property values' ); } From 80ca760b2d9297778ac7b0a1edd621f80bf8e451 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 21 May 2013 16:55:20 +0200 Subject: [PATCH 8/8] dough! --- yii/bootstrap/TypeAhead.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yii/bootstrap/TypeAhead.php b/yii/bootstrap/TypeAhead.php index a2dce0e..fd1d90c 100644 --- a/yii/bootstrap/TypeAhead.php +++ b/yii/bootstrap/TypeAhead.php @@ -95,6 +95,6 @@ class TypeAhead extends Widget ); } - return Html::textInput($this->name, '', $this->options); + return Html::textInput($this->name, $this->value, $this->options); } }