validation check on custom field

  • Posts: 8
  • Thank you received: 2
11 years 6 months ago #100883

I am using a custom field and the donation plugin to create a gift card product where the site visitor can enter the amount to put on the card. One problem stumbled upon is that the user can enter something other than a digit or decimal. I would like to add validation to require a valid numerical value, or a way to process the input so as to strip out any non-numeric character.

Is this a setting I can make? If not, do you have a suggestion where in the code I can add this extra check?

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

  • Posts: 12953
  • Thank you received: 1778
11 years 6 months ago #101135

Hi,

If you want to do some verification in you custom field value, you'll have to do it through the custom_price.php file of your Hikashop custom price plugin by editing these lines :

if(!empty($product->amount)){
			foreach($product->prices as $k => $price){
				$product->prices[$k]->price_value = $product->amount;
				$product->prices[$k]->price_value_with_tax = $product->amount;
			}
and these lines :
if(!empty($product->amount)){
			$product->order_product_price = $product->amount;
			$product->order_product_tax = 0;
		}
Hope this will help you a little.

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

  • Posts: 8
  • Thank you received: 2
11 years 6 months ago #101157

Thanks.

Yesterday, I found a way to add client-side validation as control leaves the field. Briefly, here's how I did that...

In the view files, for front-end products, I edited show_block_custom_item.php. At the top of this file I added an implementation of the javascript function that would perform the validation. This function receives a value, performs validation, and then returns a valid form of the value. For my need, the user-entered text that had to represent a dollar amount. So the code stripped out all character except digits and a decimal. I was also able to add an alert box to inform the user if the amount was less than the minimum (and then reset the text to the minimum). This function's name was forceToNumber(text).

Then, about 20 lines into the code, where the javascript function hikashopToggleFields() is inserted, I insert a call to my function just before that call to hikashopToggleFields(). Here is the completed line of code with my addition in bold:

echo $this->fieldsClass->display($oneExtraField,$itemData,'data[item]',false,' '.$onWhat.'="this.value=forceToNumber(this.value);hikashopToggleFields(this.value,\''.$fieldName.'\',\'item\',0);"');


So in short, my validation function preprocesses the value and corrects any invalid values just before the processing function is called upon this value.

I realize that because this is the only custom field I am using, a simple implementation like this works for me. Were I to be using multiple custom fields, then I would have to add a parameter that would pass the field's name (or type) and then perform the appropriate validation per field name/type.

The following user(s) said Thank You: nicolas

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

Time to create page: 0.067 seconds
Powered by Kunena Forum