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
9f7ca5d2
Commit
9f7ca5d2
authored
Oct 23, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1051 from cebe/validator-message-format
Validator message format
parents
ee2af266
3631890c
Hide whitespace changes
Inline
Side-by-side
Showing
23 changed files
with
145 additions
and
30 deletions
+145
-30
I18N.php
framework/yii/i18n/I18N.php
+41
-0
BooleanValidator.php
framework/yii/validators/BooleanValidator.php
+2
-2
CompareValidator.php
framework/yii/validators/CompareValidator.php
+2
-2
FileValidator.php
framework/yii/validators/FileValidator.php
+5
-5
NumberValidator.php
framework/yii/validators/NumberValidator.php
+2
-2
RequiredValidator.php
framework/yii/validators/RequiredValidator.php
+1
-1
StringValidator.php
framework/yii/validators/StringValidator.php
+3
-3
Validator.php
framework/yii/validators/Validator.php
+3
-3
BaseListView.php
framework/yii/widgets/BaseListView.php
+13
-11
BooleanValidatorTest.php
tests/unit/framework/validators/BooleanValidatorTest.php
+6
-0
CompareValidatorTest.php
tests/unit/framework/validators/CompareValidatorTest.php
+5
-0
DateValidatorTest.php
tests/unit/framework/validators/DateValidatorTest.php
+6
-0
DefaultValueValidatorTest.php
...s/unit/framework/validators/DefaultValueValidatorTest.php
+6
-0
EmailValidatorTest.php
tests/unit/framework/validators/EmailValidatorTest.php
+6
-0
ExistValidatorTest.php
tests/unit/framework/validators/ExistValidatorTest.php
+1
-0
FilterValidatorTest.php
tests/unit/framework/validators/FilterValidatorTest.php
+6
-0
NumberValidatorTest.php
tests/unit/framework/validators/NumberValidatorTest.php
+6
-0
RangeValidatorTest.php
tests/unit/framework/validators/RangeValidatorTest.php
+6
-0
RegularExpressionValidatorTest.php
...t/framework/validators/RegularExpressionValidatorTest.php
+6
-0
RequiredValidatorTest.php
tests/unit/framework/validators/RequiredValidatorTest.php
+6
-0
UniqueValidatorTest.php
tests/unit/framework/validators/UniqueValidatorTest.php
+1
-0
UrlValidatorTest.php
tests/unit/framework/validators/UrlValidatorTest.php
+6
-0
ValidatorTest.php
tests/unit/framework/validators/ValidatorTest.php
+6
-1
No files found.
framework/yii/i18n/I18N.php
View file @
9f7ca5d2
...
...
@@ -62,6 +62,9 @@ class I18N extends Component
/**
* Translates a message to the specified language.
*
* After translation the message will be formatted using [[MessageFormatter]] if it contains
* ICU message format and `$params` are not empty.
*
* @param string $category the message category.
* @param string $message the message to be translated.
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
...
...
@@ -101,6 +104,44 @@ class I18N extends Component
}
/**
* Formats a message using using [[MessageFormatter]].
*
* @param string $message the message to be formatted.
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
* @param string $language the language code (e.g. `en_US`, `en`).
* @return string the formatted message.
*/
public
function
format
(
$message
,
$params
,
$language
)
{
$params
=
(
array
)
$params
;
if
(
$params
===
[])
{
return
$message
;
}
if
(
preg_match
(
'~{\s*[\d\w]+\s*,~u'
,
$message
))
{
$formatter
=
new
MessageFormatter
(
$language
,
$message
);
if
(
$formatter
===
null
)
{
Yii
::
warning
(
"Message for
$language
is invalid:
$message
."
);
return
$message
;
}
$result
=
$formatter
->
format
(
$params
);
if
(
$result
===
false
)
{
$errorMessage
=
$formatter
->
getErrorMessage
();
Yii
::
warning
(
"Formatting message for
$language
failed with error:
$errorMessage
. Message is:
$message
."
);
return
$message
;
}
else
{
return
$result
;
}
}
$p
=
[];
foreach
(
$params
as
$name
=>
$value
)
{
$p
[
'{'
.
$name
.
'}'
]
=
$value
;
}
return
strtr
(
$message
,
$p
);
}
/**
* Returns the message source for the given category.
* @param string $category the category name.
* @return MessageSource the message source for the given category.
...
...
framework/yii/validators/BooleanValidator.php
View file @
9f7ca5d2
...
...
@@ -58,8 +58,8 @@ class BooleanValidator extends Validator
$value
=
$object
->
$attribute
;
if
(
!
$this
->
validateValue
(
$value
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
,
[
'
{true}
'
=>
$this
->
trueValue
,
'
{false}
'
=>
$this
->
falseValue
,
'
true
'
=>
$this
->
trueValue
,
'
false
'
=>
$this
->
falseValue
,
]);
}
}
...
...
framework/yii/validators/CompareValidator.php
View file @
9f7ca5d2
...
...
@@ -143,8 +143,8 @@ class CompareValidator extends Validator
}
if
(
!
$valid
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
,
[
'
{compareAttribute}
'
=>
$compareLabel
,
'
{compareValue}
'
=>
$compareValue
,
'
compareAttribute
'
=>
$compareLabel
,
'
compareValue
'
=>
$compareValue
,
]);
}
}
...
...
framework/yii/validators/FileValidator.php
View file @
9f7ca5d2
...
...
@@ -144,7 +144,7 @@ class FileValidator extends Validator
$this
->
addError
(
$object
,
$attribute
,
$this
->
uploadRequired
);
}
if
(
count
(
$files
)
>
$this
->
maxFiles
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooMany
,
[
'
{attribute}'
=>
$attribute
,
'{limit}
'
=>
$this
->
maxFiles
]);
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooMany
,
[
'
limit
'
=>
$this
->
maxFiles
]);
}
else
{
foreach
(
$files
as
$file
)
{
$this
->
validateFile
(
$object
,
$attribute
,
$file
);
...
...
@@ -171,18 +171,18 @@ class FileValidator extends Validator
switch
(
$file
->
error
)
{
case
UPLOAD_ERR_OK
:
if
(
$this
->
maxSize
!==
null
&&
$file
->
size
>
$this
->
maxSize
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooBig
,
[
'
{file}'
=>
$file
->
name
,
'{limit}
'
=>
$this
->
getSizeLimit
()]);
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooBig
,
[
'
file'
=>
$file
->
name
,
'limit
'
=>
$this
->
getSizeLimit
()]);
}
if
(
$this
->
minSize
!==
null
&&
$file
->
size
<
$this
->
minSize
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooSmall
,
[
'
{file}'
=>
$file
->
name
,
'{limit}
'
=>
$this
->
minSize
]);
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooSmall
,
[
'
file'
=>
$file
->
name
,
'limit
'
=>
$this
->
minSize
]);
}
if
(
!
empty
(
$this
->
types
)
&&
!
in_array
(
strtolower
(
pathinfo
(
$file
->
name
,
PATHINFO_EXTENSION
)),
$this
->
types
,
true
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
wrongType
,
[
'
{file}'
=>
$file
->
name
,
'{extensions}
'
=>
implode
(
', '
,
$this
->
types
)]);
$this
->
addError
(
$object
,
$attribute
,
$this
->
wrongType
,
[
'
file'
=>
$file
->
name
,
'extensions
'
=>
implode
(
', '
,
$this
->
types
)]);
}
break
;
case
UPLOAD_ERR_INI_SIZE
:
case
UPLOAD_ERR_FORM_SIZE
:
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooBig
,
[
'
{file}'
=>
$file
->
name
,
'{limit}
'
=>
$this
->
getSizeLimit
()]);
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooBig
,
[
'
file'
=>
$file
->
name
,
'limit
'
=>
$this
->
getSizeLimit
()]);
break
;
case
UPLOAD_ERR_PARTIAL
:
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
...
...
framework/yii/validators/NumberValidator.php
View file @
9f7ca5d2
...
...
@@ -91,10 +91,10 @@ class NumberValidator extends Validator
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
}
if
(
$this
->
min
!==
null
&&
$value
<
$this
->
min
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooSmall
,
[
'
{min}
'
=>
$this
->
min
]);
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooSmall
,
[
'
min
'
=>
$this
->
min
]);
}
if
(
$this
->
max
!==
null
&&
$value
>
$this
->
max
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooBig
,
[
'
{max}
'
=>
$this
->
max
]);
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooBig
,
[
'
max
'
=>
$this
->
max
]);
}
}
...
...
framework/yii/validators/RequiredValidator.php
View file @
9f7ca5d2
...
...
@@ -75,7 +75,7 @@ class RequiredValidator extends Validator
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
);
}
else
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
message
,
[
'
{requiredValue}
'
=>
$this
->
requiredValue
,
'
requiredValue
'
=>
$this
->
requiredValue
,
]);
}
}
...
...
framework/yii/validators/StringValidator.php
View file @
9f7ca5d2
...
...
@@ -112,13 +112,13 @@ class StringValidator extends Validator
$length
=
mb_strlen
(
$value
,
$this
->
encoding
);
if
(
$this
->
min
!==
null
&&
$length
<
$this
->
min
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooShort
,
[
'
{min}
'
=>
$this
->
min
]);
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooShort
,
[
'
min
'
=>
$this
->
min
]);
}
if
(
$this
->
max
!==
null
&&
$length
>
$this
->
max
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooLong
,
[
'
{max}
'
=>
$this
->
max
]);
$this
->
addError
(
$object
,
$attribute
,
$this
->
tooLong
,
[
'
max
'
=>
$this
->
max
]);
}
if
(
$this
->
length
!==
null
&&
$length
!==
$this
->
length
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
notEqual
,
[
'
{length}
'
=>
$this
->
length
]);
$this
->
addError
(
$object
,
$attribute
,
$this
->
notEqual
,
[
'
length
'
=>
$this
->
length
]);
}
}
...
...
framework/yii/validators/Validator.php
View file @
9f7ca5d2
...
...
@@ -251,9 +251,9 @@ abstract class Validator extends Component
public
function
addError
(
$object
,
$attribute
,
$message
,
$params
=
[])
{
$value
=
$object
->
$attribute
;
$params
[
'
{attribute}
'
]
=
$object
->
getAttributeLabel
(
$attribute
);
$params
[
'
{value}
'
]
=
is_array
(
$value
)
?
'array()'
:
$value
;
$object
->
addError
(
$attribute
,
strtr
(
$message
,
$params
));
$params
[
'
attribute
'
]
=
$object
->
getAttributeLabel
(
$attribute
);
$params
[
'
value
'
]
=
is_array
(
$value
)
?
'array()'
:
$value
;
$object
->
addError
(
$attribute
,
Yii
::
$app
->
getI18n
()
->
format
(
$message
,
$params
,
Yii
::
$app
->
language
));
}
/**
...
...
framework/yii/widgets/BaseListView.php
View file @
9f7ca5d2
...
...
@@ -131,30 +131,32 @@ abstract class BaseListView extends Widget
public
function
renderSummary
()
{
$count
=
$this
->
dataProvider
->
getCount
();
if
((
$pagination
=
$this
->
dataProvider
->
getPagination
())
!==
false
&&
$count
>
0
)
{
if
((
$pagination
=
$this
->
dataProvider
->
getPagination
())
!==
false
)
{
$totalCount
=
$this
->
dataProvider
->
getTotalCount
();
$begin
=
$pagination
->
getPage
()
*
$pagination
->
pageSize
+
1
;
$end
=
$begin
+
$count
-
1
;
$page
=
$pagination
->
getPage
()
+
1
;
$pageCount
=
$pagination
->
pageCount
;
if
((
$summaryContent
=
$this
->
summary
)
===
null
)
{
$summaryContent
=
'<div class="summary">'
.
Yii
::
t
(
'yii'
,
'Showing <b>{begin}-{end}</b> of <b>{totalCount}</b> {0, plural, =1{item} other{items}}.'
,
$totalCount
)
.
'</div>'
;
$summaryContent
=
'<div class="summary">'
.
Yii
::
t
(
'yii'
,
'Showing <b>{totalCount, plural, =0{0} other{{begin}-{end}}}</b> of <b>{totalCount}</b> {totalCount, plural, one{item} other{items}}.'
)
.
'</div>'
;
}
}
else
{
$begin
=
$page
=
$pageCount
=
1
;
$end
=
$totalCount
=
$count
;
if
((
$summaryContent
=
$this
->
summary
)
===
null
)
{
$summaryContent
=
'<div class="summary">'
.
Yii
::
t
(
'yii'
,
'Total <b>{count}</b> {
0, plural, =1{item} other{items}}.'
,
$count
)
.
'</div>'
;
$summaryContent
=
'<div class="summary">'
.
Yii
::
t
(
'yii'
,
'Total <b>{count}</b> {
count, plural, one{item} other{items}}.'
)
.
'</div>'
;
}
}
return
strtr
(
$summaryContent
,
[
'
{begin}
'
=>
$begin
,
'
{end}
'
=>
$end
,
'
{count}
'
=>
$count
,
'
{totalCount}
'
=>
$totalCount
,
'
{page}
'
=>
$page
,
'
{pageCount}
'
=>
$pageCount
,
]);
return
Yii
::
$app
->
getI18n
()
->
format
(
$summaryContent
,
[
'
begin
'
=>
$begin
,
'
end
'
=>
$end
,
'
count
'
=>
$count
,
'
totalCount
'
=>
$totalCount
,
'
page
'
=>
$page
,
'
pageCount
'
=>
$pageCount
,
]
,
Yii
::
$app
->
language
);
}
/**
...
...
tests/unit/framework/validators/BooleanValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -10,6 +10,12 @@ use yiiunit\TestCase;
*/
class
BooleanValidatorTest
extends
TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testValidateValue
()
{
$val
=
new
BooleanValidator
;
...
...
tests/unit/framework/validators/CompareValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -10,6 +10,11 @@ use yiiunit\TestCase;
class
CompareValidatorTest
extends
TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testValidateValueException
()
{
...
...
tests/unit/framework/validators/DateValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -10,6 +10,12 @@ use yiiunit\TestCase;
class
DateValidatorTest
extends
TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testEnsureMessageIsSet
()
{
$val
=
new
DateValidator
;
...
...
tests/unit/framework/validators/DefaultValueValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -8,6 +8,12 @@ use yiiunit\TestCase;
*/
class
DefaultValueValidatorTest
extends
TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testValidateAttribute
()
{
$val
=
new
DefaultValueValidator
;
...
...
tests/unit/framework/validators/EmailValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -11,6 +11,12 @@ use yiiunit\TestCase;
*/
class
EmailValidatorTest
extends
TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testValidateValue
()
{
$validator
=
new
EmailValidator
();
...
...
tests/unit/framework/validators/ExistValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -18,6 +18,7 @@ class ExistValidatorTest extends DatabaseTestCase
public
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
ActiveRecord
::
$db
=
$this
->
getConnection
();
}
...
...
tests/unit/framework/validators/FilterValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -9,6 +9,12 @@ use yiiunit\TestCase;
class
FilterValidatorTest
extends
TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testAssureExceptionOnInit
()
{
$this
->
setExpectedException
(
'yii\base\InvalidConfigException'
);
...
...
tests/unit/framework/validators/NumberValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -9,6 +9,12 @@ use yiiunit\TestCase;
class
NumberValidatorTest
extends
TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testEnsureMessageOnInit
()
{
$val
=
new
NumberValidator
;
...
...
tests/unit/framework/validators/RangeValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -9,6 +9,12 @@ use yiiunit\TestCase;
class
RangeValidatorTest
extends
TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testInitException
()
{
$this
->
setExpectedException
(
'yii\base\InvalidConfigException'
,
'The "range" property must be set.'
);
...
...
tests/unit/framework/validators/RegularExpressionValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -9,6 +9,12 @@ use yiiunit\TestCase;
class
RegularExpressionValidatorTest
extends
TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testValidateValue
()
{
$val
=
new
RegularExpressionValidator
([
'pattern'
=>
'/^[a-zA-Z0-9](\.)?([^\/]*)$/m'
]);
...
...
tests/unit/framework/validators/RequiredValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -8,6 +8,12 @@ use yiiunit\TestCase;
class
RequiredValidatorTest
extends
TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testValidateValueWithDefaults
()
{
$val
=
new
RequiredValidator
();
...
...
tests/unit/framework/validators/UniqueValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -18,6 +18,7 @@ class UniqueValidatorTest extends DatabaseTestCase
public
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
ActiveRecord
::
$db
=
$this
->
getConnection
();
}
...
...
tests/unit/framework/validators/UrlValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -9,6 +9,12 @@ use yiiunit\TestCase;
*/
class
UrlValidatorTest
extends
TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
public
function
testValidateValue
()
{
$val
=
new
UrlValidator
;
...
...
tests/unit/framework/validators/ValidatorTest.php
View file @
9f7ca5d2
...
...
@@ -12,6 +12,11 @@ use yiiunit\TestCase;
class
ValidatorTest
extends
TestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
$this
->
mockApplication
();
}
protected
function
getTestModel
(
$additionalAttributes
=
[])
{
...
...
@@ -220,7 +225,7 @@ class ValidatorTest extends TestCase
$errors
=
$m
->
getErrors
(
'attr_msg_val'
);
$this
->
assertEquals
(
'attr_msg_val::array()'
,
$errors
[
0
]);
$m
=
$this
->
getTestModel
([
'attr_msg_val'
=>
'abc'
]);
$val
->
addError
(
$m
,
'attr_msg_val'
,
'{attribute}::{value}::{param}'
,
[
'
{param}
'
=>
'param_value'
]);
$val
->
addError
(
$m
,
'attr_msg_val'
,
'{attribute}::{value}::{param}'
,
[
'
param
'
=>
'param_value'
]);
$errors
=
$m
->
getErrors
(
'attr_msg_val'
);
$this
->
assertEquals
(
'attr_msg_val::abc::param_value'
,
$errors
[
0
]);
}
...
...
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