Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-11-21 03:20:33

kevinpotts
Member
From: Ghost Coast
Registered: 2004-12-07
Posts: 370

zem_contact_reborn: checking values of two e-mail fields

Being an absolute JavaScript moron, I need help. I have a great form built with zem_contact_reborn, and it includes a field that asks for an e-mail address, and then another field that asks to confirm that address. The trick of course is valaidating the data between those two fields and alerting the user to any errors. From a couple hours of research, I have discovered I need one of two things:

  1. A means of adding an onSubmit value to the <form> tag itself, as demonstrated in a script like this
  2. A script that uses zem_contact_reborn’s ID and class hooks; in other words, a script that can target the class of zemContactForm without adding any junk to the form HTML itself, such as the one used here (which is a Textpattern-driven site)

I hope this makes sense.


Kevin
(graphicpush)

Offline

#2 2008-11-21 08:42:12

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,729
Website

Re: zem_contact_reborn: checking values of two e-mail fields

Kevin, zem_contact_reborn provides some callbacks for additional php functions to hook into and feed back an error message using zcr’s built-in error handling. Tranquillo’s pap_contact_cleaner uses this for example to add some honeypot fields to the end of the form and test whether they have been used or not. You can use this to validate that both fields have been filled in identically.

By way of example, here’s a clip of the code for pap_contact_cleaner with some comments added by me:

// callback to display pap_cleaner honeypot fields 
register_callback('pap_zemcontact_form','zemcontact.form');

// callback to run form through pap_zemcontact_submit on submission of form 
register_callback('pap_zemcontact_submit','zemcontact.submit');


// displays additional fields in form

function pap_zemcontact_form() {

	$field = '<div style="visibility: hidden; clear:both;">
'.finput('text','phone',ps('phone'),'','','','','','phone').'
<br />
'.finput('text','mail',ps('mail'),'','','','','','mail').'
</div>';
	return $field;

}

// checks whether honeypot fields are filled out on submit and rejects if used

function pap_zemcontact_submit() {

	$checking_mail_field = trim(ps('mail'));
	$checking_phone_field = trim(ps('phone'));

	$evaluation =& get_zemcontact_evaluator();

	// If the hidden fields are filled out, the contact form won't be submitted!
	if ($checking_mail_field != '' || $checking_phone_field != ''){
		$evaluation -> add_zemcontact_status(1);
	}
	return;

}

A second example, this one used with mem_form which behaves almost identically to zcr except for the function names. This one checks two date fields for an event and verifies that the start is in the future and that the end date is after the start date.

// callback to validating date plausibility on submission of form
register_callback('jcr_memform_validate_date','mem_form.submit');


// date validation function

function jcr_memform_validate_date() {

	global $mem_form_error, $isError;

	$begin_date  = strtotime(trim(ps('custom_2')));
	$end_date    = strtotime(trim(ps('custom_3')));
	$todays_date = strtotime(date('Y-m-d'));

	// if end date is before start date
	if ($end_date < $begin_date) {
	    $mem_form_error[] = 'Please check your input: The end date you have specified is before the start date.';
	    $isError = "errorElement";
	} 

	// if start date is in the past
	if ($begin_date < $todays_date ) {
	    $mem_form_error[] = 'Please check your input: The start date you have given is in the past.';
	    $isError = "errorElement";
	}
	return;
}

With that you should be able to cobble together a function to verify that both fields are identical.

Last edited by jakob (2008-11-21 08:43:03)


TXP Builders – finely-crafted code, design and txp

Offline

#3 2008-11-21 10:01:54

gomedia
Plugin Author
Registered: 2008-06-01
Posts: 1,373

Re: zem_contact_reborn: checking values of two e-mail fields

Hi Kevin. You’ve probably seen this but I’ll make the link anyway. There’s a conversation going on in the zem_contact_reborn forum at the moment regarding validating user input. This may be pertinent to your query.

  • cbeyls requirement is for a way of returning the user to the form if validation of a CAPTCHA fails.
  • My query was regarding field dependencies (e.g. IF FieldA is filled in then FieldB must be also ELSE return to form input).

jakob: Your suggestion looks interesting but (without fully understanding ZCR) I’ve a feeling that the stumbling block is:

  1. generating a custom error message in ZCR pertinent to the validation error
  2. returning the user to the form for corrections

It’ll be interesting to see how this one pans out.

Offline

#4 2008-11-21 13:31:32

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,729
Website

Re: zem_contact_reborn: checking values of two e-mail fields

…I’ve a feeling that the stumbling block is:

  1. generating a custom error message in ZCR pertinent to the validation error
  2. returning the user to the form for corrections

zcr and mem_form handle this all for you. In my second example (in that particular case for mem_form) you can see the error string I specified for that particular error and because it hooks into the callback it gets displayed in the same way as any other error generated by zcr/mem_form. If you’re not familiar with zcr, if zcr encounters a form error it returns you to the form (maintaining the form input) and reports errors as a ul-list in a div at the top of the form. It also appends an error class to the relevant fields so that you can highlight them with css.

Last edited by jakob (2008-11-21 13:32:57)


TXP Builders – finely-crafted code, design and txp

Offline

#5 2008-11-23 10:52:44

cbeyls
Archived Plugin Author
From: Brussels, Belgium
Registered: 2005-09-12
Posts: 136
Website

Re: zem_contact_reborn: checking values of two e-mail fields

Hello jakob,

You are right, I did not realize that ZCR allows you to do form validation even for fields that are added automatically to the form by antispam plugins, but what I’m still missing is a way to redisplay the form after the form validation, during the spam check. Because I want to display the captcha only if required, after all fields are validated and if the content can’t be flagged as spam or ham.


My plugins : cbs_live_search (improved) – cbs_category_list – cbs_navigation_menu – cbs_gravatar (updated) – cbs_article_index – cbs_maintenance_mode (new) – cbs_section_language (new)

Offline

Board footer

Powered by FluxBB