Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
399a53cf
Commit
399a53cf
authored
Apr 27, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
activeform wip
parent
3a146a06
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
62 additions
and
22 deletions
+62
-22
SiteController.php
app/protected/controllers/SiteController.php
+15
-4
LoginForm.php
app/protected/models/LoginForm.php
+1
-1
login.php
app/protected/views/site/login.php
+20
-1
Html.php
framework/helpers/base/Html.php
+11
-11
RequiredValidator.php
framework/validators/RequiredValidator.php
+1
-1
ActiveField.php
framework/widgets/ActiveField.php
+10
-0
HtmlTest.php
tests/unit/framework/helpers/HtmlTest.php
+4
-4
No files found.
app/protected/controllers/SiteController.php
View file @
399a53cf
<?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
()
...
...
app/protected/models/LoginForm.php
View file @
399a53cf
...
...
@@ -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.'
);
}
}
...
...
app/protected/views/site/login.php
View file @
399a53cf
<?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
framework/helpers/base/Html.php
View file @
399a53cf
...
...
@@ -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
);
}
/**
...
...
framework/validators/RequiredValidator.php
View file @
399a53cf
...
...
@@ -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}".'
);
}
}
...
...
framework/widgets/ActiveField.php
View file @
399a53cf
...
...
@@ -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
);
}
...
...
tests/unit/framework/helpers/HtmlTest.php
View file @
399a53cf
...
...
@@ -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
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment