Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
I have added a few custom input fields to certain product pages by creating a custom product template (copying product.json) and a custom product section (copying main-product.liquid). For example:
<input type="text" id="custom-input-" name="properties[Custom Input]" class="field__input" form="{{ product_form_id }}" required>
This is working, and I've added some custom JavaScript for client-side validation for the custom input fields.
Is it possible to add server-side validation for the custom fields as well? Server-side validation will help ensure data integrity as a fallback in case the client-side validation fails or is circumvented (for example, JS is disabled). I do not want to allow products to be added to the cart if the custom fields are empty or do not have a valid value. Ideally I want to block this on the product page when the user clicks the "Add to cart" button and display an error message.
At a bare minimum, I want to validate a field isn't empty. Ideally I'd like to validate the value is numeric, or a certain length, or a valid date/time, etc.
Is this possible to implement without using Checkout UI Extensions? I am not a Shopify Plus merchant.
Solved! Go to the solution
This is an accepted solution.
In short no, shopify does not execute arbitrary code you may implement, it's not like wordpress server etc.
In general: Any "validation" sanitation for form submissions is already handled by shopify.
Beyond that you'd have to implement custom checks in liquid for themes.
Which can be an exercise in futility as you don't not actually have any control over the form.error properties coming from shopify's server and liquid is a templating language that does not have things like regex for parsing strings.
If you want to reject orders , without checkout extensions, then you need to do it AFTER the order is placed as a backend process using an app, such as a general automation app like shopify-flow, usemechanic app, or more specific order handling apps on the app store.
About the only workaround do to pseudo validation in the checkout is trying to hack it using the carrier-service api, or setting high prices with discounts that require certain cart.attributes to bring the price down etc.
Or sidestep cart to checkout with draft orders https://tasks.mechanic.dev/create-a-draft-order-from-the-cart
Contact paull.newton+shopifyforum@gmail.com for the solutions you need
Save time & money ,Ask Questions The Smart Way
Problem Solved? ✔Accept and Like solutions to help future merchants
Answers powered by coffee Thank Paul with a ☕ Coffee for more answers or donate to eff.org
This is an accepted solution.
In short no, shopify does not execute arbitrary code you may implement, it's not like wordpress server etc.
In general: Any "validation" sanitation for form submissions is already handled by shopify.
Beyond that you'd have to implement custom checks in liquid for themes.
Which can be an exercise in futility as you don't not actually have any control over the form.error properties coming from shopify's server and liquid is a templating language that does not have things like regex for parsing strings.
If you want to reject orders , without checkout extensions, then you need to do it AFTER the order is placed as a backend process using an app, such as a general automation app like shopify-flow, usemechanic app, or more specific order handling apps on the app store.
About the only workaround do to pseudo validation in the checkout is trying to hack it using the carrier-service api, or setting high prices with discounts that require certain cart.attributes to bring the price down etc.
Or sidestep cart to checkout with draft orders https://tasks.mechanic.dev/create-a-draft-order-from-the-cart
Contact paull.newton+shopifyforum@gmail.com for the solutions you need
Save time & money ,Ask Questions The Smart Way
Problem Solved? ✔Accept and Like solutions to help future merchants
Answers powered by coffee Thank Paul with a ☕ Coffee for more answers or donate to eff.org
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024