diff --git a/app/protected/controllers/SiteController.php b/app/protected/controllers/SiteController.php index 776c927..ab4b6c7 100644 --- a/app/protected/controllers/SiteController.php +++ b/app/protected/controllers/SiteController.php @@ -1,5 +1,8 @@ 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() diff --git a/app/protected/models/LoginForm.php b/app/protected/models/LoginForm.php index 7151bce..743f95d 100644 --- a/app/protected/models/LoginForm.php +++ b/app/protected/models/LoginForm.php @@ -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.'); } } diff --git a/app/protected/views/site/login.php b/app/protected/views/site/login.php index 3178f14..54afed2 100644 --- a/app/protected/views/site/login.php +++ b/app/protected/views/site/login.php @@ -1,7 +1,25 @@ +

Login

Please fill out the following fields to login:

-beginWidget('yii\widgets\ActiveForm', array('method' => 'put')); ?> +beginWidget('yii\widgets\ActiveForm'); ?> + 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(); + ?> + endWidget(); ?> \ No newline at end of file diff --git a/framework/helpers/base/Html.php b/framework/helpers/base/Html.php index 110dad9..e89e197 100644 --- a/framework/helpers/base/Html.php +++ b/framework/helpers/base/Html.php @@ -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); } /** diff --git a/framework/validators/RequiredValidator.php b/framework/validators/RequiredValidator.php index 3b13eb3..f4746e6 100644 --- a/framework/validators/RequiredValidator.php +++ b/framework/validators/RequiredValidator.php @@ -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}".'); } } diff --git a/framework/widgets/ActiveField.php b/framework/widgets/ActiveField.php index 50b9619..965f48a 100644 --- a/framework/widgets/ActiveField.php +++ b/framework/widgets/ActiveField.php @@ -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); } diff --git a/tests/unit/framework/helpers/HtmlTest.php b/tests/unit/framework/helpers/HtmlTest.php index 4077043..a3d0e65 100644 --- a/tests/unit/framework/helpers/HtmlTest.php +++ b/tests/unit/framework/helpers/HtmlTest.php @@ -157,20 +157,20 @@ class HtmlTest extends \yii\test\TestCase public function testButton() { $this->assertEquals('', Html::button()); - $this->assertEquals('', Html::button('test', 'value', 'content<>')); - $this->assertEquals('', Html::button('test', 'value', 'content<>', array('type' => 'submit', 'class' => "t"))); + $this->assertEquals('', Html::button('content<>', 'test', 'value')); + $this->assertEquals('', Html::button('content<>', 'test', 'value', array('type' => 'submit', 'class' => "t"))); } public function testSubmitButton() { $this->assertEquals('', Html::submitButton()); - $this->assertEquals('', Html::submitButton('test', 'value', 'content<>', array('class' => 't'))); + $this->assertEquals('', Html::submitButton('content<>', 'test', 'value', array('class' => 't'))); } public function testResetButton() { $this->assertEquals('', Html::resetButton()); - $this->assertEquals('', Html::resetButton('test', 'value', 'content<>', array('class' => 't'))); + $this->assertEquals('', Html::resetButton('content<>', 'test', 'value', array('class' => 't'))); } public function testInput()