Пример #1
0
if (Yii::$app->request->post()) {
    $postData = Yii::$app->request->post();
}
foreach ($questionsModel as $model) {
    $value = $option_id = '';
    if ($answersModel = MembersQuestionsAnswers::findOne(['question_id' => $model->id, 'member_id' => $member_id])) {
        $value = $answersModel->value;
        $option_id = $answersModel->option_id;
    } else {
        if (!empty($postData['MembersQuestionsAnswers']["value_{$model->id}"])) {
            $value = $option_id = $postData['MembersQuestionsAnswers']["value_{$model->id}"];
        }
    }
    if ($model->fieldsTypes[0]->has_options) {
        //if question has options display a dropdown
        $optionsArray = ArrayHelper::map(MappingQuestionsToOptions::findAll(['question_id' => $model->id]), 'id', 'title');
        if ($model->fieldsTypes[0]->has_other_field && $model->has_other) {
            $optionsArray[-1] = Yii::t('frontend', 'Other');
        }
        echo $form->field($questionsAnswers, "value_{$model->id}")->dropDownList($optionsArray, ['prompt' => Yii::t('frontend', '-- Select --'), 'options' => [$option_id => ['Selected' => 'selected']]])->label($model->title);
    } else {
        // else if question does not have options display a text field
        echo $form->field($questionsAnswers, "value_{$model->id}")->textInput(['value' => $value]);
    }
    if ($model->fieldsTypes[0]->has_other_field && $model->has_other) {
        if ($answersModel) {
            $options = ['value' => $answersModel->other];
        }
        if ($option_id > 0) {
            $options = ['style' => 'display: none;'];
        } else {
Пример #2
0
<?php 
/* output all attributes related tot the mapping category */
$member_id = Yii::$app->getUser()->id;
?>

<?php 
/* output all questions related tot the mapping category */
foreach ($questionsModel as $model) {
    $value = $option_id = '';
    if ($answersModel = MembersQuestionsAnswers::findOne(['question_id' => $model->id, 'member_id' => $member_id])) {
        $value = $answersModel->value;
        $option_id = $answersModel->option_id;
    }
    if ($model->fieldsTypes[0]->has_options) {
        //if question has options display a dropdown
        echo $form->field($questionsAnswers, "value_{$model->id}")->dropDownList(ArrayHelper::map(MappingQuestionsToOptions::findAll(['question_id' => $model->id]), 'id', 'title'), ['prompt' => Yii::t('frontend', '-- Select --'), 'options' => [$option_id => ['Selected' => 'selected']]])->label($model->title);
        //$this->registerJs("$('form').yiiActiveForm('updateMessages', {
        //'MembersQuestionsAnswers[32][value]': ['I don\'t like it!']
        //}, true);");
    } else {
        // else if question does not have options display a text field
        echo $form->field($questionsAnswers, "value_{$model->id}")->textInput(['value' => $value]);
    }
}
?>

    <div class="form-group">
        <?php 
echo Html::submitButton(Yii::t('frontend', 'Save'), ['class' => 'btn btn-success']);
?>
    </div>