Commit e2073612 by Qiang Xue

Refactored the widget usage with ActiveField.

parent 920098c0
...@@ -31,15 +31,9 @@ $this->params['breadcrumbs'][] = $this->title; ...@@ -31,15 +31,9 @@ $this->params['breadcrumbs'][] = $this->title;
<?php echo $form->field($model, 'email')->textInput(); ?> <?php echo $form->field($model, 'email')->textInput(); ?>
<?php echo $form->field($model, 'subject')->textInput(); ?> <?php echo $form->field($model, 'subject')->textInput(); ?>
<?php echo $form->field($model, 'body')->textArea(array('rows' => 6)); ?> <?php echo $form->field($model, 'body')->textArea(array('rows' => 6)); ?>
<?php <?php echo $form->field($model, 'verifyCode')->widget(Captcha::className(), array(
$field = $form->field($model, 'verifyCode'); 'options' => array('class' => 'input-medium'),
echo $field->begin() )); ?>
. $field->label()
. Captcha::widget()
. Html::activeTextInput($model, 'verifyCode', array('class' => 'input-medium'))
. $field->error()
. $field->end();
?>
<div class="form-actions"> <div class="form-actions">
<?php echo Html::submitButton('Submit', null, null, array('class' => 'btn btn-primary')); ?> <?php echo Html::submitButton('Submit', null, null, array('class' => 'btn btn-primary')); ?>
</div> </div>
......
...@@ -557,7 +557,14 @@ class ActiveField extends Component ...@@ -557,7 +557,14 @@ class ActiveField extends Component
} }
/** /**
* Renders a field containing a widget. * Renders a field containing an input widget.
*
* Note that the widget must have both `model` and `attribute` properties. They will
* be initialized with [[model]] and [[attribute]] of this field, respectively.
*
* If you want to use a widget that does not have `model` and `attribute` properties,
* please use [[render()]] instead.
*
* @param string $class the widget class name * @param string $class the widget class name
* @param array $config name-value pairs that will be used to initialize the widget * @param array $config name-value pairs that will be used to initialize the widget
* @return string the rendering result * @return string the rendering result
...@@ -565,6 +572,9 @@ class ActiveField extends Component ...@@ -565,6 +572,9 @@ class ActiveField extends Component
public function widget($class, $config = array()) public function widget($class, $config = array())
{ {
/** @var \yii\base\Widget $class */ /** @var \yii\base\Widget $class */
$config['model'] = $this->model;
$config['attribute'] = $this->attribute;
$config['view'] = $this->form->getView();
return $this->render($class::widget($config)); return $this->render($class::widget($config));
} }
} }
...@@ -9,13 +9,12 @@ namespace yii\widgets; ...@@ -9,13 +9,12 @@ namespace yii\widgets;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\Html; use yii\helpers\Html;
use yii\helpers\Json; use yii\helpers\Json;
use yii\web\CaptchaAction; use yii\web\CaptchaAction;
/** /**
* Captcha renders a CAPTCHA image element. * Captcha renders a CAPTCHA image and an input field that takes user-entered verification code.
* *
* Captcha is used together with [[CaptchaAction]] provide [CAPTCHA](http://en.wikipedia.org/wiki/Captcha) * Captcha is used together with [[CaptchaAction]] provide [CAPTCHA](http://en.wikipedia.org/wiki/Captcha)
* - a way of preventing Website spamming. * - a way of preventing Website spamming.
...@@ -32,7 +31,7 @@ use yii\web\CaptchaAction; ...@@ -32,7 +31,7 @@ use yii\web\CaptchaAction;
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class Captcha extends Widget class Captcha extends InputWidget
{ {
/** /**
* @var string the route of the action that generates the CAPTCHA images. * @var string the route of the action that generates the CAPTCHA images.
...@@ -40,27 +39,66 @@ class Captcha extends Widget ...@@ -40,27 +39,66 @@ class Captcha extends Widget
*/ */
public $captchaAction = 'site/captcha'; public $captchaAction = 'site/captcha';
/** /**
* @var array HTML attributes to be applied to the rendered image element. * @var array HTML attributes to be applied to the text input field.
*/ */
public $options = array(); public $options = array();
/**
* @var array HTML attributes to be applied to the CAPTCHA image tag.
*/
public $imageOptions = array();
/**
* @var string the template for arranging the CAPTCHA image tag and the text input tag.
* In this template, the token `{image}` will be replaced with the actual image tag,
* while `{input}` will be replaced with the text input tag.
*/
public $template = '{image} {input}';
/** /**
* Renders the widget. * Initializes the widget.
*/ */
public function run() public function init()
{ {
parent::init();
$this->checkRequirements(); $this->checkRequirements();
if (!isset($this->options['id'])) { if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId(); $this->options['id'] = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId();
}
if (!isset($this->imageOptions['id'])) {
$this->imageOptions['id'] = $this->options['id'] . '-image';
}
}
/**
* Renders the widget.
*/
public function run()
{
$this->registerClientScript();
if ($this->hasModel()) {
$input = Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
$input = Html::textInput($this->name, $this->value, $this->options);
} }
$id = $this->options['id'];
$options = Json::encode($this->getClientOptions());
$this->view->registerAssetBundle('yii/captcha');
$this->view->registerJs("jQuery('#$id').yiiCaptcha($options);");
$url = Yii::$app->getUrlManager()->createUrl($this->captchaAction, array('v' => uniqid())); $url = Yii::$app->getUrlManager()->createUrl($this->captchaAction, array('v' => uniqid()));
echo Html::img($url, $this->options); $image = Html::img($url, $this->imageOptions);
echo strtr($this->template, array(
'{input}' => $input,
'{image}' => $image,
));
}
/**
* Registers the needed JavaScript.
*/
public function registerClientScript()
{
$options = $this->getClientOptions();
$options = empty($options) ? '' : Json::encode($options);
$id = $this->imageOptions['id'];
$this->getView()->registerAssetBundle('yii/captcha');
$this->getView()->registerJs("jQuery('#$id').yiiCaptcha($options);");
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment