In my advanced template project I have an ActiveForm which contains a RadioList field.
<?php $form = ActiveForm::begin(['id' => 'form-medical-questionnaire']); ?>
<?= $form->field($model, 'heart_cond_stroke')->radioList([1 => 'Yes', 0 => 'No'])
->label('Has your doctor ever told you that you have a heart condition or suffered a stroke?'); ?>
In my test suite I have the following test:
public function medicalWithoutHealthIssues(FunctionalTester $I)
{
$user = \common\models\User::findByEmail('medical.incomplete@gmail.com');
$I->amLoggedInAs($user);
$I->amOnRoute('user/medical/' . $user->id);
$I->selectOption(['name' => 'MedicalQuestionnaire[heart_cond_stroke]'], 0);
$I->click('submitButton');
$I->see('Great! You\'re free to use the system.', '.alert-success');
}
When I run the test I get an error "A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is hidden)" This is because a hidden field with the name MedicalQuestionnaire[heart_cond_stroke] is generated to capture unselected values for the purposes of validation.
I can fix the failing test by changing the RadioList to:
{...}->radioList([1 => 'Yes', 0 => 'No'], ['unselect' => null])
This stops unselected values from being tracked and therefore removes the hidden input which means my test now passes, but as a result my form is no longer protected against incomplete fields which is a bigger problem than the failing test!
Can anybody tell me what is the best-practice solution for this issue?