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
b5be2ccf
Commit
b5be2ccf
authored
Mar 15, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
activeform WIP
parent
b71e8301
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
119 additions
and
15 deletions
+119
-15
ActiveForm.php
framework/widgets/ActiveForm.php
+119
-15
No files found.
framework/widgets/ActiveForm.php
View file @
b5be2ccf
...
@@ -8,6 +8,7 @@
...
@@ -8,6 +8,7 @@
namespace
yii\widgets
;
namespace
yii\widgets
;
use
Yii
;
use
Yii
;
use
yii\base\InvalidParamException
;
use
yii\base\Widget
;
use
yii\base\Widget
;
use
yii\base\Model
;
use
yii\base\Model
;
use
yii\util\Html
;
use
yii\util\Html
;
...
@@ -35,6 +36,7 @@ class ActiveForm extends Widget
...
@@ -35,6 +36,7 @@ class ActiveForm extends Widget
* @see errorSummary()
* @see errorSummary()
*/
*/
public
$errorSummaryClass
=
'yii-error-summary'
;
public
$errorSummaryClass
=
'yii-error-summary'
;
public
$errorMessageClass
=
'yii-error-message'
;
/**
/**
* @var string the default CSS class that indicates an input has error.
* @var string the default CSS class that indicates an input has error.
* This is
* This is
...
@@ -50,7 +52,10 @@ class ActiveForm extends Widget
...
@@ -50,7 +52,10 @@ class ActiveForm extends Widget
public
$enableClientValidation
=
false
;
public
$enableClientValidation
=
false
;
public
$options
=
array
();
public
$options
=
array
();
/**
* @var array model-class mapped to name prefix
*/
public
$modelMap
;
/**
/**
* @param Model|Model[] $models
* @param Model|Model[] $models
...
@@ -63,7 +68,7 @@ class ActiveForm extends Widget
...
@@ -63,7 +68,7 @@ class ActiveForm extends Widget
$models
=
array
(
$models
);
$models
=
array
(
$models
);
}
}
$showAll
=
isset
(
$options
[
'showAll'
])
&&
$options
[
'showAll'
]
)
;
$showAll
=
isset
(
$options
[
'showAll'
])
&&
$options
[
'showAll'
];
$lines
=
array
();
$lines
=
array
();
/** @var $model Model */
/** @var $model Model */
foreach
(
$models
as
$model
)
{
foreach
(
$models
as
$model
)
{
...
@@ -78,20 +83,22 @@ class ActiveForm extends Widget
...
@@ -78,20 +83,22 @@ class ActiveForm extends Widget
$header
=
isset
(
$options
[
'header'
])
?
$options
[
'header'
]
:
'<p>'
.
Yii
::
t
(
'yii|Please fix the following errors:'
)
.
'</p>'
;
$header
=
isset
(
$options
[
'header'
])
?
$options
[
'header'
]
:
'<p>'
.
Yii
::
t
(
'yii|Please fix the following errors:'
)
.
'</p>'
;
$footer
=
isset
(
$options
[
'footer'
])
?
$options
[
'footer'
]
:
''
;
$footer
=
isset
(
$options
[
'footer'
])
?
$options
[
'footer'
]
:
''
;
$
container
=
isset
(
$options
[
'container'
])
?
$options
[
'container
'
]
:
'div'
;
$
tag
=
isset
(
$options
[
'tag'
])
?
$options
[
'tag
'
]
:
'div'
;
unset
(
$options
[
'showAll'
],
$options
[
'header'
],
$options
[
'footer'
],
$options
[
'container'
]);
unset
(
$options
[
'showAll'
],
$options
[
'header'
],
$options
[
'footer'
],
$options
[
'container'
]);
if
(
!
isset
(
$options
[
'class'
]))
{
if
(
!
isset
(
$options
[
'class'
]))
{
$options
[
'class'
]
=
$this
->
errorSummaryClass
;
$options
[
'class'
]
=
$this
->
errorSummaryClass
;
}
else
{
$options
[
'class'
]
.=
' '
.
$this
->
errorSummaryClass
;
}
}
if
(
$lines
!==
array
())
{
if
(
$lines
!==
array
())
{
$content
=
"<ul><li>"
.
implode
(
"</li>
\n
<li>"
,
ArrayHelper
::
htmlEncode
(
$lines
))
.
"</li><ul>"
;
$content
=
"<ul><li>"
.
implode
(
"</li>
\n
<li>"
,
ArrayHelper
::
htmlEncode
(
$lines
))
.
"</li><ul>"
;
return
Html
::
tag
(
$
container
,
$header
.
$content
.
$footer
,
$options
);
return
Html
::
tag
(
$
tag
,
$header
.
$content
.
$footer
,
$options
);
}
else
{
}
else
{
$content
=
"<ul></ul>"
;
$content
=
"<ul></ul>"
;
$options
[
'style'
]
=
isset
(
$options
[
'style'
])
?
rtrim
(
$options
[
'style'
],
';'
)
.
'; display:none'
:
'display:none'
;
$options
[
'style'
]
=
isset
(
$options
[
'style'
])
?
rtrim
(
$options
[
'style'
],
';'
)
.
'; display:none'
:
'display:none'
;
return
Html
::
tag
(
$
container
,
$header
.
$content
.
$footer
,
$options
);
return
Html
::
tag
(
$
tag
,
$header
.
$content
.
$footer
,
$options
);
}
}
}
}
...
@@ -103,25 +110,32 @@ class ActiveForm extends Widget
...
@@ -103,25 +110,32 @@ class ActiveForm extends Widget
*/
*/
public
function
error
(
$model
,
$attribute
,
$options
=
array
())
public
function
error
(
$model
,
$attribute
,
$options
=
array
())
{
{
self
::
resolveName
(
$model
,
$attribute
);
// turn [a][b]attr into attr
$attribute
=
$this
->
normalizeAttributeName
(
$attribute
);
$container
=
isset
(
$options
[
'container'
])
?
$options
[
'container'
]
:
'div'
;
$this
->
getInputName
(
$model
,
$attribute
);
unset
(
$options
[
'container'
]);
$tag
=
isset
(
$options
[
'tag'
])
?
$options
[
'tag'
]
:
'div'
;
unset
(
$options
[
'tag'
]);
$error
=
$model
->
getFirstError
(
$attribute
);
$error
=
$model
->
getFirstError
(
$attribute
);
return
Html
::
tag
(
$container
,
Html
::
encode
(
$error
),
$options
);
return
Html
::
tag
(
$tag
,
Html
::
encode
(
$error
),
$options
);
}
public
function
resolveAttributeName
(
$name
)
{
}
}
/**
* @param Model $model
* @param string $attribute
* @param array $options
* @return string
*/
public
function
label
(
$model
,
$attribute
,
$options
=
array
())
public
function
label
(
$model
,
$attribute
,
$options
=
array
())
{
{
$attribute
=
$this
->
normalizeAttributeName
(
$attribute
);
$label
=
$model
->
getAttributeLabel
(
$attribute
);
return
Html
::
label
(
Html
::
encode
(
$label
),
isset
(
$options
[
'for'
])
?
$options
[
'for'
]
:
null
,
$options
);
}
}
public
function
input
(
$type
,
$model
,
$attribute
,
$options
=
array
())
public
function
input
(
$type
,
$model
,
$attribute
,
$options
=
array
())
{
{
return
''
;
$value
=
$this
->
getAttributeValue
(
$model
,
$attribute
);
$name
=
$this
->
getInputName
(
$model
,
$attribute
);
return
Html
::
input
(
$type
,
$name
,
$value
,
$options
);
}
}
public
function
textInput
(
$model
,
$attribute
,
$options
=
array
())
public
function
textInput
(
$model
,
$attribute
,
$options
=
array
())
...
@@ -146,29 +160,119 @@ class ActiveForm extends Widget
...
@@ -146,29 +160,119 @@ class ActiveForm extends Widget
public
function
textarea
(
$model
,
$attribute
,
$options
=
array
())
public
function
textarea
(
$model
,
$attribute
,
$options
=
array
())
{
{
$value
=
$this
->
getAttributeValue
(
$model
,
$attribute
);
$name
=
$this
->
getInputName
(
$model
,
$attribute
);
return
Html
::
textarea
(
$name
,
$value
,
$options
);
}
}
public
function
radio
(
$model
,
$attribute
,
$value
=
'1'
,
$options
=
array
())
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'
;
}
return
Html
::
radio
(
$name
,
$checked
,
$value
,
$options
);
}
}
public
function
checkbox
(
$model
,
$attribute
,
$value
=
'1'
,
$options
=
array
())
public
function
checkbox
(
$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'
;
}
return
Html
::
checkbox
(
$name
,
$checked
,
$value
,
$options
);
}
}
public
function
dropDownList
(
$model
,
$attribute
,
$items
,
$options
=
array
())
public
function
dropDownList
(
$model
,
$attribute
,
$items
,
$options
=
array
())
{
{
$checked
=
$this
->
getAttributeValue
(
$model
,
$attribute
);
$name
=
$this
->
getInputName
(
$model
,
$attribute
);
return
Html
::
dropDownList
(
$name
,
$checked
,
$items
,
$options
);
}
}
public
function
listBox
(
$model
,
$attribute
,
$items
,
$options
=
array
())
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'
;
}
return
Html
::
listBox
(
$name
,
$checked
,
$items
,
$options
);
}
}
public
function
checkboxList
(
$model
,
$attribute
,
$items
,
$options
=
array
())
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
())
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
getInputName
(
$model
,
$attribute
)
{
$class
=
get_class
(
$model
);
if
(
isset
(
$this
->
modelMap
[
$class
]))
{
$class
=
$this
->
modelMap
[
$class
];
}
elseif
((
$pos
=
strrpos
(
$class
,
'\\'
))
!==
false
)
{
$class
=
substr
(
$class
,
$pos
);
}
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
(
$class
===
''
&&
$prefix
===
''
)
{
return
$attribute
.
$suffix
;
}
elseif
(
$class
!==
''
)
{
return
$class
.
$prefix
.
"[
$attribute
]"
.
$suffix
;
}
else
{
throw
new
InvalidParamException
(
'Model name cannot be mapped to empty for tabular inputs.'
);
}
}
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
;
}
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
;
}
}
public
function
normalizeAttributeName
(
$attribute
)
{
if
(
preg_match
(
'/(^|.*\])(\w+)(\[.*|$)/'
,
$attribute
,
$matches
))
{
return
$matches
[
2
];
}
else
{
throw
new
InvalidParamException
(
'Attribute name must contain word characters only.'
);
}
}
}
}
}
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