我有我的Yii2項目中的3個相關表(模型)。Yii2:動態表單部件wbraganca不是
- 用戶具有
| ID (primary key) | username |
- PHONETYPE具有
| ID (primary key) | typename |
- Phonenumber來具有
| ID | user_ID (foreign key) | type_ID (foreign key) | phonenumbers |
我在我的Yii2應用程序中集成了「wbraganca Yii2動態表單」,但是當我點擊「創建」按鈕時,什麼都沒有發生,只是空白頁。但是,當我切入分貝我得到的用戶名是創建沒有數字。
請不要將其標記爲重複項,因爲直到現在我還沒有找到解決此問題的方法。 在此先感謝。下面
<div class="user-form">
<?php $form = ActiveForm::begin(['id'=>'dynamic-form']); ?>
<?= $form->field($model, 'username')->textInput(['maxlength' => true]) ?>
<!-- Creating Phone numbers for user -->
<div class="row">
<div class="panel panel-default">
<div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Телефонные номера </h4></div>
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 5, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelsPhone[0],
'formId' => 'dynamic-form',
'formFields' => [
'phonenumbers',
],
]); ?>
<div class="container-items"><!-- widgetContainer -->
<?php foreach ($modelsPhone as $i => $modelPhone): ?>
<div class="item panel panel-default"><!-- widgetBody -->
<div class="panel-heading">
<h3 class="panel-title pull-left">Телефонный номер </h3>
<div class="pull-right">
<button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
<button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<?php
// necessary for update action.
if (! $modelPhone->isNewRecord) {
echo Html::activeHiddenInput($modelPhone, "[{$i}]id");
}
?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($modelPhone, "[{$i}]type_id")->dropDownList(
ArrayHelper::map(Phonetype::find()->all(), 'id','typename'),
['prompt'=>'Выберите категорию ...']
) ?>
</div>
<div class="col-sm-6">
<?= $form->field($modelPhone, "[{$i}]phonenumbers")->textInput(['maxlength' => true]) ?>
</div>
</div><!-- .row -->
</div>
</div>
<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Создать') : Yii::t('app', 'Редактировать'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
這裏
這裏是我的UserController的代碼
public function actionCreate()
{
$model = new User();
$modelsPhone = [new Phonenumber];
if ($model->load(Yii::$app->request->post())) {
if($model->save()):
$modelsPhone = Model::createMultiple(Phonenumber::classname());
Model::loadMultiple($modelsPhone, Yii::$app->request->post());
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsPhone) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelsPhone as $modelPhone) {
$modelPhone->user_id = $model->id;
if (! ($flag = $modelPhone->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
endif;
} else {
return $this->render('create', [
'model' => $model,
'modelsPhone' => (empty($modelsPhone)) ? [new Phonenumber] : $modelsPhone
]);
}
}
/**
* Updates an existing User 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);
$modelsPhone = $model->phonenumbers;
if ($model->load(Yii::$app->request->post())) {
$oldIDs = ArrayHelper::map($modelsPhone, 'id', 'id');
$modelsPhone = Model::createMultiple(Phonenumber::classname(), $modelsPhone);
Model::loadMultiple($modelsPhone, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsPhone, 'id', 'id')));
$valid = $model->validate();
$valid = Model::validateMultiple($modelsPhone) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
if (! empty($deletedIDs)) {
Phonenumber::deleteAll(['id' => $deletedIDs]);
}
foreach ($modelsPhone as $modelPhone) {
$modelPhone->user_id = $model->id;
if (! ($flag = $modelPhone->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,
'modelsPhone' => (empty($modelsPhone)) ? [new Phonenumber] : $modelsPhone
]);
}
/**
* Deletes an existing User model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the User model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return User the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = User::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
protected function findModelPhones($id)
{
if (($model = Phonenumber::find()->where(['user_id'=>$id])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
_form.php這個代碼是我create.php代碼
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model app\models\User */
$this->title = Yii::t('app', 'Создать контакт');
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Контакты'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="user-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
'modelsPhone'=>$modelsPhone,
]) ?>
Here you can see that i filled all fields.Then when im clicking button, nothing happened
請避免這樣的問候和其他foofaraws。問題應儘可能純粹,以便清楚,並且易於諮詢。 –
讓我們來檢查你的controller.for的第一部分,請檢查$ valid value.is它是真是假? – yafater
謝謝,夥計。我只是試圖修復我的規則驗證並解決了我的問題。現在一切都運轉良好。 – Miracle633