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
7ca1be35
Commit
7ca1be35
authored
Apr 22, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #3008: Added `Html::errorSummary()`
parent
83a63f23
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
32 deletions
+41
-32
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
BaseHtml.php
framework/helpers/BaseHtml.php
+37
-0
ActiveForm.php
framework/widgets/ActiveForm.php
+3
-32
No files found.
framework/CHANGELOG.md
View file @
7ca1be35
...
...
@@ -21,6 +21,7 @@ Yii Framework 2 Change Log
-
Bug #3204:
`yii\di\Container`
did not handle the
`$config`
parameter well in case when it does not have a default value (qiangxue)
-
Bug #3216: Fixed the bug that
`yii.activeForm.destroy()`
did not remove
`submit`
event handlers (qiangxue)
-
Enh #2837: Error page now shows arguments in stack trace method calls (samdark)
-
Enh #3008: Added
`Html::errorSummary()`
(qiangxue)
-
Enh #3088: The debug and gii modules will manage their own URL rules now (hiltonjanfield, qiangxue)
-
Enh #3103: debugger panel is now not displayed when printing a page (githubjeka)
-
Enh #3108: Added
`yii\debug\Module::enableDebugLogs`
to disable logging debug logs by default (qiangxue)
...
...
framework/helpers/BaseHtml.php
View file @
7ca1be35
...
...
@@ -1032,6 +1032,43 @@ class BaseHtml
}
/**
* Generates a summary of the validation errors.
* If there is no validation error, an empty error summary markup will still be generated, but it will be hidden.
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled:
*
* - header: string, the header HTML for the error summary. If not set, a default prompt string will be used.
* - footer: string, the footer HTML for the error summary.
*
* The rest of the options will be rendered as the attributes of the container 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 error summary
*/
public
static
function
errorSummary
(
$models
,
$options
=
[])
{
$lines
=
[];
foreach
((
array
)
$models
as
$model
)
{
/** @var Model $model */
foreach
(
$model
->
getFirstErrors
()
as
$error
)
{
$lines
[]
=
Html
::
encode
(
$error
);
}
}
$header
=
isset
(
$options
[
'header'
])
?
$options
[
'header'
]
:
'<p>'
.
Yii
::
t
(
'yii'
,
'Please fix the following errors:'
)
.
'</p>'
;
$footer
=
isset
(
$options
[
'footer'
])
?
$options
[
'footer'
]
:
''
;
unset
(
$options
[
'header'
],
$options
[
'footer'
]);
if
(
empty
(
$lines
))
{
// still render the placeholder for client-side validation use
$content
=
"<ul></ul>"
;
$options
[
'style'
]
=
isset
(
$options
[
'style'
])
?
rtrim
(
$options
[
'style'
],
';'
)
.
'; display:none'
:
'display:none'
;
}
else
{
$content
=
"<ul><li>"
.
implode
(
"</li>
\n
<li>"
,
$lines
)
.
"</li></ul>"
;
}
return
Html
::
tag
(
'div'
,
$header
.
$content
.
$footer
,
$options
);
}
/**
* Generates a tag that contains the first validation error of the specified model attribute.
* Note that even if there is no validation error, this method will still return an empty error tag.
* @param Model $model the model object
...
...
framework/widgets/ActiveForm.php
View file @
7ca1be35
...
...
@@ -217,41 +217,12 @@ class ActiveForm extends Widget
* The rest of the options will be rendered as the attributes of the container tag. The values will
* be HTML-encoded using [[\yii\helpers\Html::encode()]]. If a value is null, the corresponding attribute will not be rendered.
* @return string the generated error summary
* @see errorSummaryCssClass
*/
public
function
errorSummary
(
$models
,
$options
=
[])
{
if
(
!
is_array
(
$models
))
{
$models
=
[
$models
];
}
$lines
=
[];
foreach
(
$models
as
$model
)
{
/** @var Model $model */
foreach
(
$model
->
getFirstErrors
()
as
$error
)
{
$lines
[]
=
Html
::
encode
(
$error
);
}
}
$header
=
isset
(
$options
[
'header'
])
?
$options
[
'header'
]
:
'<p>'
.
Yii
::
t
(
'yii'
,
'Please fix the following errors:'
)
.
'</p>'
;
$footer
=
isset
(
$options
[
'footer'
])
?
$options
[
'footer'
]
:
''
;
unset
(
$options
[
'header'
],
$options
[
'footer'
]);
if
(
!
isset
(
$options
[
'class'
]))
{
$options
[
'class'
]
=
$this
->
errorSummaryCssClass
;
}
else
{
$options
[
'class'
]
.=
' '
.
$this
->
errorSummaryCssClass
;
}
if
(
!
empty
(
$lines
))
{
$content
=
"<ul><li>"
.
implode
(
"</li>
\n
<li>"
,
$lines
)
.
"</li></ul>"
;
return
Html
::
tag
(
'div'
,
$header
.
$content
.
$footer
,
$options
);
}
else
{
$content
=
"<ul></ul>"
;
$options
[
'style'
]
=
isset
(
$options
[
'style'
])
?
rtrim
(
$options
[
'style'
],
';'
)
.
'; display:none'
:
'display:none'
;
return
Html
::
tag
(
'div'
,
$header
.
$content
.
$footer
,
$options
);
}
Html
::
addCssClass
(
$options
,
$this
->
errorSummaryCssClass
);
return
Html
::
errorSummary
(
$models
,
$options
);
}
/**
...
...
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