Skip to content

Commit

Permalink
Added ActiveForm::mfield().
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Jun 19, 2013
1 parent c210de9 commit 9a5ca99
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 28 deletions.
11 changes: 7 additions & 4 deletions apps/advanced/backend/views/site/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

<p>Please fill out the following fields to login:</p>

<?php $form = ActiveForm::begin(array('options' => array('class' => 'form-horizontal'))); ?>
<?php echo $form->field($model, 'username')->textInput(); ?>
<?php echo $form->field($model, 'password')->passwordInput(); ?>
<?php echo $form->field($model, 'rememberMe')->checkbox(); ?>
<?php $form = ActiveForm::begin(array(
'model' => $model,
'options' => array('class' => 'form-horizontal'),
)); ?>
<?php echo $form->field('username')->textInput(); ?>
<?php echo $form->field('password')->passwordInput(); ?>
<?php echo $form->field('rememberMe')->checkbox(); ?>
<div class="form-actions">
<?php echo Html::submitButton('Login', null, null, array('class' => 'btn btn-primary')); ?>
</div>
Expand Down
11 changes: 6 additions & 5 deletions apps/advanced/frontend/views/site/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
</p>

<?php $form = ActiveForm::begin(array(
'model' => $model,
'options' => array('class' => 'form-horizontal'),
'fieldConfig' => array('inputOptions' => array('class' => 'input-xlarge')),
)); ?>
<?php echo $form->field($model, 'name')->textInput(); ?>
<?php echo $form->field($model, 'email')->textInput(); ?>
<?php echo $form->field($model, 'subject')->textInput(); ?>
<?php echo $form->field($model, 'body')->textArea(array('rows' => 6)); ?>
<?php echo $form->field($model, 'verifyCode')->widget(Captcha::className(), array(
<?php echo $form->field('name')->textInput(); ?>
<?php echo $form->field('email')->textInput(); ?>
<?php echo $form->field('subject')->textInput(); ?>
<?php echo $form->field('body')->textArea(array('rows' => 6)); ?>
<?php echo $form->field('verifyCode')->widget(Captcha::className(), array(
'options' => array('class' => 'input-medium'),
)); ?>
<div class="form-actions">
Expand Down
11 changes: 7 additions & 4 deletions apps/advanced/frontend/views/site/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

<p>Please fill out the following fields to login:</p>

<?php $form = ActiveForm::begin(array('options' => array('class' => 'form-horizontal'))); ?>
<?php echo $form->field($model, 'username')->textInput(); ?>
<?php echo $form->field($model, 'password')->passwordInput(); ?>
<?php echo $form->field($model, 'rememberMe')->checkbox(); ?>
<?php $form = ActiveForm::begin(array(
'model' => $model,
'options' => array('class' => 'form-horizontal'),
)); ?>
<?php echo $form->field('username')->textInput(); ?>
<?php echo $form->field('password')->passwordInput(); ?>
<?php echo $form->field('rememberMe')->checkbox(); ?>
<div class="form-actions">
<?php echo Html::submitButton('Login', null, null, array('class' => 'btn btn-primary')); ?>
</div>
Expand Down
11 changes: 6 additions & 5 deletions apps/basic/views/site/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
</p>

<?php $form = ActiveForm::begin(array(
'model' => $model,
'options' => array('class' => 'form-horizontal'),
'fieldConfig' => array('inputOptions' => array('class' => 'input-xlarge')),
)); ?>
<?php echo $form->field($model, 'name')->textInput(); ?>
<?php echo $form->field($model, 'email')->textInput(); ?>
<?php echo $form->field($model, 'subject')->textInput(); ?>
<?php echo $form->field($model, 'body')->textArea(array('rows' => 6)); ?>
<?php echo $form->field($model, 'verifyCode')->widget(Captcha::className(), array(
<?php echo $form->field('name')->textInput(); ?>
<?php echo $form->field('email')->textInput(); ?>
<?php echo $form->field('subject')->textInput(); ?>
<?php echo $form->field('body')->textArea(array('rows' => 6)); ?>
<?php echo $form->field('verifyCode')->widget(Captcha::className(), array(
'options' => array('class' => 'input-medium'),
)); ?>
<div class="form-actions">
Expand Down
11 changes: 7 additions & 4 deletions apps/basic/views/site/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

<p>Please fill out the following fields to login:</p>

<?php $form = ActiveForm::begin(array('options' => array('class' => 'form-horizontal'))); ?>
<?php echo $form->field($model, 'username')->textInput(); ?>
<?php echo $form->field($model, 'password')->passwordInput(); ?>
<?php echo $form->field($model, 'rememberMe')->checkbox(); ?>
<?php $form = ActiveForm::begin(array(
'model' => $model,
'options' => array('class' => 'form-horizontal'),
)); ?>
<?php echo $form->field('username')->textInput(); ?>
<?php echo $form->field('password')->passwordInput(); ?>
<?php echo $form->field('rememberMe')->checkbox(); ?>
<div class="form-actions">
<?php echo Html::submitButton('Login', null, null, array('class' => 'btn btn-primary')); ?>
</div>
Expand Down
6 changes: 3 additions & 3 deletions docs/guide/upgrade-from-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ is a container consisting of a label, an input, and an error message. It is repr
as an `ActiveField` object. Using fields, you can build a form more cleanly than before:

```php
<?php $form = yii\widgets\ActiveForm::begin(); ?>
<?php echo $form->field($model, 'username')->textInput(); ?>
<?php echo $form->field($model, 'password')->passwordInput(); ?>
<?php $form = yii\widgets\ActiveForm::begin(array('model' => $model)); ?>
<?php echo $form->field('username')->textInput(); ?>
<?php echo $form->field('password')->passwordInput(); ?>
<div class="form-actions">
<?php echo Html::submitButton('Login'); ?>
</div>
Expand Down
46 changes: 43 additions & 3 deletions framework/yii/widgets/ActiveForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace yii\widgets;

use Yii;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\base\Model;
use yii\helpers\Html;
Expand All @@ -30,6 +31,12 @@ class ActiveForm extends Widget
* Defaults to 'post'.
*/
public $method = 'post';
/**
* @var Model the model associated with this form. This property must be set if you call [[field()]]
* to create input fields. If a form is associated with multiple models, this property is not needed
* and you should use [[mfield()]] to create input fields.
*/
public $model;
/**
* @var array the HTML attributes (name-value pairs) for the form tag.
* The values will be HTML-encoded using [[Html::encode()]].
Expand Down Expand Up @@ -208,17 +215,50 @@ public function errorSummary($models, $options = array())
}

/**
* Generates a form field.
* Generates a form field using [[model]] and the specified attribute.
*
* A form field is associated with a model and an attribute. It contains a label, an input and an error message
* and use them to interact with end users to collect their inputs for the attribute.
* and uses them to interact with end users to collect their input for the attribute.
*
* This method is mainly used for a form with a single model.
*
* @param string $attribute the attribute name or expression. See [[Html::getAttributeName()]] for the format
* about attribute expression.
* @param array $options the additional configurations for the field object
* @return ActiveField the created ActiveField object
* @throws InvalidConfigException if [[model]] is not specified
* @see fieldConfig
* @see mfield
*/
public function field($attribute, $options = array())
{
if (!$this->model instanceof Model) {
throw new InvalidConfigException('The "model" property must be set.');
}
return Yii::createObject(array_merge($this->fieldConfig, $options, array(
'model' => $this->model,
'attribute' => $attribute,
'form' => $this,
)));
}

/**
* Generates a form field using the specified model and attribute.
*
* This method differs from [[field()]] in that the model is passed as a parameter rather than obtained
* from [[model]].
*
* This method is mainly used for a form with multiple models.
*
* @param Model $model the data model
* @param string $attribute the attribute name or expression. See [[Html::getAttributeName()]] for the format
* about attribute expression.
* @param array $options the additional configurations for the field object
* @return ActiveField the created ActiveField object
* @see fieldConfig
* @see field
*/
public function field($model, $attribute, $options = array())
public function mfield($model, $attribute, $options = array())
{
return Yii::createObject(array_merge($this->fieldConfig, $options, array(
'model' => $model,
Expand Down

0 comments on commit 9a5ca99

Please sign in to comment.