Browse Source

activeform WIP

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
4131878512
  1. 4
      app/protected/controllers/SiteController.php
  2. 12
      app/protected/models/LoginForm.php
  3. 15
      app/protected/views/site/login.php
  4. 95
      framework/widgets/ActiveField.php
  5. 40
      framework/widgets/ActiveForm.php

4
app/protected/controllers/SiteController.php

@ -15,9 +15,7 @@ class SiteController extends \yii\web\Controller
$model = new LoginForm(); $model = new LoginForm();
if (isset($_POST[$model->formName()])) { if (isset($_POST[$model->formName()])) {
$model->attributes = $_POST[$model->formName()]; $model->attributes = $_POST[$model->formName()];
if ($model->validate()) { if ($model->login()) {
$user = User::findByUsername($model->username);
Yii::$app->getUser()->login($user);
Yii::$app->getResponse()->redirect(array('site/index')); Yii::$app->getResponse()->redirect(array('site/index'));
} }
} }

12
app/protected/models/LoginForm.php

@ -7,6 +7,7 @@
namespace app\models; namespace app\models;
use Yii;
use yii\base\Model; use yii\base\Model;
/** /**
@ -34,4 +35,15 @@ class LoginForm extends Model
$this->addError('password', 'Incorrect username or password.'); $this->addError('password', 'Incorrect username or password.');
} }
} }
public function login()
{
if ($this->validate()) {
$user = User::findByUsername($this->username);
Yii::$app->getUser()->login($user);
return true;
} else {
return false;
}
}
} }

15
app/protected/views/site/login.php

@ -12,14 +12,15 @@ use yii\helpers\Html;
<p>Please fill out the following fields to login:</p> <p>Please fill out the following fields to login:</p>
<?php $form = $this->beginWidget('yii\widgets\ActiveForm'); ?> <?php $form = $this->beginWidget('yii\widgets\ActiveForm'); ?>
<?php echo $form->field($model, 'username')->textInput(); ?>
<?php echo $form->field($model, 'password')->checkboxAlt(); ?>
<?php <?php
$field = $form->beginField($model, 'username'); $field = $form->field($model, 'username');
echo $field->label() . "\n" . $field->textInput() . "\n" . $field->error() . "\n"; echo $field->begin() . "\n"
$form->endField(); . $field->label() . "\n"
. Html::activeTextInput($model, 'username') . "\n"
$field = $form->beginField($model, 'password'); . $field->error() . "\n"
echo $field->label() . "\n" . $field->textInput() . "\n" . $field->error() . "\n"; . $field->end();
$form->endField();
?> ?>
<?php echo Html::submitButton('Login'); ?> <?php echo Html::submitButton('Login'); ?>
<?php $this->endWidget(); ?> <?php $this->endWidget(); ?>

95
framework/widgets/ActiveField.php

@ -8,6 +8,7 @@
namespace yii\widgets; namespace yii\widgets;
use yii\base\Component; use yii\base\Component;
use yii\base\InvalidParamException;
use yii\helpers\Html; use yii\helpers\Html;
/** /**
@ -17,6 +18,10 @@ use yii\helpers\Html;
class ActiveField extends Component class ActiveField extends Component
{ {
/** /**
* @var string the tag name. Defaults to 'div'.
*/
public $tag;
/**
* @var ActiveForm * @var ActiveForm
*/ */
public $form; public $form;
@ -35,42 +40,60 @@ class ActiveField extends Component
public function begin() public function begin()
{ {
$options = $this->options === null ? $this->form->fieldOptions : $this->options;
$this->tag = isset($options['tag']) ? $options['tag'] : 'div';
unset($options['tag']);
$class = isset($options['class']) ? array($options['class']) : array();
if ($this->form->autoFieldCssClass) {
$class[] = 'field-' . Html::getInputId($this->model, $this->attribute);
}
if ($this->model->isAttributeRequired($this->attribute)) {
$class[] = $this->form->requiredCssClass;
}
if ($this->model->hasErrors($this->attribute)) { if ($this->model->hasErrors($this->attribute)) {
if (isset($this->options['class'])) { $class[] = $this->form->errorCssClass;
$this->options['class'] .= ' ' . $this->form->errorCssClass; }
} else { if ($class !== array()) {
$this->options['class'] = $this->form->errorCssClass; $options['class'] = implode(' ', $class);
}
} }
return Html::beginTag('div', $this->options); return Html::beginTag($this->tag, $options);
} }
public function end() public function end()
{ {
return Html::endTag('div'); return Html::endTag($this->tag);
}
public function label($options = null)
{
if ($options === null) {
$options = $this->form->labelOptions;
}
return Html::activeLabel($this->model, $this->attribute, $options);
} }
public function error($options = array()) public function error($options = null)
{ {
if (empty($options)) { if ($options === null) {
$options = $this->form->errorOptions; $options = $this->form->errorOptions;
} }
$attribute = Html::getAttributeName($this->attribute); $attribute = Html::getAttributeName($this->attribute);
$tag = isset($options['tag']) ? $options['tag'] : 'div';
unset($options['tag']);
$error = $this->model->getFirstError($attribute); $error = $this->model->getFirstError($attribute);
if ($error === null) { if ($error === null) {
$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none'; $options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
} }
$tag = isset($options['tag']) ? $options['tag'] : 'span';
unset($options['tag']);
return Html::tag($tag, Html::encode($error), $options); return Html::tag($tag, Html::encode($error), $options);
} }
public function label($options = array()) protected function render($input)
{ {
if (empty($options)) { return $this->begin() . "\n" . strtr($this->form->fieldTemplate, array(
$options = $this->form->labelOptions; '{input}' => $input,
} '{label}' => $this->label(),
return Html::activeLabel($this->model, $this->attribute, $options); '{error}' => $this->error(),
)) . $this->end();
} }
/** /**
@ -82,7 +105,7 @@ class ActiveField extends Component
*/ */
public function input($type, $options = array()) public function input($type, $options = array())
{ {
return Html::activeInput($type, $this->model, $this->attribute, $options); return $this->render(Html::activeInput($type, $this->model, $this->attribute, $options));
} }
/** /**
@ -95,7 +118,7 @@ class ActiveField extends Component
*/ */
public function textInput($options = array()) public function textInput($options = array())
{ {
return Html::activeTextInput($this->model, $this->attribute, $options); return $this->render(Html::activeTextInput($this->model, $this->attribute, $options));
} }
/** /**
@ -108,7 +131,7 @@ class ActiveField extends Component
*/ */
public function hiddenInput($options = array()) public function hiddenInput($options = array())
{ {
return Html::activeHiddenInput($this->model, $this->attribute, $options); return $this->render(Html::activeHiddenInput($this->model, $this->attribute, $options));
} }
/** /**
@ -121,7 +144,7 @@ class ActiveField extends Component
*/ */
public function passwordInput($options = array()) public function passwordInput($options = array())
{ {
return Html::activeHiddenInput($this->model, $this->attribute, $options); return $this->render(Html::activePasswordInput($this->model, $this->attribute, $options));
} }
/** /**
@ -134,7 +157,7 @@ class ActiveField extends Component
*/ */
public function fileInput($options = array()) public function fileInput($options = array())
{ {
return Html::activeFileInput($this->model, $this->attribute, $options); return $this->render(Html::activeFileInput($this->model, $this->attribute, $options));
} }
/** /**
@ -146,7 +169,7 @@ class ActiveField extends Component
*/ */
public function textarea($options = array()) public function textarea($options = array())
{ {
return Html::activeTextarea($this->model, $this->attribute, $options); return $this->render(Html::activeTextarea($this->model, $this->attribute, $options));
} }
/** /**
@ -168,7 +191,16 @@ class ActiveField extends Component
*/ */
public function radio($value = '1', $options = array()) public function radio($value = '1', $options = array())
{ {
return Html::activeRadio($this->model, $this->attribute, $value, $options); return $this->render(Html::activeRadio($this->model, $this->attribute, $value, $options));
}
public function radioAlt($value = '1', $options = array())
{
$label = Html::encode($this->model->getAttributeLabel($this->attribute));
return $this->begin() . "\n"
. Html::label(Html::activeRadio($this->model, $this->attribute, $value, $options) . ' ' . $label) . "\n"
. $this->error() . "\n"
. $this->end();
} }
/** /**
@ -190,7 +222,16 @@ class ActiveField extends Component
*/ */
public function checkbox($value = '1', $options = array()) public function checkbox($value = '1', $options = array())
{ {
return Html::activeCheckbox($this->model, $this->attribute, $value, $options); return $this->render(Html::activeCheckbox($this->model, $this->attribute, $value, $options));
}
public function checkboxAlt($value = '1', $options = array())
{
$label = Html::encode($this->model->getAttributeLabel($this->attribute));
return $this->begin() . "\n"
. Html::label(Html::activeCheckbox($this->model, $this->attribute, $value, $options) . ' ' . $label) . "\n"
. $this->error() . "\n"
. $this->end();
} }
/** /**
@ -225,9 +266,9 @@ class ActiveField extends Component
* *
* @return string the generated drop-down list tag * @return string the generated drop-down list tag
*/ */
public function DropDownList($items, $options = array()) public function dropDownList($items, $options = array())
{ {
return Html::activeDropDownList($this->model, $this->attribute, $items, $options); return $this->render(Html::activeDropDownList($this->model, $this->attribute, $items, $options));
} }
/** /**
@ -267,7 +308,7 @@ class ActiveField extends Component
*/ */
public function listBox($items, $options = array()) public function listBox($items, $options = array())
{ {
return Html::activeListBox($this->model, $this->attribute, $items, $options); return $this->render(Html::activeListBox($this->model, $this->attribute, $items, $options));
} }
/** /**

40
framework/widgets/ActiveForm.php

@ -8,8 +8,6 @@
namespace yii\widgets; namespace yii\widgets;
use Yii; use Yii;
use yii\base\InvalidCallException;
use yii\base\InvalidParamException;
use yii\base\Widget; use yii\base\Widget;
use yii\base\Model; use yii\base\Model;
use yii\helpers\Html; use yii\helpers\Html;
@ -33,25 +31,32 @@ class ActiveForm extends Widget
*/ */
public $method = 'post'; public $method = 'post';
public $options = array(); public $options = array();
public $errorOptions = array('tag' => 'div', 'class' => 'yii-error-message'); public $fieldOptions = array('tag' => 'div', 'class' => 'yii-field');
public $labelOptions = array('class' => 'yii-input-label'); public $fieldTemplate = "{label}\n{input}\n{error}";
public $autoFieldCssClass = true;
public $errorOptions = array('tag' => 'span', 'class' => 'yii-error-message');
public $labelOptions = array('class' => 'control-label');
/** /**
* @var string the default CSS class for the error summary container. * @var string the default CSS class for the error summary container.
* @see errorSummary() * @see errorSummary()
*/ */
public $errorSummaryCssClass = 'yii-error-summary'; public $errorSummaryCssClass = 'yii-error-summary';
/** /**
* @var string the default CSS class that indicates an input is required.
*/
public $requiredCssClass = 'required';
/**
* @var string the default CSS class that indicates an input has error. * @var string the default CSS class that indicates an input has error.
*/ */
public $errorCssClass = 'yii-error'; public $errorCssClass = 'error';
/** /**
* @var string the default CSS class that indicates an input validated successfully. * @var string the default CSS class that indicates an input validated successfully.
*/ */
public $successCssClass = 'yii-success'; public $successCssClass = 'success';
/** /**
* @var string the default CSS class that indicates an input is currently being validated. * @var string the default CSS class that indicates an input is currently being validated.
*/ */
public $validatingCssClass = 'yii-validating'; public $validatingCssClass = 'validating';
/** /**
* @var boolean whether to enable client-side data validation. Defaults to false. * @var boolean whether to enable client-side data validation. Defaults to false.
* When this property is set true, client-side validation will be performed by validators * When this property is set true, client-side validation will be performed by validators
@ -123,31 +128,14 @@ class ActiveForm extends Widget
} }
} }
/** public function field($model, $attribute, $options = null)
* @var ActiveField[]
*/
private $_fieldStack = array();
public function beginField($model, $attribute, $options = array())
{ {
$field = Yii::createObject(array( return Yii::createObject(array(
'class' => $this->fieldClass, 'class' => $this->fieldClass,
'model' => $model, 'model' => $model,
'attribute' => $attribute, 'attribute' => $attribute,
'form' => $this, 'form' => $this,
'options' => $options, 'options' => $options,
)); ));
echo $field->begin();
return $this->_fieldStack[] = $field;
}
public function endField()
{
if ($this->_fieldStack !== array()) {
$field = array_pop($this->_fieldStack);
echo $field->end();
} else {
throw new InvalidCallException('The "beginField" and "endField" calls are not matching.');
}
} }
} }

Loading…
Cancel
Save