• Resolved danomurray

    (@danomurray)


    Hi,

    First thanks for the great plugin donation to the community – it performs a critical function very well.

    One issue I have found is that if my custom add-to-cart validation fails, the custom form field values are cleared when the form is redisplayed with the validation error message. I am using the cloning fields feature (which is fantastic) and the correct number of custom form fields appear – they just don’t contain any values.

    Is it possible to get the submitted values to show in the form again?

    My validation code is:

    add_action('woocommerce_add_to_cart_validation', array($this, 'validate_order_meta_data'), 10, 3);
    
    function validate_order_meta_data($valid, $product_id, $quantity)
    {
    	$values = array();
    
    	// for each quantity of item added to cart check whether the meta value is unique
    	for ($i = 0; $i < $quantity; $i++)
    	{
    		// request params are built from the field name and a base 1 index
    		$request_key_index = $i + 1;
    		$request_key = "email_$request_key_index";
    		if (isset($_REQUEST[$request_key]))
    		{
    			$value = $_REQUEST[$request_key];
    			// check for duplication
    			if (in_array($value, $values))
    			{
    				wc_add_notice("Value $value appears multiple times", 'error');
    				$valid = false;
    			}
    			else
    			{
    				$values[] = $value;
    			}
    		}
    	}
    
    	return $valid;
    }

    I’m using plugin version 1.2.4 and WordPress 4.3.1

    Thanks,

    Dan.

    https://wordpress.org/plugins/wc-fields-factory/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter danomurray

    (@danomurray)

    Hi,

    Just wondered if it was possible to get a quick update – is this something that is already possible or will be possible in a future version, or should we be looking to find a different solution?

    Thanks,

    Dan.

    Plugin Author Saravana Kumar K

    (@mycholan)

    Hi danomurray, thanks for raising this.

    That would be great if all values remains even after a validation failed.
    Yes it is possible. I will try to include on my next release.

    ( I will try to give you quick update, if got time )

    Regards
    Sark

    Thread Starter danomurray

    (@danomurray)

    Hi Sark,

    Thanks for the quick reply and it’s great that this can be in a future release.

    I actually meant a quick update in terms of information rather than code but if you can provide a quick code update then even better!

    Thanks again,

    Dan.

    Plugin Author Saravana Kumar K

    (@mycholan)

    Hi, V1.2.5 has been released with Client side validation option, so that option will prevent this issue from happening.

    Note : by default it’s in disable state, you will have to enable it by WC Fields Factory settings page.

    Thread Starter danomurray

    (@danomurray)

    Hi Sark,

    Thanks very much for your help and for the code update – I have installed the update and enabled the Client Side Validation.

    I am using the cloning fields feature (which is great), and I have an email address for each item added to the basket. So if the quantity is 2 then there are 2 email address fields, and I need to ensure that the same email address is not entered in more than one field. To achieve this I have in place my custom validation:

    add_action('woocommerce_add_to_cart_validation', array($this, 'validate_order_meta_data'), 10, 3);
    
    function validate_order_meta_data($valid, $product_id, $quantity)
    {
    	$values = array();
    
    	// for each quantity of item added to cart check whether the meta value is unique
    	for ($i = 0; $i < $quantity; $i++)
    	{
    		// request params are built from the field name and a base 1 index
    		$request_key_index = $i + 1;
    		$request_key = "email_$request_key_index";
    		if (isset($_REQUEST[$request_key]))
    		{
    			$value = $_REQUEST[$request_key];
    			// check for duplication
    			if (in_array($value, $values))
    			{
    				wc_add_notice("Value $value appears multiple times", 'error');
    				$valid = false;
    			}
    			else
    			{
    				$values[] = $value;
    			}
    		}
    	}
    
    	return $valid;
    }

    At the moment, if my custom validation fails then I see the validation error message shown, but my email address field values are cleared. Can I achieve what I want (my custom validation but not clearing the email address fields) using the new Client Side Validation?

    Thanks again for your help,

    Dan.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Validation failure clears form fields’ is closed to new replies.