Commit 399a53cf by Qiang Xue

activeform wip

parent 3a146a06
<?php
use app\models\LoginForm;
use app\models\User;
class SiteController extends \yii\web\Controller
{
public function actionIndex()
......@@ -9,10 +12,18 @@ class SiteController extends \yii\web\Controller
public function actionLogin()
{
echo $this->render('login');
// $user = app\models\User::findIdentity(100);
// Yii::$app->getUser()->login($user);
// Yii::$app->getResponse()->redirect(array('site/index'));
$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);
Yii::$app->getResponse()->redirect(array('site/index'));
}
}
echo $this->render('login', array(
'model' => $model,
));
}
public function actionLogout()
......
......@@ -30,7 +30,7 @@ class LoginForm extends Model
public function validatePassword()
{
$user = User::findByUsername($this->username);
if (!$user && $user->validatePassword($this->password)) {
if (!$user || !$user->validatePassword($this->password)) {
$this->addError('password', 'Incorrect username or password.');
}
}
......
<?php
use yii\helpers\Html;
/**
* @var yii\base\View $this
* @var yii\widgets\ActiveForm $form
* @var app\models\LoginForm $model
*/
?>
<h1>Login</h1>
<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(); ?>
\ No newline at end of file
......@@ -415,18 +415,18 @@ class Html
/**
* 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.
* 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.
* @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
* 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 the options does not contain "type", a "type" attribute with value "button" will be rendered.
* @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['value'] = $value;
......@@ -438,38 +438,38 @@ class Html
/**
* 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.
* 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.
* @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
* 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.
* @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';
return static::button($name, $value, $content, $options);
return static::button($content, $name, $value, $options);
}
/**
* 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.
* 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.
* @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
* 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.
* @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';
return static::button($name, $value, $content, $options);
return static::button($content, $name, $value, $options);
}
/**
......
......@@ -47,7 +47,7 @@ class RequiredValidator extends Validator
{
parent::init();
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}".');
}
}
......
......@@ -35,6 +35,13 @@ class ActiveField extends Component
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);
}
......@@ -52,6 +59,9 @@ class ActiveField extends Component
$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';
}
return Html::tag($tag, Html::encode($error), $options);
}
......
......@@ -157,20 +157,20 @@ class HtmlTest extends \yii\test\TestCase
public function testButton()
{
$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="submit" class="t" name="test" value="value">content<></button>', Html::button('test', 'value', 'content<>', array('type' => 'submit', 'class' => "t")));
$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('content<>', 'test', 'value', array('type' => 'submit', 'class' => "t")));
}
public function testSubmitButton()
{
$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()
{
$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()
......
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