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
41318785
Commit
41318785
authored
Apr 27, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
activeform WIP
parent
a4b7d493
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
105 additions
and
63 deletions
+105
-63
SiteController.php
app/protected/controllers/SiteController.php
+1
-3
LoginForm.php
app/protected/models/LoginForm.php
+13
-0
login.php
app/protected/views/site/login.php
+9
-7
ActiveField.php
framework/widgets/ActiveField.php
+68
-27
ActiveForm.php
framework/widgets/ActiveForm.php
+14
-26
No files found.
app/protected/controllers/SiteController.php
View file @
41318785
...
...
@@ -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'
));
}
}
...
...
app/protected/models/LoginForm.php
View file @
41318785
...
...
@@ -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
app/protected/views/site/login.php
View file @
41318785
...
...
@@ -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
framework/widgets/ActiveField.php
View file @
41318785
...
...
@@ -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
);
}
p
ublic
function
label
(
$options
=
array
()
)
p
rotected
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
D
ropDownList
(
$items
,
$options
=
array
())
public
function
d
ropDownList
(
$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
)
);
}
/**
...
...
framework/widgets/ActiveForm.php
View file @
41318785
...
...
@@ -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.'
);
}
}
}
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