Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2008-02-26 19:55:24

Manfre
Plugin Author
From: North Carolina
Registered: 2004-05-22
Posts: 588
Website

Re: mem_form - Generic HTTP form processing

Am I right in thinking that with the display event I can add HTML to the form at either the beginning or the end, based on the $pre value? Is there any way of replacing a tag on a page from within a callback in Texttpattern? Ie. can I put something in the middle of the form and have the callback replace that with something else?

Correct. $pre 1 is before and $pre 0 is after the mem_form input tags. If you need more control over where text is positioned, I’d recommend using CSS and jQuery to shuffle it around. If you replacing text, then mem_form will not be able to process it properly.

Also, am I right that using the callback to programmatically add fields, I still can’t add <txp:mem_form…> tags and pull out the data from the $mem_form_values[] array? As they’re not procecced I’ll still have to work with $_POST to get the values from the extra fields I add? That seems to be the result I get if I add mem_form tags from my display callback.

Are you trying to access $mem_form_values[] from within the display callback or from the submit callback? Is there anything in the array? Did you declare it as a global in your function?

Offline

#14 2008-02-26 20:13:19

kelsta
New Member
Registered: 2006-05-31
Posts: 7

Re: mem_form - Generic HTTP form processing

I added a diplay call back which adds some extra checkboxes to the form like this

$tag = "";
$skillAreas = safe_rows('*', 'skill_areas', 'true');
foreach ($skillAreas as $skillArea)
{
   $tag .= "<txp:mem_form_checkbox name=\"".$skillArea['skill']."\" label=\"".$skillArea['long_name']."\" break=\"\" required=\"0\" /><br />\n";
}
return $tag;

Then in the submit callback I have

global $mem_form_values;

foreach($mem_form_values as $key => $value) { echo $key . “ “ .$value.”<br />”;
}

But the values of the extra checkboxes aren’t in the array.

Looking at the debugging section of the source for the produced form, the added mem_form_chackbox tags come after the [secondpass]

Offline

#15 2008-02-26 20:50:53

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: mem_form - Generic HTTP form processing

kelsta, I think your initial approach using inline PHP was better, but you should do something like this:

<txp:php>
$skillAreas = safe_rows('*', 'skill_areas', 'true');
foreach ($skillAreas as $skillArea)
{
  echo mem_form_checkbox(
    array(
      'name' => $skillArea['skill'],
      'label' => $skillArea['long_name'],
      'break' => "",
      'required' => 0
    )
  ). "<br />\n";
}
</txp:php>

Offline

#16 2008-02-27 14:33:06

kelsta
New Member
Registered: 2006-05-31
Posts: 7

Re: mem_form - Generic HTTP form processing

Thanks ruud, calling the mem_form functions directly like that works a treat to allow me to add elements to my form programatically.

Just a couple of little niggles left. If I add some radio buttons to the form, setting group=“sameGroupName” and name=“differentValue” for each button, I don’t get a radio button group on the page. Any and all of the buttons in the group can be selected individually. Is this a bug or am I misunderstanding how this element is supposed to work?

Secondly, it’d be nice to be able to add a checkbox group to the form using the name=“groupName[]” value=“differentValues” syntax and then access all the values from those checkboxes as an array on the server side. Not quite sure how that’d work with the code that saves the values to mem_form_values[] off the top of my head. It’d be a nice addition if it’s possible though.

Offline

#17 2008-02-27 16:55:36

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: mem_form - Generic HTTP form processing

That’s the correct way to use radio buttons, yes, assuming that part of mem_form is still identical to ZCR.
Check the resulting HTML code to see if the name of those input tags is the same for all radio buttons in one group.

Offline

#18 2008-02-27 17:04:11

benbruce
Plugin Author
Registered: 2006-01-13
Posts: 328
Website

Re: mem_form - Generic HTTP form processing

I’m trying to use the redirect="' attribute to refresh the page. ie, something like:

<txp:asy_wondertag><txp:mem_form type="form" redirect="<txp:permlink />" /></txp:asy_wondertag>

But that’s not working. I’m using <txp:asy_wondertag> around particular mem_form inputs, but around the actual mem_form tag it breaks. Is there an easier way to do this?

Thanks,

  • Ben

Offline

#19 2008-02-27 17:14:29

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: mem_form - Generic HTTP form processing

Try using the <txp:page_url /> tag instead of <txp:permlink />. The redirect attribute expects a location to the textpattern root directory, not a full URL.

Offline

#20 2008-02-27 17:46:54

benbruce
Plugin Author
Registered: 2006-01-13
Posts: 328
Website

Re: mem_form - Generic HTTP form processing

thanks for your help, Ruud. Using <txp:page_url /> got me a bit closer, but not there yet. Here’s the code I’m using:

<txp:asy_wondertag><txp:mem_form type="txphorum" redirect="<txp:page_url />" /></txp:asy_wondertag>

And here’s the error message I’m getting:

tag_error <txp:mem_form type="txphorum" redirect="/txphorum/not-logged-in" /> -> Textpattern Warning: Argument not specified for mem_form tag on line 103

which is coming from this section of the plugin:

	if (empty($type) or (empty($form) && empty($thing))) {
		trigger_error('Argument not specified for mem_form tag', E_USER_WARNING);
		return '';
	}

I should add that if I remove the <txp:ay_wondertag> and hardcode a redirect (redirect="/url") the form works fine. It’s something about the wondertag around the form function itself (because it works around individual form inputs inside the form no problem). Is there another way to get the page_url into the form variables?

Last edited by benbruce (2008-02-27 18:09:59)

Offline

#21 2008-02-27 18:20:42

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: mem_form - Generic HTTP form processing

Shouldn’t it be form="txphorum" instead of type="txphorum" ?

If that is some new feature in mem_form, then you could also do it like this:

<txp:php>echo mem_form(array('type' => 'txphorum', 'redirect' => page_url(array())));</txp:php>

Last edited by ruud (2008-02-27 18:23:41)

Offline

#22 2008-02-28 17:06:39

benbruce
Plugin Author
Registered: 2006-01-13
Posts: 328
Website

Re: mem_form - Generic HTTP form processing

ruud,

I didn’t have everything in a separate form, and that was my issue. Once I made the mem_form tag a self-closing tag (<txp:asy_wondertag><txp:mem_form form="txphorum" redirect="<txp:page_url />" /></txp:asy_wondertag>) it worked perfectly. Thank you,

  • Ben

Offline

#23 2008-02-28 23:16:48

Manfre
Plugin Author
From: North Carolina
Registered: 2004-05-22
Posts: 588
Website

Re: mem_form - Generic HTTP form processing

The “type” argument is used by plugins to determine which of the (potentially many) mem_form events they should process.

Offline

#24 2008-04-23 08:57:37

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

Re: mem_form - Generic HTTP form processing

I’m trying to validate that two date fields for an event are plausible, e.g. that the event begins in the future and that the end date is after the start date.

I have assembled a working function in a plugin for mem_form that works as desired returning the appropriate errors but with two remaining problems:

  1. The form is correctly redisplayed with the values previously entered but they are lost on resubmitting, e.g. when you correct your false date and resubmit. The form then returns a new set of errors saying that all the other values are missing.
  2. How do I add the isError class to the relevant fields so that they can be highlighted?

The relevant section of the form that is called from txp:mem_moderation_article_form is as follows (including format="..." for regex pattern to match YYYY-MM-DD date format):

<txp:mem_form_text		name="custom_2"	label="Begin Date" 
						required="1" break="" class="short" 
						example="(YYYY-MM-DD)" 
						format="/^(20[0-9]{2})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/" 
						/>

<txp:mem_form_text		name="custom_3"	label="End Date" 
						required="1" break="" class="short" 
						example="(YYYY-MM-DD)" 
						format="/^(20[0-9]{2})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/" 
						/>

The plugin function hooks into the mem_form.submit callback and is as follows:

// callback for validating date plausibility
register_callback('jcr_memform_validate_date','mem_form.submit');

// new function - date validation

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[] = 'The specified end date lies before the begin date. Please check your details.';
	    $isError = "errorElement";
	} 

	// if start date is in the past
	if ($begin_date < $todays_date ) {
	    $mem_form_error[] = 'The specified start date lies in the past. Please check your details.';
	    $isError = "errorElement";
	}
	return;
}

BTW: I’m using the svn versions of mem_moderation and mem_form from http://manfre.net/svn/txp/.

I’m sure I’m missing something simple that retains the form session. Any help much appreciated.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB