CustomValidator for AddToCart not working

Posted by Community Admin on 05-Aug-2018 19:25

CustomValidator for AddToCart not working

All Replies

Posted by Community Admin on 04-Jun-2013 00:00

I need to do special validation on the product quantity before adding it to the shopping cart.

1. I mapped the AddToCartWidget to use my external template.

2. I add a CustomValidator to the Quantity field:
<asp:CustomValidator ID="CustomValidator1" runat="server"
    OnServerValidate="CaseValidate"
    ValidationGroup="addToCart"
    ControlToValidate="quantity"
    Type="Integer"
    Display="Dynamic"
    ErrorMessage="Must order in multiples of case pack.">
</asp:CustomValidator>

3. My validation function gets called ok, I set args.IsValid = false, but the product is added to the cart, anyway.

Will a <asp:CustomValidator work here, or do I need another approach ?

Thanks much,
Tim




Posted by Community Admin on 06-Jun-2013 00:00

Hi Tim,

 Thank you for using Sitefinity.

What kind of validation are you trying to accomplish?

There is already a quantity and range validator available in the template.

<asp:RequiredFieldValidator ID="quantityRequiredFieldValidator" runat="server"
                ControlToValidate="quantity"
                ValidationGroup="addToCart"
                Display="Dynamic"
                CssClass="sfErrorWrp">
                <span class="sfError">
                    <asp:Literal runat="server" ID="lProductQuantityIsRequired" Text="<%$Resources:OrdersResources, ProductQuantityIsRequired %>"
 
/></span>
            </asp:RequiredFieldValidator>
            <asp:RangeValidator ID="quantityValidator" runat="server"
                MinimumValue="1"
                MaximumValue="9999"
                ControlToValidate="quantity"
                ValidationGroup="addToCart"
                Type="Integer"
                Display="Dynamic"
                CssClass="sfErrorWrp">
                <span class="sfError">
                    <asp:Literal runat="server" ID="lProductQuantityIsInvalid" Text="<%$Resources: OrdersResources, ProductQuantityIsInvalid %>"
 
/></span>

If you can let me know I can point you to which asp validation control is best for your situation.

Regards,
Patrick Dunn
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 07-Jun-2013 00:00

Hi Patrick,

One example is "casepack" size  (you have to order in multiples of  12 (or whatever the number is... the casepack is a  custom fieled added to products)

-tim

Posted by Community Admin on 12-Jun-2013 00:00

Hello Tim,

I hope you're well today.

Here's my add to cart with a custom validator that checks if the quantity is 2.

<%@ Control Language="C#" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI" Assembly="Telerik.Sitefinity" %>
<asp:UpdatePanel ID="UpdatePanel" runat="server">
    <ContentTemplate>
        <sf:Message runat="server" ID="addedToCartMessage" CssClass="sfMessage sfTopMsg" RemoveAfter="10000" />
        <div id="addToCartWidgetWrp" runat="server">
            <asp:Label ID="quantityLabel" runat="server" Text='<%$Resources:OrdersResources, QuantityColon %>' AssociatedControlID="quantity"
 
CssClass="sfTxtLbl" />
            <asp:RequiredFieldValidator ID="quantityRequiredFieldValidator" runat="server"
                ControlToValidate="quantity"
                ValidationGroup="addToCart"
                Display="Dynamic"
                CssClass="sfErrorWrp">
                <span class="sfError">
                    <asp:Literal runat="server" ID="lProductQuantityIsRequired" Text="<%$Resources:OrdersResources, ProductQuantityIsRequired %>"
 
/></span>
            </asp:RequiredFieldValidator>
            <asp:RangeValidator ID="quantityValidator" runat="server"
                MinimumValue="1"
                MaximumValue="9999"
                ControlToValidate="quantity"
                ValidationGroup="addToCart"
                Type="Integer"
                Display="Dynamic"
                CssClass="sfErrorWrp">
      
                <span class="sfError">
                    <asp:Literal runat="server" ID="lProductQuantityIsInvalid" Text="<%$Resources: OrdersResources, ProductQuantityIsInvalid %>" /></span>
            </asp:RangeValidator>
                             
            <asp:CustomValidator ID="custom" runat="server"
                ControlToValidate="quantity" ClientValidationFunction="validateQuantity"
                ErrorMessage="Failed validation" ValidationGroup="addToCart" Display="Dynamic"   />
 
 
            <asp:TextBox ID="quantity" runat="server" CssClass="sfTxt sfQuantity" CausesValidation="true" ValidationGroup="addToCart" />
            <asp:Button ID="addToCartButton" runat="server" Text='<%$Resources:OrdersResources, AddToCart %>' ValidationGroup="addToCart" CssClass="sfAddToCartBtn" />
 
   </div>
            <script>
                function validateQuantity(sender, args)
                    if (args.Value == 2)
                        alert("TEST");
                        args.IsValid = false;
                    
                    else
                        args.IsValid = true;
                    
                
        </script>
    </ContentTemplate>
</asp:UpdatePanel>

It works as you can see the video here:

http://screencast.com/t/LQmpPYg31x
Regards,
Patrick Dunn
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 12-Jun-2013 00:00

Thanks Patrick, that helps.

ServerSide validation would have been better, but I can probably live with it.

Is there any trick to getting access to other product custom variables  that I need to do the validation calculation (for example I need casePack for this particular example)  

Just  make hidden fields for all of the things I need ? 

Thanks again for your help!
-Tim

Posted by Community Admin on 13-Jun-2013 00:00

Hi Tim,

 Hidden fields would be the best way to go about it with client validation, yes, or grab the control that they already have values loaded in and do the validation that way.

Regards,
Patrick Dunn
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed