How can I make the shopify theme text field as a required field ?
my liquid file :
{% schema %}
{
"name": "Badges & Icons",
"target": "section",
"settings": [
{"type":"text","label":"Badge ID","id":"badge-id"}
],
"stylesheet" : "animation.css"
}
{% endschema %}
1 Like
@weebySagar you can do with JavaScript like this try code
{% schema %}
{
"name": "Badges & Icons",
"target": "section",
"settings": [
{
"type": "text",
"label": "Badge ID",
"id": "badge-id",
"default": "",
"info": "Please enter a Badge ID. This field is required."
}
],
"stylesheet": "animation.css"
}
{% endschema %}
Script
document.addEventListener("DOMContentLoaded", function() {
const form = document.querySelector("form"); // Replace with the actual form selector if needed
const badgeIdField = document.querySelector("#badge-id"); // Use the ID from the schema
if (form && badgeIdField) {
form.addEventListener("submit", function(event) {
if (badgeIdField.value.trim() === "") {
alert("Please enter a Badge ID. This field is required.");
event.preventDefault(); // Prevent form submission
}
});
}
});
add above script bottom of the your js file
Hello @weebySagar
You can’t make this field required in the customize area. However, you can render the desired element only if this text field has value.
To do this, just go to the element you are rendering and wrap it in an if-statement:
{% if section.settings.badge-id != blank %}
Your section code here
{% endif %}
This will show the section/element only if the text field has value in it.
I hope this is helpful.