You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by ZKN __ <zk...@abv.bg> on 2012/09/13 16:38:00 UTC

Confirm mixin with form validation errors

Hi,
 
I'm having some troubles with the ClickOnce mixin from Tapestry's user guide. Luckily I found this post http://tapestry.1045711.n5.nabble.com/quot-Confirm-quot-mixin-won-t-cancel-when-in-zone-td2436465.html how to stop event bubbling and that works great. 
Now I have a different problem. When the user clicks the submit link, the mixin gets executed, the alreadyClickedOnce variable gets set to true as it should be and the button cannot be clicked again. Perfect. 
BUT the form validation fails and Tapestry stops the form submission. From now on I can't submit the form because the button is already disabled by the ClickOnce mixin. 
Anyone have an idea if it is possible to handle form validation errors within the mixin? I would like to avoid disabling java scrip validation of the form. 
Thanks, 
Oz 

Re: Confirm mixin with form validation errors

Posted by Josh Canfield <jo...@gmail.com>.
I don't think this addresses the problem though, right?

If client-side form validation fails you still are in a state where you
can't click the submit button again after fixing the error.

Tapestry doesn't have an event for "validation failed", and I don't know of
an approved way to workaround that. But! What you can do is add an observer
for Tapestry.FORM_VALIDATE_FIELDS_EVENT and set a timeout (1/4 of a
second?) to re-enable the field if you don't get a
Tapestry.FORM_PREPARE_FOR_SUBMIT_EVENT before the timeout.

Here is a prototype based on Geoff's example from jumpstart. This was
created for submit buttons, you'd have to adjust for links (find the
form...)

ClickOnce = Class.create( {

    initialize: function(elementId) {
        var el = $(elementId);
        el.clickOnce = this;
        if ( el['form'] ) {
            el.form.observe(Tapestry.FORM_VALIDATE_FIELDS_EVENT, function()
{
                console.log("Hey, we're observing!");
                el.clickOnce.clickOnceTimeout =
window.setTimeout(function(){
                    console.log("Let them click again")
                    el.clickOnce.alreadyClickedOnce = false;
                }, 250)
            });

            el.form.observe(Tapestry.FORM_PREPARE_FOR_SUBMIT_EVENT,
function() {
                window.clearTimeout(el.clickOnce.clickOnceTimeout);
            });
        }
        this.alreadyClickedOnce = false;

        Event.observe(el, 'click',
this.doClickOnce.bindAsEventListener(this));
    },

    doClickOnce: function(e) {
        var element = Event.element(event);
        console.log("Clicked");
        if (element.clickOnce.alreadyClickedOnce) {
            console.log("and cancelled");
            e.stop();
        }
        element.clickOnce.alreadyClickedOnce = true;
    }

} );


// Extend the Tapestry.Initializer with a static method that instantiates a
ClickOnce.

Tapestry.Initializer.clickOnce = function(spec) {
    new ClickOnce(spec.elementId);
};

Re: Confirm mixin with form validation errors

Posted by trsvax <tr...@gmail.com>.
Here is my jQuery one that is added to a form

@MixinAfter
@Import( library={"submitonce.js"})
public class SubmitOnce {
	
	@Inject
	private Logger logger;
	
	@Inject
	private JavaScriptSupport javaScriptSupport;
	
	@InjectContainer
	private ClientElement clientElement;
	
	private String id;
	
	void beginRender() {
		id = clientElement.getClientId();
	}
	
	void afterRender(MarkupWriter writer) {

		JSONObject params = new JSONObject();
		params.put("id",id);
		javaScriptSupport.addInitializerCall("submitonce", params);
	}

}

(function( $ ) {
	$.extend(Tapestry.Initializer, {
		
		
		submitonce: function(specs) {		 
			$('#'+specs.id + ' :submit').removeAttr('disabled');
			
			$('#'+specs.id).bind('tapestry:formprepareforsubmit',function() {
					$('#'+specs.id + ' :submit').attr('disabled', 'disabled');
				});
	
		}
	});
}) ( jQuery );

You need to look for the formprepareforsubmit event.



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Confirm-mixin-with-form-validation-errors-tp5716265p5716266.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org