CustomValidator for AddToCart not working
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
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
>
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
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
>
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
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,