I made a survey form with html, php and yii2. I have used the jquery validation plugin to check whether each question has an answer. Now the validation works and an error message pops up when a question is not answered.
I have also added a "br" tag to change line so that the error message will not be in the same row of the radio buttons. Now when I select an answer the error message disappears, but the "br" tag still exists. So how do I remove the "br" tag tag as well?
The following are my codes:
<?php
foreach ($questions as $question) {
?>
<label class="question"><?php echo $question->getTitle(); ?></label><br>
<?php
foreach ($choices as $choice) {
?>
<label class="radio-inline"><input type="radio" name="<?php echo $question->id . 'radio'; ?>"
value="<?php echo $choice->id; ?>"><?php echo $choice->getTitle(); ?></label>
<?php }
$this->registerJs('
rule[ "' . $question->id . 'radio"] = {
required: true,
}
', View::POS_BEGIN);
?>
<?php } ?>
<?php
$this->registerJs('
$(document).ready(function(){
$("#testForm").validate({
rules: rule,
errorPlacement: function (error, element) {
if (element.attr("type") == "checkbox" || element.attr("type") == "radio") {
error.insertAfter($(element).parents("label").prev($(".question")));
$("<br>").insertAfter($(element).parents("label").prev($(".question")));
}
else{
error.insertAfter(element);
}
}
});
});
',
View::POS_END);
?>