[Skip to main content](#vessel-content)

#   How to create a custom validation for custom fields

##

- **[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")**
- [![pjdevries's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/64503-pjdevries "View pjdevries's Profile") Topic Author
- [ Offline](/forum/user/64503-pjdevries)

Less More

- Posts: 29
- Thank you received: 2
-
-

4 years 8 months ago [\#339494](#339494) by [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")  *[How to create a custom validation for custom fields](/forum/25-install-update/903983-how-to-create-a-custom-validation-for-custom-fields#339494 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.x -- PHP version -- : 8.x  Is it possible to create custom validations for custom fields and if so, how do I do that? I also need to know how to show custom notifications and error messages, both from within JavaScript and PHP code.")* was created by *[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")*

-- HikaShop version -- : 4.4.5
-- Joomla version -- : 4.x
-- PHP version -- : 8.x

Is it possible to create custom validations for custom fields and if so, how do I do that? I also need to know how to show custom notifications and error messages, both from within JavaScript and PHP code.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2luc3RhbGwtdXBkYXRlLzkwMzk4My1ob3ctdG8tY3JlYXRlLWEtY3VzdG9tLXZhbGlkYXRpb24tZm9yLWN1c3RvbS1maWVsZHM/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86358
- Thank you received: 14290
-
-

4 years 8 months ago [\#339498](#339498) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[How to create a custom validation for custom fields](/forum/25-install-update/903983-how-to-create-a-custom-validation-for-custom-fields#339498 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.x -- PHP version -- : 8.x  Is it possible to create custom validations for custom fields and if so, how do I do that? I also need to know how to show custom notifications and error messages, both from within JavaScript and PHP code.")*

Hi,

It sure is possible.
The simplest would be to use the "regular expression" setting of the custom field to set a regex check. That way, the mechanism is handled by HikaShop and you don't need to do any coding.
If you need something more involved, it will require some coding.
However, I can't give say exactly how as it depends on how you configured the custom field.
Could you provide a screenshot of the settings of the custom field ?

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2luc3RhbGwtdXBkYXRlLzkwMzk4My1ob3ctdG8tY3JlYXRlLWEtY3VzdG9tLXZhbGlkYXRpb24tZm9yLWN1c3RvbS1maWVsZHM/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")**
- [![pjdevries's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/64503-pjdevries "View pjdevries's Profile") Topic Author
- [ Offline](/forum/user/64503-pjdevries)

Less More

- Posts: 29
- Thank you received: 2
-
-

4 years 8 months ago [\#339505](#339505) by [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")  Replied by *[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")* on topic *[How to create a custom validation for custom fields](/forum/25-install-update/903983-how-to-create-a-custom-validation-for-custom-fields#339505 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.x -- PHP version -- : 8.x  Is it possible to create custom validations for custom fields and if so, how do I do that? I also need to know how to show custom notifications and error messages, both from within JavaScript and PHP code.")*

Thanx for the swift response.

> If you need something more involved, it will require some coding.

Yes I definitly need something more involved and coding is not a problem at all. Neither PHP nor JavaScript.

> However, I can't give say exactly how as it depends on how you configured the custom field.
> Could you provide a screenshot of the settings of the custom field ?

I provided a screenshot, but currently no real attention has been payed to specific settings yet. I hope you can provide me with general, conceptual information on how to approach this, so I can use it for future custom fields as well, not bound to specific settings ![:)](/media/kunena/emoticons/3.png ":)")

- [ ![Image19.png](/forum/attachment/54375) ](/forum/attachment/54375 "Image19.png") [ ](/forum/attachment/54375?download=1 "Image19.png")

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2luc3RhbGwtdXBkYXRlLzkwMzk4My1ob3ctdG8tY3JlYXRlLWEtY3VzdG9tLXZhbGlkYXRpb24tZm9yLWN1c3RvbS1maWVsZHM/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86358
- Thank you received: 14290
-
-

4 years 8 months ago [\#339508](#339508) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[How to create a custom validation for custom fields](/forum/25-install-update/903983-how-to-create-a-custom-validation-for-custom-fields#339508 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.x -- PHP version -- : 8.x  Is it possible to create custom validations for custom fields and if so, how do I do that? I also need to know how to show custom notifications and error messages, both from within JavaScript and PHP code.")*

Hi,

Ok, so it is a custom field of the table "item", which is displayed on the product details page on the frontend.

For that kind of custom field, the simplest is to do the check only in javascript. In that case, you can just create a view override of show_default via the menu Display&gt;Views and add a bit of javascript to the page like so: Code:

&lt;script&gt; document.getElementById('xxx').addEventListener('change', (event) =&gt; { // do your check on event.target.value , you can modify event.target.value and use alert() to display a message }); &lt;/script&gt;

where xxx is the column name of the custom field (in this case it would be "width").

In PHP, it gets a bit more complex. You need to create a Joomla plugin of the group "hikashop" and implement the event:
onBeforeProductQuantityCheck(&amp;$products, &amp;$cart, &amp;$options)
where $products contains the data of the products being added to the cart, $cart contains the data already in the cart, a $options some options you don't care about.
The code will be something like that: Code:

function onBeforeProductQuantityCheck(&amp;$products, &amp;$cart, &amp;$options) { if($this-&gt;checkFunction($products) === false) { $products = array(); // remove the products data being added to the cart so that the add to cart fails $cartClass = hikashop_get('class.cart'); $cartClass-&gt;addMessage($cart, array('msg' =&gt; 'the product with the id $id could not be added to the cart because the check failed', 'product_id' =&gt; $id, 'type' =&gt; 'error')); } } function checkFunction($products) { // do the check return true; }

If you don't know how to create Joomla plugins, we have some information on this at the beginning of our developer documentation page:
[ www.hikashop.com/support/documentation/6...r-documentation.html](https://www.hikashop.com/support/documentation/62-hikashop-developer-documentation.html)

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2luc3RhbGwtdXBkYXRlLzkwMzk4My1ob3ctdG8tY3JlYXRlLWEtY3VzdG9tLXZhbGlkYXRpb24tZm9yLWN1c3RvbS1maWVsZHM/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")**
- [![pjdevries's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/64503-pjdevries "View pjdevries's Profile") Topic Author
- [ Offline](/forum/user/64503-pjdevries)

Less More

- Posts: 29
- Thank you received: 2
-
-

4 years 8 months ago [\#339517](#339517) by [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")  Replied by *[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")* on topic *[How to create a custom validation for custom fields](/forum/25-install-update/903983-how-to-create-a-custom-validation-for-custom-fields#339517 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.x -- PHP version -- : 8.x  Is it possible to create custom validations for custom fields and if so, how do I do that? I also need to know how to show custom notifications and error messages, both from within JavaScript and PHP code.")*

Hi,

That's very helpful and looks quite useful.

> For that kind of custom field, the simplest is to do the check only in javascript.

I agree. However, client side validation is user friendly but not safe. So I will implement both, as one always should ![:)](/media/kunena/emoticons/3.png ":)")

> ...and use alert() to display a message

Alert is possible but not very stylish. Is there a way to make use of the HikaShop notification mechanism?

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2luc3RhbGwtdXBkYXRlLzkwMzk4My1ob3ctdG8tY3JlYXRlLWEtY3VzdG9tLXZhbGlkYXRpb24tZm9yLWN1c3RvbS1maWVsZHM/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86358
- Thank you received: 14290
-
-

4 years 8 months ago -  4 years 8 months ago [\#339518](#339518) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[How to create a custom validation for custom fields](/forum/25-install-update/903983-how-to-create-a-custom-validation-for-custom-fields#339518 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.x -- PHP version -- : 8.x  Is it possible to create custom validations for custom fields and if so, how do I do that? I also need to know how to show custom notifications and error messages, both from within JavaScript and PHP code.")*

Hi,

That's indeed good practice.

If the HikaShop cart notification plugin is activated and configured to display the notification box upon add to cart, then you can use such code: Code:

jQuery.notify({title:'My title',text:'My text'},{style:"metro-lite",className:'warning'});

If you have a DOM element on which to attach the notification box, you can do it like that: Code:

jQuery(el).notify({title:'My title',text:'My text'},{style:"metro-lite",className:'warning',arrowShow:true});

Last edit: 4 years 8 months ago by [nicolas](/forum/user/63-nicolas "View nicolas's Profile").

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2luc3RhbGwtdXBkYXRlLzkwMzk4My1ob3ctdG8tY3JlYXRlLWEtY3VzdG9tLXZhbGlkYXRpb24tZm9yLWN1c3RvbS1maWVsZHM/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")**
- [![pjdevries's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/64503-pjdevries "View pjdevries's Profile") Topic Author
- [ Offline](/forum/user/64503-pjdevries)

Less More

- Posts: 29
- Thank you received: 2
-
-

4 years 8 months ago [\#339530](#339530) by [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")  Replied by *[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")* on topic *[How to create a custom validation for custom fields](/forum/25-install-update/903983-how-to-create-a-custom-validation-for-custom-fields#339530 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.x -- PHP version -- : 8.x  Is it possible to create custom validations for custom fields and if so, how do I do that? I also need to know how to show custom notifications and error messages, both from within JavaScript and PHP code.")*

Works like a charm. Thank you very much for your help.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2luc3RhbGwtdXBkYXRlLzkwMzk4My1ob3ctdG8tY3JlYXRlLWEtY3VzdG9tLXZhbGlkYXRpb24tZm9yLWN1c3RvbS1maWVsZHM/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")**
- [![pjdevries's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/64503-pjdevries "View pjdevries's Profile") Topic Author
- [ Offline](/forum/user/64503-pjdevries)

Less More

- Posts: 29
- Thank you received: 2
-
-

4 years 8 months ago -  4 years 8 months ago [\#339531](#339531) by [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")  Replied by *[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")* on topic *[How to create a custom validation for custom fields](/forum/25-install-update/903983-how-to-create-a-custom-validation-for-custom-fields#339531 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.x -- PHP version -- : 8.x  Is it possible to create custom validations for custom fields and if so, how do I do that? I also need to know how to show custom notifications and error messages, both from within JavaScript and PHP code.")*

Unfortunately not like a charm after all ![:(](/media/kunena/emoticons/22.png ":(")

> function onBeforeProductQuantityCheck(&amp;$products, &amp;$cart, &amp;$options) {
> if($this-&gt;checkFunction($products) === false) {
> $products = array(); // remove the products data being added to the cart so that the add to cart fails
> $cartClass = hikashop\_get('class.cart');
> $cartClass-&gt;addMessage($cart, array('msg' =&gt; 'the product with the id $id could not be added to the cart because the check failed', 'product\_id' =&gt; $id, 'type' =&gt; 'error'));
> }
> }

The message I pass in the **msg** item of the second parameter for **$cartClass-&gt;addMessage(...)**, is not displayed. Instead a general text is displayed: "Product not added to the cart" (PRODUCT_NOT_ADDED_TO_CART).

Is something missing?

Last edit: 4 years 8 months ago by [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile").

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2luc3RhbGwtdXBkYXRlLzkwMzk4My1ob3ctdG8tY3JlYXRlLWEtY3VzdG9tLXZhbGlkYXRpb24tZm9yLWN1c3RvbS1maWVsZHM/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")**
- [![pjdevries's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/64503-pjdevries "View pjdevries's Profile") Topic Author
- [ Offline](/forum/user/64503-pjdevries)

Less More

- Posts: 29
- Thank you received: 2
-
-

4 years 8 months ago [\#339533](#339533) by [pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")  Replied by *[pjdevries](/forum/user/64503-pjdevries "View pjdevries's Profile")* on topic *[How to create a custom validation for custom fields](/forum/25-install-update/903983-how-to-create-a-custom-validation-for-custom-fields#339533 "-- HikaShop version -- : 4.4.5 -- Joomla version -- : 4.x -- PHP version -- : 8.x  Is it possible to create custom validations for custom fields and if so, how do I do that? I also need to know how to show custom notifications and error messages, both from within JavaScript and PHP code.")*

Yes there was something missing :oops:. I did not pass the correct **product\_id**, but 0 (zero). Apparently the **msg** is ignored in that case.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2luc3RhbGwtdXBkYXRlLzkwMzk4My1ob3ctdG8tY3JlYXRlLWEtY3VzdG9tLXZhbGlkYXRpb24tZm9yLWN1c3RvbS1maWVsZHM/dG1wbD1tYXJrZG93bg==) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

Board Categories HikaShop Install &amp; Update Product &amp; Category Display Content Creation Sales and Orders Orders management Reports &amp; Statistics Customers Checkout Payment Methods Shipping Methods Import &amp; Export (CSV, XLS…) Specific Features Affilation Taxes Wishlist Vote Carousel &amp; Slider Mass Actions Filters &amp; Search Discount &amp; Coupon Feature Request Integrations Showcase General Talk about HikaShop Development HikaShop 3.0 Beta Translation Commercial Jobs HikaShop Components HikaMarket Market: Feature requests Market: How to? Market: Bug Report HikaSerial General talk about HikaSerial Serial: Bug Report Serial: How to? Support en Français de HikaSerial (French Support) HikaSubscription HikaAuction Auction: Feature requests Auction: How to? Auction: Bug report Hikashop around the world Support en Français (French Support) Russian FAQ. Курс выживания с Hikashop. Частые вопросы и о Расширения Hikashop Наши сайты на Hikashop 日本語版サポート(Japanese Support) Thai German Archives Bug Report How to?

[](/forum/topics/feed/mode-topics?format=feed&type=rss)

Time to create page: 0.078 seconds

[Powered by](/forum/credits) [Kunena Forum](https://www.kunena.org)
