";echo $v->song->name;echo "</div>";echo "</div>";}}}echo "</fieldset>";
我在模型中的规则
['selected_releases', 'required', 'on' =>['catalog', 'catalog_update'], 'requiredValue' =>1、'当' =>function ($model) {return ($model->scenario == self::SCENARIO_CATALOG_BASED || $model->scenario == self::SCENARIO_CATALOG_BASED_UPDATE);},'whenClient' =>'function(attribute,value){return ("' . $this->scenario . '"=="' . self::SCENARIO_CATALOG_BASED .'" || "' . $this->scenario .'"=="' . self::SCENARIO_CATALOG_BASED_UPDATE .'")}','消息' =>'您必须至少选择一个版本',]
当我提交表单时,发布的输入变量如下所示
[selected_releases] =>大批([0] =>0[1] =>0)
规则中的 requiredValue
不起作用,因为 selected_releases
是一个值数组,它总是说
您必须至少选择一个版本
我如何将 requiredValue
参数选项用于规则,以检查至少一个复选框
或
我是否必须在验证时调用自定义验证方法
解决方案
我为此使用了自定义验证方法,我的验证规则如下
['selected_releases', 'checkreleaseSelection', '开' =>['目录', 'catalog_update'],'当' =>function ($model) {return ($model->scenario == self::SCENARIO_CATALOG_BASED || $model->scenario == self::SCENARIO_CATALOG_BASED_UPDATE);},'whenClient' =>'function(attribute,value){return ("' . $this->scenario . '"=="' . self::SCENARIO_CATALOG_BASED .'" || "' . $this->scenario .'"=="' . self::SCENARIO_CATALOG_BASED_UPDATE .'")}',]
自定义方法
公共函数checkreleaseSelection(){$错误=真;foreach ($this->selected_releases as $selection) {如果($选择){$错误=假;}}如果($错误){$this->addError('selected_releases', '您必须至少选择一个版本才能继续');}};
I have multiple Music releases for a user for which he wants to create a promo, on the promo create form we have all the releases listed and I am using a form model for creating the promo where I define all rules for my several inputs in the promo form model.
I want to check if at-least one release is selected by the user when saving the form but it is not working as the checkboxes are created dynamically.
my form code for that field
foreach ($releaseInfo as $releases) {
if (!is_null($releases->releaseSongs)) {
echo "<fieldset><legend>" . \yii\helpers\Html::activeCheckbox($model, 'selected_releases[]', ['id' => 'release_' . $releases->id, 'onclick' => '$("#songs_' . $releases->id . '").toggle()', 'label' => false]) . " " . $releases->name . "</legend>";
foreach ($releases->releaseSongs as $k => $v) {
echo "<div id='songs_" . $releases->id . "' style='display:none'>";
echo "<div>";
echo $v->song->name;
echo "</div>";
echo "</div>";
}
}
}
echo "</fieldset>";
my rule in model
['selected_releases', 'required', 'on' => ['catalog', 'catalog_update'], 'requiredValue' => 1,
'when' => function ($model) {return ($model->scenario == self::SCENARIO_CATALOG_BASED || $model->scenario == self::SCENARIO_CATALOG_BASED_UPDATE);},
'whenClient' => 'function(attribute,value){return ("' . $this->scenario . '"=="' . self::SCENARIO_CATALOG_BASED . '" || "' . $this->scenario . '"=="' . self::SCENARIO_CATALOG_BASED_UPDATE . '")}',
'message' => 'You must select atleast one release',
]
when i submit my form the posted input variable looks like this
[selected_releases] => Array(
[0] => 0
[1] => 0
)
the requiredValue
in the rule does not work because the selected_releases
is an array of values it always says that
You must select atleast one release
how shoule i use the requiredValue
parameter option for rules in a way that it checks for atleast one slection of the checkbox
OR
do i have to make a custom validation method an call it when validating
解决方案
i used custom validation method for this purpose , my validation rule looks like following
['selected_releases', 'checkreleaseSelection'
, 'on' => ['catalog', 'catalog_update'],
'when' => function ($model) {return ($model->scenario == self::SCENARIO_CATALOG_BASED || $model->scenario == self::SCENARIO_CATALOG_BASED_UPDATE);},
'whenClient' => 'function(attribute,value){return ("' . $this->scenario . '"=="' . self::SCENARIO_CATALOG_BASED . '" || "' . $this->scenario . '"=="' . self::SCENARIO_CATALOG_BASED_UPDATE . '")}',
]
Custom method
public function checkreleaseSelection()
{
$error = true;
foreach ($this->selected_releases as $selection) {
if ($selection) {
$error = false;
}
}
if ($error) {
$this->addError('selected_releases', 'You must select atleast one of your releases to continue');
}
};
这篇关于Yii2 : 验证是否至少选择了一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!