本文整理汇总了PHP中yii\bootstrap\ActiveForm::validateMultiple方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveForm::validateMultiple方法的具体用法?PHP ActiveForm::validateMultiple怎么用?PHP ActiveForm::validateMultiple使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\bootstrap\ActiveForm
的用法示例。
在下文中一共展示了ActiveForm::validateMultiple方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
public function run()
{
Yii::setAlias('@unclead-examples', realpath(__DIR__ . '/../'));
$models = $this->getItems();
$request = Yii::$app->getRequest();
if ($request->isPost && $request->post('ajax') !== null) {
$data = Yii::$app->request->post('Item', []);
foreach (array_keys($data) as $index) {
$models[$index] = new Item();
}
Model::loadMultiple($models, Yii::$app->request->post());
Yii::$app->response->format = Response::FORMAT_JSON;
$result = ActiveForm::validateMultiple($models);
return $result;
}
if (Model::loadMultiple($models, Yii::$app->request->post())) {
// your magic
}
return $this->controller->render('@unclead-examples/views/tabular-input.php', ['models' => $models]);
}
示例2: actionUpdate
/**
* Updates an existing Tour model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelsCustomField = $model->customFields;
if ($model->load(Yii::$app->request->post())) {
$oldIDs = ArrayHelper::map($modelsCustomField, 'id', 'id');
$modelsCustomField = Model::createMultiple(CustomField::classname(), $modelsCustomField);
Model::loadMultiple($modelsCustomField, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsCustomField, 'id', 'id')));
// ajax validation
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(ActiveForm::validateMultiple($modelsCustomField), ActiveForm::validate($model));
}
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsCustomField) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
if (!empty($deletedIDs)) {
CustomField::deleteAll(['id' => $deletedIDs]);
}
foreach ($modelsCustomField as $modelCustomField) {
$modelCustomField->tour_id = $model->id;
if (!($flag = $modelCustomField->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}
return $this->render('update', ['model' => $model, 'modelsCustomField' => empty($modelsCustomField) ? [new CustomField()] : $modelsCustomField]);
}