Yii2 : validate if at least one checkbox is selected

只谈情不闲聊 提交于 2019-12-13 18:24:46

问题


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]) . "&nbsp;" . $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


回答1:


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');
    }
};


来源:https://stackoverflow.com/questions/41209265/yii2-validate-if-at-least-one-checkbox-is-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!