Validating Custom Field Input
Validating Custom Field Input
Hi,
Currently we have products that have a custom field where you can enter in the approximate date you need/would like the item delivered to you.
There is also a product option for shipping time-frame, so you can choose standard or rush.
The custom field needs to be required and in a date format. So far the changes made to accommodate that part are working well.
What would be the best spot to tackle validating that the date put into the custom field by the customer is actually within a valid range based on the current date?
Is there a spot where it would be possible to validate it when they choose between standard and rush? Or should this be done as part of the check when they attempt to add it into the basket? (And if so, where would that be?)
Just recently learning my way around the source/api, so bear with me on what might be obvious questions.
Thank you!
Currently we have products that have a custom field where you can enter in the approximate date you need/would like the item delivered to you.
There is also a product option for shipping time-frame, so you can choose standard or rush.
The custom field needs to be required and in a date format. So far the changes made to accommodate that part are working well.
What would be the best spot to tackle validating that the date put into the custom field by the customer is actually within a valid range based on the current date?
Is there a spot where it would be possible to validate it when they choose between standard and rush? Or should this be done as part of the check when they attempt to add it into the basket? (And if so, where would that be?)
Just recently learning my way around the source/api, so bear with me on what might be obvious questions.
Thank you!
Re: Validating Custom Field Input
You can add a custom validator in ProductHelper class when adding the custom field. You can then adjust this custom validator to run some server code for validation when some try to add product to basket. In server validation function you can put your custom logic to check date within range. So first you need to edit the App_Code/ProductHelper.cs class and make BuildProductChoices(Product product, PlaceHolder phChoices) method look like
Now add following function to ProductHelper class
Finally update this function according to your validation needs. I am assuming that you are using product templates's customer field of text box to handle custom date input. You need to put the name customer field in following statement
in place of DateField text.
Code: Select all
public static void BuildProductChoices(Product product, PlaceHolder phChoices)
{
// ADD IN THE PRODUCT TEMPLATE CHOICES
ProductTemplate template = product.ProductTemplate;
if (template != null)
{
foreach (InputField input in template.InputFields)
{
if (!input.IsMerchantField)
{
// ADD THE CONTROL TO THE PLACEHOLDER
phChoices.Controls.Add(new LiteralControl("<tr><td colspan=\"2\">"));
phChoices.Controls.Add(new LiteralControl((input.UserPrompt + "<br />")));
WebControl o = input.GetControl();
if (o != null)
{
phChoices.Controls.Add(o);
if (input.Name == "DateField")
{
CustomValidator customValidator = new CustomValidator();
customValidator.ID = "mycustomevalidator";
customValidator.ControlToValidate = string.Empty;
customValidator.ValidationGroup = "AddToBasket";
customValidator.ErrorMessage = "Date validation message here...";
customValidator.Text = "*";
customValidator.ServerValidate += new ServerValidateEventHandler(ServerValidation);
phChoices.Controls.Add(customValidator);
}
}
phChoices.Controls.Add(new LiteralControl("</td></tr>"));
}
}
}
}
Code: Select all
public static void ServerValidation(object source, ServerValidateEventArgs arguments)
{
bool isValid = false;
// Your validation code here
// isValid = your validation result
arguments.IsValid = isValid;
}
Code: Select all
if (input.Name == "DateField")
Re: Validating Custom Field Input
You may want to see this post as well: viewtopic.php?f=42&t=11004&hilit=+regular+expression. It describes how to add a regular expression validator.
It explains how to use additional custom fields as parameters to your custom validation if you need that. If you do, it notes that you need to hide those custom parameter fields from the product display in several places.
Hope that helps.
It explains how to use additional custom fields as parameters to your custom validation if you need that. If you do, it notes that you need to hide those custom parameter fields from the product display in several places.
Hope that helps.
Re: Validating Custom Field Input
That was helpful, thanks. Almost done!
What would be the best way of retrieving the current value of one of the product option/dropdown selections inside of the ServerValidation function?
(There is a TimeFrame dropdown selection/option as part of the product that designates Rush or Standard, and the verification of the date specified in the custom date field will vary based on which Time Frame option was chosen.)
What would be the best way of retrieving the current value of one of the product option/dropdown selections inside of the ServerValidation function?
(There is a TimeFrame dropdown selection/option as part of the product that designates Rush or Standard, and the verification of the date specified in the custom date field will vary based on which Time Frame option was chosen.)
Re: Validating Custom Field Input
For this check CollectProductTemplateInput method of ProductHelper class.
Re: Validating Custom Field Input
Sorry, this is all pretty new to me, and I'm at a loss as to what should be passed to the CollectProductTemplateInput function to retrieve the info I'm after when I'm inside of the ServerValidation routine. (The option on the product is called "Time Frame.")
I can see how I might do it if I were inside of the BuyProductDialog.ascx.cs file, but I'm just not sure how to reference getting to the page/product/etc variables from inside of the Product Helper class, in particular from inside of the ServerValidation routine.
(Or on that same note, how to reference/pull the value for the custom text box from inside of ServerValidation if the ControlToValidate property of the CustomValidator isn't set to o.ID as opposed to string.Empty.
If it is set the CustomValidator property to o.ID I can check arguments.Value to see what they typed in for the custom text box without any problem, but when it's empty, I'm not sure what the name of the field should be that I would reference, since those are dynamically built.)
I think that would be okay though, so my main question is just how to call the CollectProductTemplateInput from inside the ServerValidation function to find the Time Frame option/value?
Thanks for bearing with me with what are probably really easy questions.
I can see how I might do it if I were inside of the BuyProductDialog.ascx.cs file, but I'm just not sure how to reference getting to the page/product/etc variables from inside of the Product Helper class, in particular from inside of the ServerValidation routine.
(Or on that same note, how to reference/pull the value for the custom text box from inside of ServerValidation if the ControlToValidate property of the CustomValidator isn't set to o.ID as opposed to string.Empty.
If it is set the CustomValidator property to o.ID I can check arguments.Value to see what they typed in for the custom text box without any problem, but when it's empty, I'm not sure what the name of the field should be that I would reference, since those are dynamically built.)
I think that would be okay though, so my main question is just how to call the CollectProductTemplateInput from inside the ServerValidation function to find the Time Frame option/value?
Thanks for bearing with me with what are probably really easy questions.

Re: Validating Custom Field Input
Okay, sorry about the confusion, I probably didn't explain that last question very well. Too many late nights! 
I was able to retrieve the value of what they had typed into the custom field/ textbox when the ServerValidation event fired by checking arguments.Value (and setting the customValidator.ControlToValidate = oID).
But I needed to also know the value of a product option though, not another custom field on the template. (The Time Frame is a dropdown/product option.)
I found the answer in the BuildProductOptions routine and the PageHelper.GetProductId() call.
Thanks for all of your help Mazhar (and ZLA!), feel free to ask if I ever post something that doesn't seem to make any sense, some nights I stay up too late and the brain turns to mush.

I was able to retrieve the value of what they had typed into the custom field/ textbox when the ServerValidation event fired by checking arguments.Value (and setting the customValidator.ControlToValidate = oID).
But I needed to also know the value of a product option though, not another custom field on the template. (The Time Frame is a dropdown/product option.)
I found the answer in the BuildProductOptions routine and the PageHelper.GetProductId() call.
Thanks for all of your help Mazhar (and ZLA!), feel free to ask if I ever post something that doesn't seem to make any sense, some nights I stay up too late and the brain turns to mush.