Browse Source

activeform wip

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
399a53cfc4
  1. 19
      app/protected/controllers/SiteController.php
  2. 2
      app/protected/models/LoginForm.php
  3. 20
      app/protected/views/site/login.php
  4. 22
      framework/helpers/base/Html.php
  5. 2
      framework/validators/RequiredValidator.php
  6. 10
      framework/widgets/ActiveField.php
  7. 8
      tests/unit/framework/helpers/HtmlTest.php

19
app/protected/controllers/SiteController.php

@ -1,5 +1,8 @@
<?php <?php
use app\models\LoginForm;
use app\models\User;
class SiteController extends \yii\web\Controller class SiteController extends \yii\web\Controller
{ {
public function actionIndex() public function actionIndex()
@ -9,10 +12,18 @@ class SiteController extends \yii\web\Controller
public function actionLogin() public function actionLogin()
{ {
echo $this->render('login'); $model = new LoginForm();
// $user = app\models\User::findIdentity(100); if (isset($_POST[$model->formName()])) {
// Yii::$app->getUser()->login($user); $model->attributes = $_POST[$model->formName()];
// Yii::$app->getResponse()->redirect(array('site/index')); if ($model->validate()) {
$user = User::findByUsername($model->username);
Yii::$app->getUser()->login($user);
Yii::$app->getResponse()->redirect(array('site/index'));
}
}
echo $this->render('login', array(
'model' => $model,
));
} }
public function actionLogout() public function actionLogout()

2
app/protected/models/LoginForm.php

@ -30,7 +30,7 @@ class LoginForm extends Model
public function validatePassword() public function validatePassword()
{ {
$user = User::findByUsername($this->username); $user = User::findByUsername($this->username);
if (!$user && $user->validatePassword($this->password)) { if (!$user || !$user->validatePassword($this->password)) {
$this->addError('password', 'Incorrect username or password.'); $this->addError('password', 'Incorrect username or password.');
} }
} }

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

@ -1,7 +1,25 @@
<?php
use yii\helpers\Html;
/**
* @var yii\base\View $this
* @var yii\widgets\ActiveForm $form
* @var app\models\LoginForm $model
*/
?>
<h1>Login</h1> <h1>Login</h1>
<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', array('method' => 'put')); ?> <?php $form = $this->beginWidget('yii\widgets\ActiveForm'); ?>
<?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();
?>
<?php echo Html::submitButton('Login'); ?>
<?php $this->endWidget(); ?> <?php $this->endWidget(); ?>

22
framework/helpers/base/Html.php

@ -415,18 +415,18 @@ class Html
/** /**
* Generates a button tag. * Generates a button tag.
* @param string $name the name attribute. If it is null, the name attribute will not be generated.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. * @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded.
* Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, * Therefore you can pass in HTML code such as an image tag. If this is is coming from end users,
* you should consider [[encode()]] it to prevent XSS attacks. * you should consider [[encode()]] it to prevent XSS attacks.
* @param string $name the name attribute. If it is null, the name attribute will not be generated.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $options the tag options in terms of name-value pairs. These will be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* If the options does not contain "type", a "type" attribute with value "button" will be rendered. * If the options does not contain "type", a "type" attribute with value "button" will be rendered.
* @return string the generated button tag * @return string the generated button tag
*/ */
public static function button($name = null, $value = null, $content = 'Button', $options = array()) public static function button($content = 'Button', $name = null, $value = null, $options = array())
{ {
$options['name'] = $name; $options['name'] = $name;
$options['value'] = $value; $options['value'] = $value;
@ -438,38 +438,38 @@ class Html
/** /**
* Generates a submit button tag. * Generates a submit button tag.
* @param string $name the name attribute. If it is null, the name attribute will not be generated.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. * @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded.
* Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, * Therefore you can pass in HTML code such as an image tag. If this is is coming from end users,
* you should consider [[encode()]] it to prevent XSS attacks. * you should consider [[encode()]] it to prevent XSS attacks.
* @param string $name the name attribute. If it is null, the name attribute will not be generated.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $options the tag options in terms of name-value pairs. These will be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated submit button tag * @return string the generated submit button tag
*/ */
public static function submitButton($name = null, $value = null, $content = 'Submit', $options = array()) public static function submitButton($content = 'Submit', $name = null, $value = null, $options = array())
{ {
$options['type'] = 'submit'; $options['type'] = 'submit';
return static::button($name, $value, $content, $options); return static::button($content, $name, $value, $options);
} }
/** /**
* Generates a reset button tag. * Generates a reset button tag.
* @param string $name the name attribute. If it is null, the name attribute will not be generated.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded. * @param string $content the content enclosed within the button tag. It will NOT be HTML-encoded.
* Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, * Therefore you can pass in HTML code such as an image tag. If this is is coming from end users,
* you should consider [[encode()]] it to prevent XSS attacks. * you should consider [[encode()]] it to prevent XSS attacks.
* @param string $name the name attribute. If it is null, the name attribute will not be generated.
* @param string $value the value attribute. If it is null, the value attribute will not be generated.
* @param array $options the tag options in terms of name-value pairs. These will be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
* @return string the generated reset button tag * @return string the generated reset button tag
*/ */
public static function resetButton($name = null, $value = null, $content = 'Reset', $options = array()) public static function resetButton($content = 'Reset', $name = null, $value = null, $options = array())
{ {
$options['type'] = 'reset'; $options['type'] = 'reset';
return static::button($name, $value, $content, $options); return static::button($content, $name, $value, $options);
} }
/** /**

2
framework/validators/RequiredValidator.php

@ -47,7 +47,7 @@ class RequiredValidator extends Validator
{ {
parent::init(); parent::init();
if ($this->message === null) { if ($this->message === null) {
$this->message = $this->requiredValue === null ? Yii::t('yii|{attribute} is invalid.') $this->message = $this->requiredValue === null ? Yii::t('yii|{attribute} cannot be blank.')
: Yii::t('yii|{attribute} must be "{requiredValue}".'); : Yii::t('yii|{attribute} must be "{requiredValue}".');
} }
} }

10
framework/widgets/ActiveField.php

@ -35,6 +35,13 @@ class ActiveField extends Component
public function begin() public function begin()
{ {
if ($this->model->hasErrors($this->attribute)) {
if (isset($this->options['class'])) {
$this->options['class'] .= ' ' . $this->form->errorCssClass;
} else {
$this->options['class'] = $this->form->errorCssClass;
}
}
return Html::beginTag('div', $this->options); return Html::beginTag('div', $this->options);
} }
@ -52,6 +59,9 @@ class ActiveField extends Component
$tag = isset($options['tag']) ? $options['tag'] : 'div'; $tag = isset($options['tag']) ? $options['tag'] : 'div';
unset($options['tag']); unset($options['tag']);
$error = $this->model->getFirstError($attribute); $error = $this->model->getFirstError($attribute);
if ($error === null) {
$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
}
return Html::tag($tag, Html::encode($error), $options); return Html::tag($tag, Html::encode($error), $options);
} }

8
tests/unit/framework/helpers/HtmlTest.php

@ -157,20 +157,20 @@ class HtmlTest extends \yii\test\TestCase
public function testButton() public function testButton()
{ {
$this->assertEquals('<button type="button">Button</button>', Html::button()); $this->assertEquals('<button type="button">Button</button>', Html::button());
$this->assertEquals('<button type="button" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>')); $this->assertEquals('<button type="button" name="test" value="value">content<></button>', Html::button('content<>', 'test', 'value'));
$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>', array('type' => 'submit', 'class' => "t"))); $this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::button('content<>', 'test', 'value', array('type' => 'submit', 'class' => "t")));
} }
public function testSubmitButton() public function testSubmitButton()
{ {
$this->assertEquals('<button type="submit">Submit</button>', Html::submitButton()); $this->assertEquals('<button type="submit">Submit</button>', Html::submitButton());
$this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::submitButton('test', 'value', 'content<>', array('class' => 't'))); $this->assertEquals('<button type="submit" class="t" name="test" value="value">content<></button>', Html::submitButton('content<>', 'test', 'value', array('class' => 't')));
} }
public function testResetButton() public function testResetButton()
{ {
$this->assertEquals('<button type="reset">Reset</button>', Html::resetButton()); $this->assertEquals('<button type="reset">Reset</button>', Html::resetButton());
$this->assertEquals('<button type="reset" class="t" name="test" value="value">content<></button>', Html::resetButton('test', 'value', 'content<>', array('class' => 't'))); $this->assertEquals('<button type="reset" class="t" name="test" value="value">content<></button>', Html::resetButton('content<>', 'test', 'value', array('class' => 't')));
} }
public function testInput() public function testInput()

Loading…
Cancel
Save