I have a small form with a select input and a textarea for commenting. I want to make the textarea required only if the value of the select input is 1, for example, otherwise, it shouldn’t be required. How can I accomplish this, is there a way to get the value of the select input once the user has clicked on one of the options ? Here’s my CommentForm.php :
$this->add(new Select(
'active',
[
1 => 'Something',
0 => 'Something else'
],
));
$comment = new TextArea('comment', [
'placeholder' => 'comment ...'
]);
$comment->addValidators([
new PresenceOf([
'message' => 'Comment is required'
])
]);
$this->add($comment);
This is how I render it in the volt file :
{{ form() }}
<div class="modal-body">
<div class="form-group">
<label>Select</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-comment-alt"></i></span>
</div>
{{ formComment.render("active", ["class": "form-control"]) }}
</div>
</div>
<div class="form-group">
<label>Comment</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-comment-alt"></i></span>
</div>
{{ formComment.render("comment", ["class": "form-control"]) }}
</div>
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="typeID" value="13">
{{ submit_button("addComment", 'name':'addComment', "class": "btn btn-success", 'value':'submit') }}
{{ formComment.render('csrf', ['value': security.getToken()]) }}
</div>
{{ end_form() }}
Thanks in advance!