相关疑难解决方法(0)

YII2:添加动态表单字段及其验证

我正在添加动态表单字段onChange of dropdown.两种类型的字段都来自不同的模型,并在不同的表中转到数据库.我已经在模型中定义了验证规则.

但验证工作不正常.我的代码如下:

型号:

<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%membership_features}}".
 *
 * @property integer $id
 * @property string $title
 * @property string $type
 * @property integer $is_new
 * @property integer $status
 * @property integer $is_deleted
 * @property string $created_date
 * @property string $modified_date
 *
 * @property MembershipFeaturesValue[] $membershipFeaturesValues
 */
class MembershipFeatures extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */

    public $value=[];
    public static function tableName()
    {
        return '{{%membership_features}}';
    }

    /**
     * …
Run Code Online (Sandbox Code Playgroud)

php dynamic yii2 yii2-advanced-app

11
推荐指数
2
解决办法
2万
查看次数

在 yii2 中使用自定义 ID 进行 Ajax 验证

我在 foreach 循环中有一个相同的字段,如下所示

foreach ( $subCategoryData as $k => $val) {
    <?= $form->field($model, 'sub_category', ['template' => '{input}'])->textInput(['maxlength' => 255, 'class' => 'form-control required section_name', 'name' => "Category[sub_category][$k][name]"]) ?>
} ?>
Run Code Online (Sandbox Code Playgroud)

我使用自定义方法进行 ajax 验证,它工作正常。

但它只使用第一个输入。因为它有相同的ID。

但是当我'inputOptions' => ['id' => 'myCustomId']用下面的方式改变它并使它独一无二时,我的ajax验证没有被调用。

foreach ( $subCategoryData as $k => $val) {
    <?= $form->field($model, 'sub_category', ['template' => '{input}','inputOptions' => ['id' => "category-sub_category_".$k]])->textInput(['maxlength' => 255, 'class' => 'form-control required section_name', 'name' => "Category[sub_category][$k][name]"]) ?>
}
Run Code Online (Sandbox Code Playgroud)

我在这里看到了这个解决方案 https://github.com/yiisoft/yii2/issues/7627

还看到了这个 /sf/answers/1992230971/

但是没有任何工作可以帮助我吗?

validation ajax yii2

5
推荐指数
1
解决办法
181
查看次数

标签 统计

yii2 ×2

ajax ×1

dynamic ×1

php ×1

validation ×1

yii2-advanced-app ×1