determine form that called fc_preprocess

Scott RestonScott Reston Member
in Help edited April 2008
Is there a way to determine the ID (or a pointer to the object) of the form that calls fc_preprocess()?

I've got a situation where I've got several product forms on a page and I want to do validation *only* for the form that was submitted.
Comments
  • brettbrett FoxyCart Team
    That's definitely an interesting question. Offhand I'm not sure but I'll think about it and get back to you.
  • Thanks. I've been struggling with it for a couple of days now. Is fc_preprocess() the only way to 'get stuff done' before foxycart submits its form?

    It looks like I can attach an onsubmit to the form tag and that'll get called before foxycart. I'm working on setting some variable to track which form was submitted. Would this be considered stable, though? Can I always depend on the onsubmit being processed before fc_preprocess does it's stuff?
  • lukeluke FoxyCart Team
    Hey Scott. You'll probably have to hack something until we add the form to fc_PreProcess() (which actually seems like a good idea). Here's what's being done in foxycart.js which is included in the foxycart_incldues.js:
    $j(document).ready(function(){
    	// don't load the cart if we're on the cart
    	if (location.href.substring(location.href.lastIndexOf('/')+1,location.href.lastIndexOf('/')+5) != "cart") {	
    		fc_UpdateCart(FoxyDomain); 
    		// overide the default submit for Foxy Cart forms
    		$j("form.foxycart").submit(function(){
    			// if they defined fc_PreProcess(), let them process the form data before we pass it along
    			if (typeof(fc_PreProcess) == 'function') { 
    				if (!fc_PreProcess()) {
    					return false;
    				}
    			}
    			MyFoxyData = $j(this).formSerialize(false);
    			// if we want to go directly to the checkout, don't show the foxybox 
    			if (MyFoxyData.match("cart=checkout")) {
    				location.href = "https://"; + FoxyDomain + "/cart.php?" + MyFoxyData + fc_AddSession();
    			} else {
    				// show ThickBox
    				fc_tb_show(null,"https://"; + FoxyDomain + "/cart.php?" + MyFoxyData + fc_AddSession(),false);
    				// Add to Thick Box's close link so that it will update the cart
    				$j("a.fc_tb_closeWindowButton").click(function(){
    					fc_UpdateCart(FoxyDomain);
    					return false;
    				});
    				// this ensures their submit doesn't actually go anywhere
    			}
    			return false;
    		});
    	}
    });
    
    If you reproduce that code on your site... it might work. Just have fc_PreProcess() return false every time... then add the above code to your site but change fc_PreProcess() to my_PreProcess(this) and fc_PreProcess to my_PreProcess and write a new function (my_PreProcess(myForm)) to do your validation. That will give you access to the form object with the myForm variable. Make sense?
  • Scott RestonScott Reston Member
    edited April 2008
    I've hacked something together to make FoxyCart work with Andrew Tetlaw's unobtrusive "really easy field validation":

    I figured out that onsubmit actions get called before FoxyCart actions, so I built something that puts the form object of the submitted form into a variable whenever a form on the page is submitted. Then I can use that form to run validation, generating a false on failure to stop the FoxyCart action. My demo page:

    http://www.skininsight.net/template-store.html

    Seems to work consistently. There were a few wrinkles that I had to work out with the validation (it likes form elements to have IDs if you've got fields with the same 'name' on each form), but it looks to be working just fine. If you're looking to add validation, an unobtrusive 'class handled' validation like Andrew's would be a nice way to go...

    Thanks for the help!
  • lukeluke FoxyCart Team
    Thanks Scott, that looks like some good stuff.

    We do something similar with the "fc_required" class but that validation looks a lot more complete as ours currently just checks for existing values.
  • I didn't really realize that I could use fc_required on the 'add to cart' form. Seems like when I read the docs for that, it read like it was aimed at adding fields to the checkout form. Thanks!
  • brettbrett FoxyCart Team
    edited April 2008
    I think Luke was perhaps making an indirect comparison or something. (Or I could be totally missing it myself ;).
    fc_required is for checkout, so you were right there, I believe. There's no built in form validation pre-cart/checkout.

    If I'm wrong, I'll delete this post and pretend it never happened ;)
  • Scott RestonScott Reston Member
    edited April 2008
    thanks... and in-built pre-cart validation would be sweet.
  • lukeluke FoxyCart Team
    Sorry for the confusion, I was just referring to the checkout form. As for pre-cart validation, we leave that completely up to the user since there are some many different variables involved that few "one-size-fits-all" solutions ever get it right. Eventually we'll probably have some libraries of code that people can pick and choose from as needed.

    Most likely, that library will start with contributions made on our wiki or this forum so feel free to post away.
Sign In or Register to comment.