- Posts: 29
- Thank you received: 2
How to create a custom validation for custom fields
-- 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.
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.
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
Please Log in or Create an account to join the conversation.
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:
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:
www.hikashop.com/support/documentation/6...r-documentation.html
Please Log in or Create an account to join the conversation.
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.
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:
If you have a DOM element on which to attach the notification box, you can do it like that:
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
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?
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.