Jump to content

Module want: form builder


apeisa

Recommended Posts

so far i get the spam field showed up and checked if ti was emty....so i handle the sendMail and save only if the spamfields are empty....

i dig deeper now to get the "hardcoded" field into the array setting for the module....;)

i like PW  because for my limited time i could even dig trought to find out how it works without look in 1xxxx lines of code!!

Thank you adrian and soma for the hints and push ^-^

        //testcheck
        echo $this->input->post->spam;

        $spamfield = $this->input->post->spam;

        if(empty($spamfield)) {
            echo "spam is blank";
        } else {
            echo "spam exists";
        };
Link to comment
Share on other sites

Great so far!

I've got it working with a visible field with label like "Show your are a human leaf this field blank"

and my check i could simply prevent the sending and saving of the form.

Next step will be adding a hidden field (honeypot) for double check to bots.

One Question about something that is not easy for me since i'm not good in "advanced security" things:

the Token from the $form is setting with the FormProcessor like:

<input type="hidden" value="6c7936d25985fbe72c8bfd7698f8c0a20e412c188ed20f504e99ecc4535232b6" name="TOKEN1942360421" id="_post_token">

I know this is build in CSRF protection.

One question on this: check this token the timestamp, too?

i mean simple spamprotection via a sessiontoken with timestamp from building the form to submitting the form...

Such a time validation would be a perfect additional spamprotection to honeypotfields.

I don't now if this is already build in the core token, or this is only a token to validate the indentity of the present form?

regards mr-fan

Link to comment
Share on other sites

So here i'm finished for now since i've not the time to get this even further...but it works and i share it.

(it is only hardcoded in the FormTemplateProcessor)

1. call the form like it is described

2. i've changed the __buildform() - to get a hidden field !!don't ignore the CSS

		// set a random name to the submit button so that this form doesn't process
		// any submissions without first receiving a rendered form. This isn't
		// necessary, but it may help to reduce automated spam to the form.
		$submit->name = "submit";
		$submit->value = 'Submit';

//Changes for better Spamprotection with a hidden additional field as a honeypot
		// create a text input a hide it via CSS .Inputfield_name2 display:none - see CSS example on the end of this module
		$field = $this->modules->get("InputfieldText");
		$field->label = "Name2";
		$field->attr('id+name','name2');
		$field->required = 0;
		$form->add($field); // append the field to the form
//end changes	
		
		// add the submit button the the form
		$form->add($submit);

so a field for a honeypot is added to the form - use in your frontend CSS

.Inputfield_name2 { display:none; }

post-2327-0-40573200-1410173071_thumb.pn

2. next step i added a check for a field called "quest" as additional spamdetection used as a "don't fill this field" check with a visible field!

post-2327-0-36094800-1410172929_thumb.pn

post-2327-0-71860500-1410172950_thumb.pn

3. check if the hidden or the visible field was empty and sendMail or save to a page....if fields are not empty show a error and do nothing

	/**
	 * Render a form or process it's input
	 *
	 * @return string Output of the form or success message upon completion.
	 *
	 */
	public function ___render() {

		if(!$this->contact->template) throw new WireException("You must specify a Template");

		$form = $this->buildForm();

		// if the form hasn't been submitted, then just return the rendered form.
		if(!$this->input->post->submit) return $form->render();

		// variable to hold our output, which we will return
		$out = '';

		// now we assume the form has been submitted.
		// tell the form to process input frmo the post vars.
		$form->processInput($this->input->post);

//Set the Spamfilter and check if field quest or spam are blank!
		$questfield = $this->input->post->quest;
		$spamfield = $this->input->post->name2;

		if(empty($spamfield) && empty($questfield)) {
			echo "spam is blank sending mail";

		// see if any errors occurred
		if(count($form->getErrors())) {
			// re-render the form, it will include the error messages
			$out .= $form->render();

		} else {
			// successful form submission, so populate the new page with the new values.
			foreach($form as $field) {
				$this->contact->set($field->name, $field->value);
			}

			if($this->email) $this->sendEmail($form);
			//if($this->parent) $this->savePage($form);

			$out .= $this->successMessage;

		}

		return $out;
		} else {
			echo "You dirty Bot!";
		}
	}

So a basic Spamprotection with two methods is working.

This could be more professional with optional/individual names for the fields, and hiding the spamfields in the mail but in lack of skills i leave this as it is for now...

regards mr-fan

Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

Hello! I'm having some trouble with the plugin. I've used Ryan's basic-form.php.txt file as a boilerplate and get the success message and all but it's seems i'm not receiving any mail to the linked email account. Tried 2 different accounts.

It's for a launched site and it would be unfortunate if sent mails got lost. Is there an easy way to save the form as a child page to the parent like with this code:

$form->parent = $page; // optional, saves form as page

I'm really stuck, been trying all night to get this to work. Help would be highly appreciated :)

Edit:

Solved it through changing from mail to wireMail and installing the Swift Mailer module.


mail($emailTo, $subject, $message, "From: $form[email]");

To

wireMail($emailTo, $form[email], $subject, $message);

Thanks!

Link to comment
Share on other sites

  • 1 month later...

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?

attachicon.gifbasic-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.

I'm sorry. I'm new on pw.

how to use basic-form.php?

I put basic-form.php under templates folder.

In back-end, added a new page called contact page, then, how to select this php template over here ? In the page settings, I could not see the contact form template in the Template select list

Link to comment
Share on other sites

  • 2 months later...

Hey Guys!

Thanks for the script Ryan, it works great! The only thing I tried to add is a file upload function. I tried to implement that from different scripts in to this one but with no avail :( Could anybody help me out? That would be greatly appreciated! This script I'm using is pretty much exactly the one in the quote below.

Thanks in advance,

Bram Wolf

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?

attachicon.gifbasic-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.

Link to comment
Share on other sites

  • 1 month later...

Hi there,

I got two questions regarding Ryan's Form: https://processwire.com/talk/topic/59-module-want-form-builder/page-3#entry8457

1. how can I display Multi Language Strings e.g. __("Your Name") in the rendered form?

// output the form
            echo <<< _OUT

            $error
            <form action="./" method="post">
            <label for="fullname">__("Your Name")</label><br />

            ....
</form>
_OUT;

2. I'd like to send Umlauts, right now they don't get sent properly "ÃÀÌ" etc. I see someone had a similar problem (https://processwire.com/talk/topic/59-module-want-form-builder/page-5#entry59150) but checking Ryan's code it should work already properly:

// encode values for placement in markup
            foreach($form as $key => $value) {
                $form[$key] = htmlentities($value, ENT_QUOTES, "UTF-8");
            }

thanks a lot, cheers, j

Link to comment
Share on other sites

1. how can I display Multi Language Strings e.g. __("Your Name") in the rendered form?

okay, found a solution for this, just assigned __("Your Name") to a variable like $name = __("Your Name") and placed the variable in the form markup

... now trying to find a solution for the Umlaut problem

Link to comment
Share on other sites

... me again :)

using Adrian's Module now https://processwire.com/talk/topic/59-module-want-form-builder/page-5#entry41431 and it works great

just wondering how the Form Markup is generated? I assume that's an internal PW Function right, since it's based on jQuery UI & uses FontAwesome

any ideas how to make the Send Button text or Tooltip notification translateable for a multi language page set-up? ...or how to alter the generated Markup, so that it's not a Tooltip but displayed like an error message like for an empty field instead?

jZHDPNw.jpg

thanks a lot, cheers, j

btw. where's the JS Tooltip coming from anyways? haven't included jQuery UI in the frontend

Link to comment
Share on other sites

btw. where's the JS Tooltip coming from anyways? haven't included jQuery UI in the frontend

Isn't that tooltip come from the default html5 validation? Try setting "novalidate" attribute on the form element (if possible).

Link to comment
Share on other sites

Hi tpr,

thanks

that's what I was assuming, but at the moment I have no idea how to alter the generated form markup

more important actually for me atm is how to make the send button multi lang

as I see it, the Submit button is added here (FormTemplateProcessor.module):

        // the inputfields don't already have a submit button, so we'll add one.
        $submit = $this->modules->get("InputfieldSubmit");

        // set a random name to the submit button so that this form doesn't process
        // any submissions without first receiving a rendered form. This isn't
        // necessary, but it may help to reduce automated spam to the form.
        $submit->name = "submit";
        $submit->value = 'Submit';

        // add the submit button the the form
        $form->add($submit);
Link to comment
Share on other sites

  • 4 months later...

Hi all

Can I override some values in FormTemplateProcessor?

This is one of my forms and it works fine.

$form = $modules->get('FormTemplateProcessor');
$form->template = $templates->get('family-form'); // required
$form->requiredFields = array(
                                                'family_firstname',
                                                'family_secondname',
                                                'family_thirdname',
                                                'family_familyname',
                                                'family_email',
                                                'family_city',
                                                'family_job',
                                                'family_mobile'
                                        );
// $form->email = 'your@email.com'; // optional, sends form as email
$form->parent = $page; // optional, saves form as page echo
echo $form->render(); // draw form or process submitted form

when add new page:

Can I use (family_firstname family_secondname family_thirdname family_familyname) in page title instead of date?

Can I change the default success message?

Can I change the Submit button value? for example "Add your info"

Thanks

Link to comment
Share on other sites

OK it didn't work this way but I find this https://processwire.com/talk/topic/2089-create-simple-forms-using-api/
And https://processwire.com/talk/topic/4788-creating-pages-via-api-but-show-error-when-duplicate/
 
test and working great
 

$out = '';
$form = $modules->get("InputfieldForm");
$form->action = "./";
$form->method = "post";
$form->attr("id+name",'family-form');

$field = $modules->get("InputfieldText");
$field->label = "Name 1";
$field->attr('id+name','family_firstname');
$field->required = 1;
$form->append($field); // append the field

$field = $modules->get("InputfieldText");
$field->label = "Name 2";
$field->attr('id+name','family_secondname');
$field->required = 1;
$form->append($field); // append the field

$submit = $modules->get("InputfieldSubmit");
$submit->attr("value","Subscribe");
$submit->attr("id+name","submit");
$form->append($submit);

if($input->post->submit) {
    $form->processInput($input->post);
        if($form->getErrors()) {
                $out .= $form->render();
        }else{
                $p = new Page();
                $p->template = 'family-form';
                $p->parent = wire('pages')->get('/family/');
                
                $p->name = date('y-m-d-H-i-s-u');
                $p->title = $form->get("family_firstname")->value." - ".$form->get("family_secondname")->value;
                $p->family_firstname = $form->get("family_firstname")->value;
                $p->family_secondname = $form->get("family_secondname")->value;
                $p->save();
                $out .= "<p>You submission was completed! Thanks for your time.";
        }
}
else {
           // render out form without processing
           $out .= $form->render();
    }
echo $out;

 
very beautiful :)

  • Like 2
Link to comment
Share on other sites

  • 1 month later...

I there.
I have been using the code posted by Diogo ( # 61) on this thread to create an edit form. It'has been working fine.

I had included pagefields on the form without problems. But Lately I tried to include in the form a page field that has a custom selector (not the php selector) and there it failled.

the field is displayed on the form but does not display the recorded value of the field and the drop down list doesn't display the pages I expected (the one I can see if I go to the editpage on the backend).

Any Idea how to fix this ???
thanks in advance

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...