Yii2在一个动作中有多种形式

时间:2015-02-17 17:47:08

标签: forms yii2

在单个操作中处理多个表单的正确方法是什么?

这是我的模特/ MembersBans.php

<?php

namespace app\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use app\models\Members;

class MembersBans extends \yii\db\ActiveRecord {

public $username;

public static function tableName() {
    return '{{%members_bans}}';
}

public static function primaryKey() {
    return array('ban_id');
}

public function behaviors() {
    return [
        [
            'class' => TimestampBehavior::className(),
            'createdAtAttribute' => 'date_added',
            'updatedAtAttribute' => 'last_updated',
        ],
    ];
}

public function rules() {
    return [
        [['ban_id', 'ban_memberid', 'date_added', 'last_updated'], 'integer'],
        [['username', 'end_date'], 'safe'],
        ['end_date', 'date', 'format' => 'yyyy-mm-dd'],
        [['ban_ip'], 'string', 'max' => 40],
        [['reason'], 'string', 'max' => 255]
    ];
}

public function attributeLabels() {
    return [
        'ban_id' => Yii::t('app', 'ID на бана'),
        'ban_memberid' => Yii::t('app', 'Потребителско ID'),
        'username' => 'Потребителско име',
        'ban_ip' => Yii::t('app', 'IP адрес'),
        'end_date' => Yii::t('app', 'Дата на изтичане'),
        'reason' => Yii::t('app', 'Причина за бана'),
        'date_added' => Yii::t('app', 'Дата на добавяне'),
    ];
}

public function getMemberBans() {
    $bans = $this->find()->where('ban_memberid');
    return $bans;
}

public function getIpBans() {
    $bans = $this->find()->where('ban_ip');
    return $bans;
}

public function getMember() {
    return $this->hasOne(Members::className(), ['member_id' => 'ban_memberid']);
}

public function banMember() {
    $memberInfo = Members::findByUsername($this->username);
    if ($memberInfo) {
        $this->ban_memberid = $memberInfo->member_id;
        if ($this->save()) {
            Yii::$app->session->setFlash('alert-success', 'Потребителят беше успешно блокиран.');
        } else {
            Yii::$app->session->setFlash('alert-error', 'Възникна грешка при блокирането на потребителя.');
        }
    } else {
        Yii::$app->session->setFlash('alert-error', 'Не съществува потребител с това потребителско име.');
    }
}

public function banIp() {
    if ($this->save()) {
        Yii::$app->session->setFlash('alert-success', 'IP адресът беше успешно блокиран.');
    } else {
        Yii::$app->session->setFlash('alert-error', 'Възникна грешка при блокирането на IP адреса.');
    }
}

}

我的控制器/ MembersBansController.php:

public function actionList() {
    $membersBans = new MembersBans();
    if ($membersBans->load(Yii::$app->request->post('banMember'))) {
        $membersBans->banMember();
    }
    if ($membersBans->load(Yii::$app->request->post('banIp'))) {
        $membersBans->banIp();
    }
    return $this->render('list', [
                'membersBans' => $membersBans,
    ]);
}

视图/构件-禁止/列表:

<div class="the-box">
    <?php
    $activeForm = ActiveForm::begin([
        'id'                     => 'banMember',
        'enableClientValidation' => true,
        'enableAjaxValidation'   => true,
        'validateOnSubmit'       => true,
        'validateOnChange'       => true,
        'validateOnType'         => true,
    ])
    ?>
    <?= $activeForm->field($membersBans, 'username', [
        'template' => '{label}{input}{hint}{error}'
    ])->textInput(['class' => 'form-control', 'placeholder' => 'Въведете потребителско име']);
    ?>
    <?= $activeForm->field($membersBans, 'end_date', [
        'template' => '{label}{input}{hint}{error}'
    ])->textInput(['class' => 'form-control datepicker', 'placeholder' => 'Въведете период на бана']);
    ?>
    <?= $activeForm->field($membersBans, 'reason', [
        'template' => '{label}{input}{hint}{error}'
    ])->textInput(['class' => 'form-control', 'placeholder' => 'Въведете причина за бана']);
    ?>
    <div class="form-group">
        <?= Html::submitButton('Добави', ['type' => 'submit', 'class' => 'btn btn-default']) ?>
    </div>
    <?php ActiveForm::end()?>
</div>


<div class="the-box">
    <?php
    $activeForm = ActiveForm::begin([
        'id'                     => 'banIp',
        'enableClientValidation' => true,
        'enableAjaxValidation'   => true,
        'validateOnSubmit'       => true,
        'validateOnChange'       => true,
        'validateOnType'         => true,
    ])
    ?>
    <?= $activeForm->field($membersBans, 'ban_ip', [
        'template' => '{label}{input}{hint}{error}'
    ])->textInput(['class' => 'form-control', 'placeholder' => 'Въведете IP адрес или цяла мрежа']);
    ?>
    <?= $activeForm->field($membersBans, 'end_date', [
        'template' => '{label}{input}{hint}{error}'
    ])->textInput(['class' => 'form-control datepicker', 'placeholder' => 'Въведете период на бана']);
    ?>
    <?= $activeForm->field($membersBans, 'reason', [
        'template' => '{label}{input}{hint}{error}'
    ])->textInput(['class' => 'form-control', 'placeholder' => 'Въведете причина за бана']);
    ?>
    <div class="form-group">
        <?= Html::submitButton('Добави', ['type' => 'submit', 'class' => 'btn btn-default']) ?>
    </div>
    <?php ActiveForm::end() ?>
</div>

它似乎不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:8)

ActiveForm中的ID并不代表$ _POST中的ID。你应该使用:

$membersBans->load(Yii::$app->request->post())

$membersBans->load(Yii::$app->request->post('MembersBans'))

表单中的加载属性。

例如来自CeBe的多个表单(http://www.yiiframework.com/forum/index.php/topic/53935-solved-subforms/page__p__248184#entry248184

public function actionCreate()
{   
    $user = new User;
    $profile = new Profile;

    if ($user->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post()) && Model::validateMultiple([$user, $profile])) {

        $user->save(false); // skip validation as model is already validated
        $profile->user_id = $user->id; // no need for validation rule on user_id as you set it yourself
        $profile-save(false); 

        return $this->redirect(['view', 'id' => $user->id]);
    } else {
        return $this->render('create', [
            'user' => $user,
            'profile' => $profile,
        ]);
    }
}

action中,您使用一个model。我认为你应该将MembersBans扩展到MembersBansIp课程。还有你的action

public function actionList() {
    $membersBans = new MembersBans();
    $membersBansIp = new MembersBansIp();
    if ($membersBans->load(Yii::$app->request->post())) {
        $membersBans->banMember();
    }
    if ($membersBansIp->load(Yii::$app->request->post())) {
        $membersBansIp->banIp();
    }
    return $this->render('list', [
                'membersBans' => $membersBans,
                'membersBansIp' => $membersBansIp,
    ]);
}

view

<?php
    $activeForm = ActiveForm::begin([
            'id' => 'banMember',
])
?>

    <?= $activeForm->field($membersBans, 'fieldMembersBans') ?>
    <?= Html::submitButton('Login', ['class' => 'btn btn-primary']) ?>
    <?php ActiveForm::end() ?>
<?php
    $activeForm = ActiveForm::begin([
            'id' => 'banMemberIp',
])
?>

    <?= $activeForm->field($membersBansIp, 'usernameMembersBansIp') ?>
    <?= Html::submitButton('Login', ['class' => 'btn btn-primary']) ?>
    <?php ActiveForm::end() ?>

修改

我把你的代码。他在工作。 https://yadi.sk/i/y7PkwGUPekjPD https://yadi.sk/i/h8dCYQz3ekk2B

但是我将控制器更改为

$membersBans = new MembersBans();
        if ($membersBans->load(Yii::$app->request->post())) {
            $membersBans->banMember();
        }
        if ($membersBans->load(Yii::$app->request->post())) {
            $membersBans->banIp();
        }

并且modelMembers更改为User,因为我没有Members个对象:

$memberInfo = User::findByUsername($this->username);
        if ($memberInfo) {
            $this->ban_memberid = $memberInfo->id;

简历:您的代码已经完成。更改controller,我的写作方式。