Jump to content

Module want: form builder


apeisa

Recommended Posts

What about http://processwire.com/tutorials/quick-start/forms-the-long-way/

****

???

I install and read the help files for the "form builder module"

but i'm still a bit confused :

* // help file Usage:

*

* 1. In admin, create the fields you want to be part of the form.

OK

* 2. Create a new template and assign your fields to this template.

OK but do i need evey other fields ?- i would say yes..

So that would be a normal template like basic pages + new fields for the form..

* 3. Create another template for your contact form page (if you don't already have one).

That's confusing..

A new template again ? then it's written (if you don't already have one)

How many do I need then !!? ;-)

* 4. Use the example below as a starting point for this contact form page:

(ok thanks Ryan as usual)

Cause i just want an easy way to make contact forms at the end..

with just some different kind of fields depending of the website..

A walkthrought/tutorial would really be great ****

or at least to explicit the number 2 and 3.

Regards,

Link to comment
Share on other sites

Alchime, I actually have not tried out that tutorial yet, so need to take a closer look at that. However, attached is a template file (basic-form.php) that is ready to use as a basic contact form with the default site profile. Let me know if this is helpful?

basic-form.php.txt

Note that you'll have to rename this to basic-form.php, place in /site/templates/. If you want it to email to a specific address (rather than the superuser) than edit the file and enter your email address in at the top where it says to.

  • Like 3
Link to comment
Share on other sites

Thanks Ryan it does help ! (and it works nicely enought - i just translate the form in french..)

I'll might try to mix it with the code from the "form builder" because i like the way that email are checked..

Hum.. what about Captcha ? Any thought of that matter ?

Regards,

M.

P.S : Have a really great day !

Link to comment
Share on other sites

If you want a really simple captcha that's very effective, add a dummy field near the top of your form like this:

<textarea id='my_message' name='my_message'></textarea>

Then have your CSS stylesheet hide it:

#my_message { display: none; }

Then on your form processing, add this check:

if($input->post->my_message) {
   // message is very likely spam so skip or send it to your spambox
   mail('your.spam.email@company.com', $subject, $message); 
} else {
   // message is good
   mail('your.email@company.com', $subject, $message); 
}

Other captcha solutions would be to add a "What is 5+4?" and verify that they entered 9 (using a method like above). Or add in a captcha service like Recaptcha.

  • Like 1
Link to comment
Share on other sites

A super-simple one I employed on one of my forums was a Q&A. The question was really simple, such as "What colour is red" and required users to type the word "red". Simply having an extra field throws off a lot of spam bots especially in more common forms where they're used to a standard number of fields, and when they eventually broke through this first tricky type of question on the forum in question I was able to simply change the question to something equally simple.

There was something very satisfying about changing the question just after they'd worked out the last one - I know it's probably all automated but I like to think I've frustrated a human spammer nonetheless ;)

Link to comment
Share on other sites

you don't really have to get both words right, you know? One of the words is there because you are helping google to scan thousands of old books.

* thought it would be interesting for those that don't know it ;)

Link to comment
Share on other sites

  • 2 months later...

I was working over this code from apeisa http://processwire.c...t__20#entry1364 and wanted to share the result with you all.

Basically, this allows you to mirror the admin form from a given page to the frontend.

To give some flexibility, you can fill an array with the fields to exclude —or else, the fields to include— on the output.

EDIT: I forgot to tell... this works well with regular fields (text, textareas, checkboxes, radios, multiple choice, etc), but didn't manage to make it work well with images, for instance.

So, here is the code:

<?php

// Get the page you need to edit
$mypage = $pages->get('/some/page/');

// Populate with the names of the fields you want to exclude OR include (see instructions below)
// Leave empty to output all the fields
$myfields = array('body', 'email');
$form = $modules->get('InputfieldForm');
$fields = $mypage->getInputfields();

// If array is not empty use it to filter the fields
if ($myfields){
foreach($fields as $f){
	// Output all the fields minus the ones listed in the $myfields array
	// Instead, to output only the fields that are in the array, remove the (!) from the condition
	if (!in_array($f->name, $myfields)){
		$form->append($f);
	}
}
}

// Else, include all the fields
else {
$form->append($fields);
}

// Add save button
$field = $this->modules->get('InputfieldSubmit');
$field->attr('id+name', 'submit_save');
$field->attr('value', 'Save');
$field->label = "submit herei";
$form->append($field);

// Process the form
// (code replaced by a new one provided by Ryan)
if($input->post->submit_save) {
$form->processInput($input->post);
if(!$form->getErrors()) {
	$mypage->of(false); // turn off output formatting before setting values
	foreach($mypage->fields as $f) {
		$mypage->set($f->name, $form->get($f->name)->value);
	}
}
}

include("./head.inc");

// Render the form
echo $form->render();

include("./foot.inc");
Edited by diogo
  • Like 6
Link to comment
Share on other sites

Looks good Diogo, thanks for posting. Only thing I wanted to mention is that it may be preferable to get the field value from the Inputfield rather than directly from $input->post. That's because the inputfield may perform some of it's own validation, providing you with a safer value. Though fieldtypes do some validation too, so your method isn't bad either. But getting values from the Inputfields themselves is preferable in many cases. Especially if you wan to display the form again with populated values, like if there's a missing required field or something. So your form processing could look something like this:

if($input->post->submit_save) {
   $form->processInput($input->post); 
   if(!$form->getErrors()) {
       $mypage->of(false); // turn off output formatting before setting values
       foreach($mypage->fields as $f) {
           $mypage->set($f->name, $form->get($f->name)->value); 
       }
   }
}
Link to comment
Share on other sites

However, attached is a template file (basic-form.php) that is ready to use as a basic contact form with the default site profile.

Just so I don't get this mixed up: This example template is sort of a "standalone solution" for contact forms, right? It works without the the FormTemplateProcessor module?

Edit: Yes, it does. Might not be the ideal solution for anyone, but works just fine for most of my purposes. (Although a form builder would still be a neat feature.) :)

Link to comment
Share on other sites

  • 2 months later...

Hello everyone,

Interesting thread! I am trying the same thing as Diogo but I need one file upload to work ... I'm getting this error

Error Call to a member function path() on a non-object (line 90 of /wire/modules/Fieldtype/FieldtypeFile.module)

Can someone help me make this FieldtypeFile work outside of the admin?

Thanks,

thomas

Edit: I almost got it. /wire/modules/Process/ProcessPageEdit/ProcessPageEdit.module holds all the secrets :)

Link to comment
Share on other sites

Can someone help me make this FieldtypeFile work outside of the admin?

I'm not sure there's enough context here to tell for sure, but the page where you are trying to add a file, try calling $page->of(false); just to make sure that the page in a state where it is ready to be saved. Also double check that the field you are calling on that page actually is a valid field name for the page. Beyond that, we might need a more specific example so I can attempt to duplicate myself.

Link to comment
Share on other sites

Thanks Ryan,

my form works so far, the only thing missing is "create page" in the InputfieldPageAutocomplete.module. Adding existing pages works fine, but creating one and adding it throws this:

Error Exception: Unknown Selector operator: '' -- was your selector value properly escaped? (in /wire/core/Selectors.php line 165)

#0 /wire/core/Selectors.php(190): Selectors->create('', '', '-1')
#1 /wire/core/Selectors.php(63): Selectors->extractString(-1)
#2 /wire/core/Pages.php(132): Selectors->__construct(-1)
#3 [internal function]: Pages->___find(-1, Array)
#4 /wire/core/Wire.php(269): call_user_func_array(Array, Array)
#5 /wire/core/Wire.php(229): Wire->runHooks('find', Array)
#6 /wire/core/Pages.php(202): Wire->__call('find', Array)
#7 /wire/core/Pages.php(202): Pages->find(-1, Array)
#8 /wire/core/Pages.php(217): Pages->findOne(-1)
#9 /wire/modules/Inputfield/InputfieldPageAutocomplete/InputfieldPageAutocomplete.module(78): Pages->get(-1)
#10 [internal function]: Inputf

The same field works fine in admin.

Any hints?

Link to comment
Share on other sites

That particular fieldtype (autocomplete) probably can't work on the front-end because it depends on an Ajax service from ProcessPageSearch. It might be possible to get it working if you are actually logged into PW admin at the time, but not sure if this is useful if you are trying to make it front-end.

Link to comment
Share on other sites

Actually it is, because I want to restrict the frontend form to registered users with a certain role anyway. Any idea why the whole select and save works, as well as creating a new page, but creating one and then saving with it selected fails?

Thanks,

thomas

Link to comment
Share on other sites

I'm not sure initially. Do you see any javascript errors or anything like that in your browser developer console? Fieldtypes like the Repeater and Autocomplete weren't ever really intended to be used outside of ProcessPageEdit, so it's possible there's some dependency in there that I'm not thinking of. It seems like the most likely possibility is a missing JS file or something which may turn up in the JS console, but hard to tell for sure.

Link to comment
Share on other sites

Not sure what I changed but it works now! Only setTrackChanges produces wild results. Maybe it is because I don't really understand it ...?

if($input->post->submit_save) {
$edit->setTrackChanges();
 $form->processInput($input->post);
 foreach($form->children() as $feld) {
  $edit->set($feld->name, $feld->value);
 }
 $edit->save();
 foreach(array_unique($edit->getChanges()) as $change) {
   $session->message(sprintf($this->_('Change: %s'), $change));
  }

When I trackChanges on $edit (which is the page that's being edited) I get loads of message but never one of a field that was actually changed. If I trackChanges on $form, I don't get any messages at all. I guess it has to do with the order but I just can't seem to get it right ...

Thanks for you help Ryan!

-thomas

Link to comment
Share on other sites

I don't think that you should need setTrackChanges. This is something that the core automatically turns on for every page it loads, so you don't need to do that unless you've got some other code elsewhere that did a setTrackChanges(false). There's also no reason to do $form->setTrackChanges(). So I'm thinking you should just remove any reference to a setTrackChanges function.

Your form may be nested, in which case your code example is only going to handle the first level of fields. Try replacing your $form->children() with $form->getAll(), which should give you all the form fields flattened.

Link to comment
Share on other sites

  • 2 weeks later...

Hi ryan

I'm using this to send a form to an email. I've got a couple of fields that are textareas, but I'd like the email to include the newlines I enter in the textareas - currently it seems to strip them out and put them all on one line.

Is there a way to have it preserve newlines after submit?

Link to comment
Share on other sites

  • 5 months later...

Diogo's code above taught me a lot and saved me lots of time, but I just noticed one tweak that I think should be made:

foreach($mypage->fields as $f) {
	$mypage->set($f->name, $form->get($f->name)->value);
}

should really be:

foreach($mypage->fields as $f) {
       if (!in_array($f->name, $myfields)){
		$mypage->set($f->name, $form->get($f->name)->value);
        }
}

so as to avoid "Notice: Trying to get property of non-object" errors on any fields that were excluded.

Also, I had to include:

$mypage->of(false); // turn off output formatting before setting values
$mypage->save();

I don't know whether this is something specific to my use, or whether it's just obvious and so wasn't included, but though it might help someone else.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...