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
2a71c72d
Commit
2a71c72d
authored
Oct 24, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixes #1066: Added ActiveForm::validate() and validateMultiple()
parent
f63720c6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
96 additions
and
2 deletions
+96
-2
Model.php
framework/yii/base/Model.php
+7
-2
ActiveForm.php
framework/yii/widgets/ActiveForm.php
+89
-0
No files found.
framework/yii/base/Model.php
View file @
2a71c72d
...
...
@@ -748,16 +748,21 @@ class Model extends Component implements IteratorAggregate, ArrayAccess
/**
* Validates multiple models.
* This method will validate every model. The models being validated may
* be of the same or different types.
* @param array $models the models to be validated
* @param array $attributes list of attributes that should be validated.
* If this parameter is empty, it means any attribute listed in the applicable
* validation rules should be validated.
* @return boolean whether all models are valid. False will be returned if one
* or multiple models have validation error.
*/
public
static
function
validateMultiple
(
$models
)
public
static
function
validateMultiple
(
$models
,
$attributes
=
null
)
{
$valid
=
true
;
/** @var Model $model */
foreach
(
$models
as
$model
)
{
$valid
=
$model
->
validate
()
&&
$valid
;
$valid
=
$model
->
validate
(
$attributes
)
&&
$valid
;
}
return
$valid
;
}
...
...
framework/yii/widgets/ActiveForm.php
View file @
2a71c72d
...
...
@@ -265,4 +265,93 @@ class ActiveForm extends Widget
'form'
=>
$this
,
]));
}
/**
* Validates one or several models and returns an error message array indexed by the attribute IDs.
* This is a helper method that simplifies the way of writing AJAX validation code.
*
* For example, you may use the following code in a controller action to respond
* to an AJAX validation request:
*
* ~~~
* $model = new Post;
* $model->load($_POST);
* if (Yii::$app->request->isAjax) {
* Yii::$app->response->format = Response::FORMAT_JSON;
* return ActiveForm::validate($model);
* }
* // ... respond to non-AJAX request ...
* ~~~
*
* To validate multiple models, simply pass each model as a parameter to this method, like
* the following:
*
* ~~~
* ActiveForm::validate($model1, $model2, ...);
* ~~~
*
* @param Model $model the model to be validated
* @param mixed $attributes list of attributes that should be validated.
* If this parameter is empty, it means any attribute listed in the applicable
* validation rules should be validated.
*
* When this method is used to validate multiple models, this parameter will be interpreted
* as a model.
*
* @return array the error message array indexed by the attribute IDs.
*/
public
static
function
validate
(
$model
,
$attributes
=
null
)
{
$result
=
[];
if
(
$attributes
instanceof
Model
)
{
// validating multiple models
$models
=
func_get_args
();
$attributes
=
null
;
}
else
{
$models
=
[
$model
];
}
/** @var Model $model */
foreach
(
$models
as
$model
)
{
$model
->
validate
(
$attributes
);
foreach
(
$model
->
getErrors
()
as
$attribute
=>
$errors
)
{
$result
[
Html
::
getInputId
(
$model
,
$attribute
)]
=
$errors
;
}
}
return
$result
;
}
/**
* Validates an array of model instances and returns an error message array indexed by the attribute IDs.
* This is a helper method that simplifies the way of writing AJAX validation code for tabular input.
*
* For example, you may use the following code in a controller action to respond
* to an AJAX validation request:
*
* ~~~
* // ... load $models ...
* if (Yii::$app->request->isAjax) {
* Yii::$app->response->format = Response::FORMAT_JSON;
* return ActiveForm::validateMultiple($models);
* }
* // ... respond to non-AJAX request ...
* ~~~
*
* @param array $models an array of models to be validated.
* @param mixed $attributes list of attributes that should be validated.
* If this parameter is empty, it means any attribute listed in the applicable
* validation rules should be validated.
* @return array the error message array indexed by the attribute IDs.
*/
public
static
function
validateMultiple
(
$models
,
$attributes
=
null
)
{
$result
=
[];
/** @var Model $model */
foreach
(
$models
as
$i
=>
$model
)
{
$model
->
validate
(
$attributes
);
foreach
(
$model
->
getErrors
()
as
$attribute
=>
$errors
)
{
$result
[
Html
::
getInputId
(
$model
,
"[
$i
]"
.
$attribute
)]
=
$errors
;
}
}
return
$result
;
}
}
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