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
784bdb32
Commit
784bdb32
authored
Apr 26, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactoring activeform.
parent
90cfb6eb
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
207 deletions
+28
-207
Html.php
framework/helpers/base/Html.php
+0
-0
ActiveField.php
framework/widgets/ActiveField.php
+0
-0
ActiveForm.php
framework/widgets/ActiveForm.php
+28
-207
No files found.
framework/helpers/base/Html.php
View file @
784bdb32
This diff is collapsed.
Click to expand it.
framework/widgets/ActiveField.php
0 → 100644
View file @
784bdb32
This diff is collapsed.
Click to expand it.
framework/widgets/ActiveForm.php
View file @
784bdb32
...
...
@@ -8,6 +8,7 @@
namespace
yii\widgets
;
use
Yii
;
use
yii\base\InvalidCallException
;
use
yii\base\InvalidParamException
;
use
yii\base\Widget
;
use
yii\base\Model
;
...
...
@@ -31,25 +32,26 @@ class ActiveForm extends Widget
* Defaults to 'post'.
*/
public
$method
=
'post'
;
public
$options
=
array
();
public
$errorOptions
=
array
(
'tag'
=>
'div'
,
'class'
=>
'yii-error-message'
);
public
$labelOptions
=
array
(
'class'
=>
'yii-input-label'
);
/**
* @var string the default CSS class for the error summary container.
* @see errorSummary()
*/
public
$errorSummaryClass
=
'yii-error-summary'
;
public
$errorMessageClass
=
'yii-error-message'
;
public
$errorSummaryCssClass
=
'yii-error-summary'
;
/**
* @var string the default CSS class that indicates an input has error.
*/
public
$errorClass
=
'yii-error'
;
public
$errorC
ssC
lass
=
'yii-error'
;
/**
* @var string the default CSS class that indicates an input validated successfully.
*/
public
$successClass
=
'yii-success'
;
public
$successCssClass
=
'yii-success'
;
/**
* @var string the default CSS class that indicates an input is currently being validated.
*/
public
$validatingClass
=
'yii-validating'
;
public
$validatingC
ssC
lass
=
'yii-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
...
...
@@ -57,9 +59,7 @@ class ActiveForm extends Widget
*/
public
$enableClientValidation
=
false
;
public
$options
=
array
();
public
$fieldClass
=
'yii\widgets\ActiveField'
;
/**
* Initializes the widget.
* This renders the form open tag.
...
...
@@ -108,9 +108,9 @@ class ActiveForm extends Widget
unset
(
$options
[
'showAll'
],
$options
[
'header'
],
$options
[
'footer'
],
$options
[
'container'
]);
if
(
!
isset
(
$options
[
'class'
]))
{
$options
[
'class'
]
=
$this
->
errorSummaryClass
;
$options
[
'class'
]
=
$this
->
errorSummaryC
ssC
lass
;
}
else
{
$options
[
'class'
]
.=
' '
.
$this
->
errorSummaryClass
;
$options
[
'class'
]
.=
' '
.
$this
->
errorSummaryC
ssC
lass
;
}
if
(
$lines
!==
array
())
{
...
...
@@ -124,209 +124,30 @@ class ActiveForm extends Widget
}
/**
* @param Model $model
* @param string $attribute
* @param array $options
* @return string
*/
public
function
error
(
$model
,
$attribute
,
$options
=
array
())
{
$attribute
=
$this
->
getAttributeName
(
$attribute
);
$tag
=
isset
(
$options
[
'tag'
])
?
$options
[
'tag'
]
:
'div'
;
unset
(
$options
[
'tag'
]);
$error
=
$model
->
getFirstError
(
$attribute
);
return
Html
::
tag
(
$tag
,
Html
::
encode
(
$error
),
$options
);
}
/**
* @param Model $model
* @param string $attribute
* @param array $options
* @return string
* @var ActiveField[]
*/
public
function
label
(
$model
,
$attribute
,
$options
=
array
())
{
$attribute
=
$this
->
getAttributeName
(
$attribute
);
$label
=
isset
(
$options
[
'label'
])
?
$options
[
'label'
]
:
Html
::
encode
(
$model
->
getAttributeLabel
(
$attribute
));
$for
=
array_key_exists
(
'for'
,
$options
)
?
$options
[
'for'
]
:
$this
->
getInputId
(
$model
,
$attribute
);
return
Html
::
label
(
$label
,
$for
,
$options
);
}
private
$_fieldStack
=
array
();
/**
* @param string $type
* @param Model $model
* @param string $attribute
* @param array $options
*
* @return string
*/
public
function
input
(
$type
,
$model
,
$attribute
,
$options
=
array
())
public
function
beginField
(
$model
,
$attribute
,
$options
=
array
())
{
$value
=
$this
->
getAttributeValue
(
$model
,
$attribute
);
$name
=
$this
->
getInputName
(
$model
,
$attribute
);
if
(
!
array_key_exists
(
'id'
,
$options
))
{
$options
[
'id'
]
=
$this
->
getInputId
(
$model
,
$attribute
);
}
return
Html
::
input
(
$type
,
$name
,
$value
,
$options
);
}
public
function
textInput
(
$model
,
$attribute
,
$options
=
array
())
{
return
$this
->
input
(
'text'
,
$model
,
$attribute
,
$options
);
}
public
function
hiddenInput
(
$model
,
$attribute
,
$options
=
array
())
{
return
$this
->
input
(
'hidden'
,
$model
,
$attribute
,
$options
);
}
public
function
passwordInput
(
$model
,
$attribute
,
$options
=
array
())
{
return
$this
->
input
(
'password'
,
$model
,
$attribute
,
$options
);
}
public
function
fileInput
(
$model
,
$attribute
,
$options
=
array
())
{
return
$this
->
input
(
'file'
,
$model
,
$attribute
,
$options
);
}
public
function
textarea
(
$model
,
$attribute
,
$options
=
array
())
{
$value
=
$this
->
getAttributeValue
(
$model
,
$attribute
);
$name
=
$this
->
getInputName
(
$model
,
$attribute
);
if
(
!
array_key_exists
(
'id'
,
$options
))
{
$options
[
'id'
]
=
$this
->
getInputId
(
$model
,
$attribute
);
}
return
Html
::
textarea
(
$name
,
$value
,
$options
);
}
public
function
radio
(
$model
,
$attribute
,
$value
=
'1'
,
$options
=
array
())
{
$checked
=
$this
->
getAttributeValue
(
$model
,
$attribute
);
$name
=
$this
->
getInputName
(
$model
,
$attribute
);
if
(
!
array_key_exists
(
'uncheck'
,
$options
))
{
$options
[
'unchecked'
]
=
'0'
;
}
if
(
!
array_key_exists
(
'id'
,
$options
))
{
$options
[
'id'
]
=
$this
->
getInputId
(
$model
,
$attribute
);
}
return
Html
::
radio
(
$name
,
$checked
,
$value
,
$options
);
$field
=
Yii
::
createObject
(
array
(
'class'
=>
$this
->
fieldClass
,
'model'
=>
$model
,
'attribute'
=>
$attribute
,
'form'
=>
$this
,
'options'
=>
$options
,
));
echo
$field
->
begin
();
return
$this
->
_fieldStack
[]
=
$field
;
}
public
function
checkbox
(
$model
,
$attribute
,
$value
=
'1'
,
$options
=
array
()
)
public
function
endField
(
)
{
$checked
=
$this
->
getAttributeValue
(
$model
,
$attribute
);
$name
=
$this
->
getInputName
(
$model
,
$attribute
);
if
(
!
array_key_exists
(
'uncheck'
,
$options
))
{
$options
[
'unchecked'
]
=
'0'
;
}
if
(
!
array_key_exists
(
'id'
,
$options
))
{
$options
[
'id'
]
=
$this
->
getInputId
(
$model
,
$attribute
);
}
return
Html
::
checkbox
(
$name
,
$checked
,
$value
,
$options
);
}
public
function
dropDownList
(
$model
,
$attribute
,
$items
,
$options
=
array
())
{
$checked
=
$this
->
getAttributeValue
(
$model
,
$attribute
);
$name
=
$this
->
getInputName
(
$model
,
$attribute
);
if
(
!
array_key_exists
(
'id'
,
$options
))
{
$options
[
'id'
]
=
$this
->
getInputId
(
$model
,
$attribute
);
}
return
Html
::
dropDownList
(
$name
,
$checked
,
$items
,
$options
);
}
public
function
listBox
(
$model
,
$attribute
,
$items
,
$options
=
array
())
{
$checked
=
$this
->
getAttributeValue
(
$model
,
$attribute
);
$name
=
$this
->
getInputName
(
$model
,
$attribute
);
if
(
!
array_key_exists
(
'unselect'
,
$options
))
{
$options
[
'unselect'
]
=
'0'
;
}
if
(
!
array_key_exists
(
'id'
,
$options
))
{
$options
[
'id'
]
=
$this
->
getInputId
(
$model
,
$attribute
);
}
return
Html
::
listBox
(
$name
,
$checked
,
$items
,
$options
);
}
public
function
checkboxList
(
$model
,
$attribute
,
$items
,
$options
=
array
())
{
$checked
=
$this
->
getAttributeValue
(
$model
,
$attribute
);
$name
=
$this
->
getInputName
(
$model
,
$attribute
);
if
(
!
array_key_exists
(
'unselect'
,
$options
))
{
$options
[
'unselect'
]
=
'0'
;
}
return
Html
::
checkboxList
(
$name
,
$checked
,
$items
,
$options
);
}
public
function
radioList
(
$model
,
$attribute
,
$items
,
$options
=
array
())
{
$checked
=
$this
->
getAttributeValue
(
$model
,
$attribute
);
$name
=
$this
->
getInputName
(
$model
,
$attribute
);
if
(
!
array_key_exists
(
'unselect'
,
$options
))
{
$options
[
'unselect'
]
=
'0'
;
}
return
Html
::
radioList
(
$name
,
$checked
,
$items
,
$options
);
}
public
function
getAttributeValue
(
$model
,
$attribute
)
{
if
(
!
preg_match
(
'/(^|.*\])(\w+)(\[.*|$)/'
,
$attribute
,
$matches
))
{
throw
new
InvalidParamException
(
'Attribute name must contain word characters only.'
);
}
$attribute
=
$matches
[
2
];
$index
=
$matches
[
3
];
if
(
$index
===
''
)
{
return
$model
->
$attribute
;
if
(
$this
->
_fieldStack
!==
array
())
{
$field
=
array_pop
(
$this
->
_fieldStack
);
echo
$field
->
end
();
}
else
{
$value
=
$model
->
$attribute
;
foreach
(
explode
(
']['
,
trim
(
$index
,
'[]'
))
as
$id
)
{
if
((
is_array
(
$value
)
||
$value
instanceof
\ArrayAccess
)
&&
isset
(
$value
[
$id
]))
{
$value
=
$value
[
$id
];
}
else
{
return
null
;
}
}
return
$value
;
throw
new
InvalidCallException
(
'The "beginField" and "endField" calls are not matching.'
);
}
}
public
function
getAttributeName
(
$attribute
)
{
if
(
preg_match
(
'/(^|.*\])(\w+)(\[.*|$)/'
,
$attribute
,
$matches
))
{
return
$matches
[
2
];
}
else
{
throw
new
InvalidParamException
(
'Attribute name must contain word characters only.'
);
}
}
/**
* @param Model $model
* @param string $attribute
* @return string
* @throws \yii\base\InvalidParamException
*/
public
static
function
getInputName
(
$model
,
$attribute
)
{
$formName
=
$model
->
formName
();
if
(
!
preg_match
(
'/(^|.*\])(\w+)(\[.*|$)/'
,
$attribute
,
$matches
))
{
throw
new
InvalidParamException
(
'Attribute name must contain word characters only.'
);
}
$prefix
=
$matches
[
1
];
$attribute
=
$matches
[
2
];
$suffix
=
$matches
[
3
];
if
(
$formName
===
''
&&
$prefix
===
''
)
{
return
$attribute
.
$suffix
;
}
elseif
(
$formName
!==
''
)
{
return
$formName
.
$prefix
.
"[
$attribute
]"
.
$suffix
;
}
else
{
throw
new
InvalidParamException
(
get_class
(
$model
)
.
'::formName() cannot be empty for tabular inputs.'
);
}
}
public
static
function
getInputId
(
$model
,
$attribute
)
{
$name
=
static
::
getInputName
(
$model
,
$attribute
);
return
str_replace
(
array
(
'[]'
,
']['
,
'['
,
']'
,
' '
),
array
(
''
,
'-'
,
'-'
,
''
,
'-'
),
$name
);
}
}
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