Commit 41318785 by Qiang Xue

activeform WIP

parent a4b7d493
......@@ -15,9 +15,7 @@ class SiteController extends \yii\web\Controller
$model = new LoginForm();
if (isset($_POST[$model->formName()])) {
$model->attributes = $_POST[$model->formName()];
if ($model->validate()) {
$user = User::findByUsername($model->username);
Yii::$app->getUser()->login($user);
if ($model->login()) {
Yii::$app->getResponse()->redirect(array('site/index'));
}
}
......
......@@ -7,6 +7,7 @@
namespace app\models;
use Yii;
use yii\base\Model;
/**
......@@ -34,4 +35,15 @@ class LoginForm extends Model
$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;
}
}
}
\ No newline at end of file
......@@ -12,14 +12,15 @@ use yii\helpers\Html;
<p>Please fill out the following fields to login:</p>
<?php $form = $this->beginWidget('yii\widgets\ActiveForm'); ?>
<?php echo $form->field($model, 'username')->textInput(); ?>
<?php echo $form->field($model, 'password')->checkboxAlt(); ?>
<?php
$field = $form->beginField($model, 'username');
echo $field->label() . "\n" . $field->textInput() . "\n" . $field->error() . "\n";
$form->endField();
$field = $form->beginField($model, 'password');
echo $field->label() . "\n" . $field->textInput() . "\n" . $field->error() . "\n";
$form->endField();
$field = $form->field($model, 'username');
echo $field->begin() . "\n"
. $field->label() . "\n"
. Html::activeTextInput($model, 'username') . "\n"
. $field->error() . "\n"
. $field->end();
?>
<?php echo Html::submitButton('Login'); ?>
<?php $this->endWidget(); ?>
\ No newline at end of file
......@@ -8,6 +8,7 @@
namespace yii\widgets;
use yii\base\Component;
use yii\base\InvalidParamException;
use yii\helpers\Html;
/**
......@@ -17,6 +18,10 @@ use yii\helpers\Html;
class ActiveField extends Component
{
/**
* @var string the tag name. Defaults to 'div'.
*/
public $tag;
/**
* @var ActiveForm
*/
public $form;
......@@ -35,42 +40,60 @@ class ActiveField extends Component
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 (isset($this->options['class'])) {
$this->options['class'] .= ' ' . $this->form->errorCssClass;
} else {
$this->options['class'] = $this->form->errorCssClass;
}
$class[] = $this->form->errorCssClass;
}
if ($class !== array()) {
$options['class'] = implode(' ', $class);
}
return Html::beginTag('div', $this->options);
return Html::beginTag($this->tag, $options);
}
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;
}
$attribute = Html::getAttributeName($this->attribute);
$tag = isset($options['tag']) ? $options['tag'] : 'div';
unset($options['tag']);
$error = $this->model->getFirstError($attribute);
if ($error === null) {
$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);
}
public function label($options = array())
protected function render($input)
{
if (empty($options)) {
$options = $this->form->labelOptions;
}
return Html::activeLabel($this->model, $this->attribute, $options);
return $this->begin() . "\n" . strtr($this->form->fieldTemplate, array(
'{input}' => $input,
'{label}' => $this->label(),
'{error}' => $this->error(),
)) . $this->end();
}
/**
......@@ -82,7 +105,7 @@ class ActiveField extends Component
*/
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())
{
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())
{
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())
{
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())
{
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())
{
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())
{
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())
{
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
*/
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())
{
return Html::activeListBox($this->model, $this->attribute, $items, $options);
return $this->render(Html::activeListBox($this->model, $this->attribute, $items, $options));
}
/**
......
......@@ -8,8 +8,6 @@
namespace yii\widgets;
use Yii;
use yii\base\InvalidCallException;
use yii\base\InvalidParamException;
use yii\base\Widget;
use yii\base\Model;
use yii\helpers\Html;
......@@ -33,25 +31,32 @@ class ActiveForm extends Widget
*/
public $method = 'post';
public $options = array();
public $errorOptions = array('tag' => 'div', 'class' => 'yii-error-message');
public $labelOptions = array('class' => 'yii-input-label');
public $fieldOptions = array('tag' => 'div', 'class' => 'yii-field');
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.
* @see errorSummary()
*/
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.
*/
public $errorCssClass = 'yii-error';
public $errorCssClass = 'error';
/**
* @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.
*/
public $validatingCssClass = 'yii-validating';
public $validatingCssClass = 'validating';
/**
* @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
......@@ -123,31 +128,14 @@ class ActiveForm extends Widget
}
}
/**
* @var ActiveField[]
*/
private $_fieldStack = array();
public function beginField($model, $attribute, $options = array())
public function field($model, $attribute, $options = null)
{
$field = Yii::createObject(array(
return Yii::createObject(array(
'class' => $this->fieldClass,
'model' => $model,
'attribute' => $attribute,
'form' => $this,
'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.');
}
}
}
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