I have in my form a textarea
input field and I am using the HTML5 required
attribute:
<textarea class="form-control text-secondary mb-3" rows="4" name="audit_req_comment_decision" type="text" value="<?php echo htmlentities($audits['audit_req_comment_decision']); ?>" required></textarea>
<button type="submit" class="btn btn-danger" name="accepted">Accepted</button>
<button type="submit" class="btn btn-danger ml-auto" name="rejected">Rejected</button>
My form also has two buttons. One will send "accepted" and the other one is to send "rejected". I would like to change my form so that when the user clicks on the "Accepted" button it will check the required input is not empty. But if the "Rejected" button is clicked, the text area should not be required and the form will submit even if it is empty.
Is there any workaround available?
Method 1: Handle the requirement validation yourself
You need to remove the "required" attribute from the element - the browser implements that requirement but you want to handle it yourself.
When that is removed, you need you use javascript to handle the submission. You can add code to your "Accepted" button that checks if the text area has a value, and if not then prevent the form from being submitted.
To do this:
required
attribute from the textarea<textarea class="required-on-accept">
false
to prevent the form from being submittedonclick
to the button as follows: <button type="submit" onclick="return check_required();">
See a working example:
function check_required() {
/* check all elements with the class required-on-accept */
var elements = document.getElementsByClassName("required-on-accept");
for (var i = 0; i < elements.length; i++) {
/*if any element is empty, return false to prevent the form being submitted */
if (elements[i].value==""){
console.log(" required value not entered!");
return false;
}
}
return true;
}
<form id="my-form">
<textarea class="form-control text-secondary mb-3 required-on-accept" rows="4" name="audit_req_comment_decision" type="text" value="123"></textarea>
<button type="submit" onclick="return check_required();" class="btn btn-danger" name="accepted">Accepted</button>
<button type="submit" class="btn btn-danger ml-auto" name="rejected">Rejected</button>
</form>
Method 2: remove the required
attribute on clicking the "Rejected" button
If you still want the browser to handle the validation except for when the "Rejected" buton is pressed, you still need javascript, but you can do it like this instead:
<textarea class="required-on-accept">
required
attributeonclick
to the button as follows: <button type="submit" onclick="return check_not_required();">
The required
attribute will remain if the "Accepted" button is clicked, so the browser will still prevent the form from being submitted if that button is clicked.
See this working below -
function check_not_required() {
/* check all elements with the class required-on-accept */
var elements = document.getElementsByClassName("required-on-accept");
for (var i = 0; i < elements.length; i++) {
/* remove the "required" attribute from the element */
elements[i].removeAttribute("required");
}
/* Tell the browser to submit the form -
the required elements are no longer required,
so the browser will not stop the submission if they're empty */
return true;
}
<form id="my-form">
<textarea class="form-control text-secondary mb-3 required-on-accept" rows="4" name="audit_req_comment_decision" type="text" value="123" required></textarea>
<button type="submit" class="btn btn-danger" name="accepted">Accepted</button>
<button type="submit" onclick="return check_not_required();" class="btn btn-danger ml-auto" name="rejected">Rejected</button>
</form>