html-forms icon indicating copy to clipboard operation
html-forms copied to clipboard

[WIP] - Throwing exceptions

Open JeroenSormani opened this issue 6 years ago • 1 comments

Wanted to do a proposal to allow for throwing Exceptions during form validation. The current PR as is doesn't include this, but I'd even like to take it a step further and also include the form/action processing in the try/catch so Exceptions can also be thrown during processing Actions.

Throwing a Exception would be a easier way of capturing errors and easier for a custom error (message) to be added.

I've come to this PR by wanting to have a way to have configurable validation rules while processing a form (checking a support license through API for example). Being able to create a custom License Validation Action where maybe I add message setting for example or API key for the call it needs to do.

Let me know what you think about using Exceptions in such manner..

JeroenSormani avatar Apr 12 '18 08:04 JeroenSormani

PS. this won't change existing validation configurations to prevent some small change of breaking existing customizations users may've done.

The following example shows what could break if a user has a custom way of throwing a exception and is not accounting for that in a hook with the current way of things.

add_action( 'hf_form_error', function( $error_code, $form, $data ) {

	error_log( 'The following error occurred: ' . $error_code ); // Works, message only
	update_post_meta( $form->ID, '_last_error', $error_code ); // Works, but maybe unexpectedly for the one to see a Exception stored
	update_option( 'test', $error_code ); // Breaks! Trying to clone a uncloneable object

	if ( ! empty( $error_code ) || $error_code == 'test' ) { // No errors

	}

}, 10, 3 );

JeroenSormani avatar Apr 12 '18 08:04 JeroenSormani