How to create a custom validation for custom fields

  • Posts: 29
  • Thank you received: 2
2 years 1 month ago #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.

Please Log in or Create an account to join the conversation.

  • Posts: 81378
  • Thank you received: 13037
  • MODERATOR
2 years 1 month ago #339498

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 or Create an account to join the conversation.

  • Posts: 29
  • Thank you received: 2
2 years 1 month ago #339505

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 :)

Attachments:

Please Log in or Create an account to join the conversation.

  • Posts: 81378
  • Thank you received: 13037
  • MODERATOR
2 years 1 month ago #339508

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>Views and add a bit of javascript to the page like so:

<script>
document.getElementById('xxx').addEventListener('change', (event) => {
  // do your check on event.target.value , you can modify event.target.value and use alert() to display a message
});
</script>
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(&$products, &$cart, &$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:
function onBeforeProductQuantityCheck(&$products, &$cart, &$options) {
 if($this->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->addMessage($cart, array('msg' => 'the product with the id $id could not be added to the cart because the check failed', 'product_id' => $id, 'type' => '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

Please Log in or Create an account to join the conversation.

  • Posts: 29
  • Thank you received: 2
2 years 1 month ago #339517

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 :)

...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 or Create an account to join the conversation.

  • Posts: 81378
  • Thank you received: 13037
  • MODERATOR
2 years 1 month ago #339518

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:

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:
jQuery(el).notify({title:'My title',text:'My text'},{style:"metro-lite",className:'warning',arrowShow:true});

Last edit: 2 years 1 month ago by nicolas.

Please Log in or Create an account to join the conversation.

  • Posts: 29
  • Thank you received: 2
2 years 1 month ago #339530

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

Please Log in or Create an account to join the conversation.

  • Posts: 29
  • Thank you received: 2
2 years 1 month ago #339531

Unfortunately not like a charm after all :(

function onBeforeProductQuantityCheck(&$products, &$cart, &$options) {
if($this->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->addMessage($cart, array('msg' => 'the product with the id $id could not be added to the cart because the check failed', 'product_id' => $id, 'type' => 'error'));
}
}


The message I pass in the msg item of the second parameter for $cartClass->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: 2 years 1 month ago by pjdevries.

Please Log in or Create an account to join the conversation.

  • Posts: 29
  • Thank you received: 2
2 years 1 month ago #339533

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 or Create an account to join the conversation.

Time to create page: 0.070 seconds
Powered by Kunena Forum