Stoiko Nalbantov
Stoiko Nalbantov

Reputation: 107

Formhandler subject dynamic field

I have problem with the TYPO3 extension "Formhandler". I installed the extension, added captcha and everything is working.

This is the HTML template:

<!-- ###TEMPLATE_FORM1### begin -->
<form action="###REL_URL###" name="projektform" id="projektform" method="post" class="formhandler">
  <br />  
  <div id="sender_name">
    <label for="sender_name"><span style="color:red;">*</span>Name:</label>
    <br />
    <input type="text" name="formhandler[sender_name]" id="sender_name"
    value="###value_sender_name###" />
    ###error_sender_name###
  </div>
 <br />
 <div id="sender_email">
    <label for="sender_email"><span style="color:red;">*</span>Email:</label>
    <br />
    <input type="text" name="formhandler[sender_email]" id="sender_email"
    value="###value_sender_email###" />
    ###error_sender_email###
  </div>
  <br />
  <div id="sender_message">
    <label for="message"><span style="color:red;">*</span>Message:</label>
    <br />
    <textarea name="formhandler[message]" id="message">###value_message###</textarea>
    ###error_message###
  </div>
  <br />
  <!--###CAPTCHA_INSERT### this subpart is removed if CAPTCHA is not enabled! -->
  <div id="captcha">
    <label for="freecapfield"><span style="color:red;">*</span>###SR_FREECAP_NOTICE###</label>
    <br />
    ###SR_FREECAP_CANT_READ###
    <br />
    <div class="cap-img">
      ###SR_FREECAP_IMAGE###
    </div>
    <br />
    <input type="text" id="freecapfield" name="formhandler[freecapfield]" title="###SR_FREECAP_NOTICE###" value="">
    <br />
    ###error_freecapfield###
  </div>
  <!--###CAPTCHA_INSERT###-->  
  <br />
  <input type="submit" value="Submit" ###submit_nextStep### />
</form>
<!-- ###TEMPLATE_FORM1### end -->

<!-- ###TEMPLATE_SUBMITTEDOK### begin -->
<p>The following message has been sent:</p>
<p>###value_message###</p>
<!-- ###TEMPLATE_SUBMITTEDOK### end -->

<!-- ###TEMPLATE_EMAIL_ADMIN_PLAIN### begin -->
The following contact form has been sent to you:
Sender: ###value_sender_name### ###value_sender_email###
Text:
###value_message###
<!-- ###TEMPLATE_EMAIL_ADMIN_PLAIN### end -->

This is the typo script:

<INCLUDE_TYPOSCRIPT: source="FILE:fileadmin/contactform/1-contactform.ts">
plugin.Tx_Formhandler.settings {  
  debug = 1
  templateFile = fileadmin/contactform/1-contactform.html  
  formValuesPrefix = formhandler   
  finishers {    
    1 {      
      class = Tx_Formhandler_Finisher_Mail
    }    
    2 {      
      class = Tx_Formhandler_Finisher_SubmittedOK      
      config.returns = 1    
    }  
  }
  # Rules for the validation
  validators.1.class = Validator_Default
  validators.1.disabled = 0
  validators.1.config.fieldConf {
    message.errorCheck.1 = required
    message.errorCheck.2 = minLength
    message.errorCheck.2.value = 5
    sender_name.errorCheck.1 = required
    sender_email.errorCheck.1 = required
    sender_email.errorCheck.2 = email
    freecapfield.errorCheck.1 = srFreecap
  }
  # Layout if the error message
  singleErrorTemplate {
    totalWrap = |
    singleWrap = <span style="color: red;">|</span>
  }
}

So what I have is Name, Email, Message and a captcha fields, working perfectly. But then I wanted to add a "subject" field in the form, so that when someone sends an email from the online contact form, he would be able to set a subject of that email. I added an additional input field:

  <div id="subject">
      <label for="subject">Subject:</label>
    <br />
    <input type="text" name="formhandler[subject]" id="subject" value="###value_subject###"/>
  </div>

After adding the input in the HTML template, I entered the value "SUBJECT". The result was that I was able to see the value in the formhandler debugger:

The current GET/POST params are:
sender_name     NAME
sender_email    [email protected]
subject     SUBJECT
message     MESSAGE
freecapfield    kdlxp
step-2-next     1
submitted   1
randomID    5fab4cc19017c5c48dafb6a05ed7687b
removeFile  
removeFileField     
submitField     

Then all I needed to do was to "assign" that value to the "admin subject" field. I did a lot of researching and I was able to find the following code:

plugin.Tx_Formhandler.settings.predef.myformname {
  finishers {
    1.class = Tx_Formhandler_Finisher_Mail
    1.config {
      limitMailsToUser = 5
      admin {
        subject = TEXT
        subject.data = GPvar:formhandler|title 
      }
    }
  }
}

So I put the code in my typo script, substituting "myformname" with the name of my form "projektform" and title with the name of my input field "subject", but when I send an email, there is no subject. I did a lot of searching, tried a lot of examples, but the result was the same. Could you please point me to the right direction?

Upvotes: 3

Views: 3253

Answers (4)

t.h3ads
t.h3ads

Reputation: 1878

Assuming that your form really is based on a predef form with the key "projektform":

plugin.Tx_Formhandler.settings.predef.projektform {
    finishers {
        1.class = Tx_Formhandler_Finisher_Mail
        1.config {
            limitMailsToUser = 5
            admin {
                subject = TEXT
                subject.data = GP:formhandler|subject
                subject.sanitize = 1
            }
        }
    }
}

If you access GET/POST parameters using a cObject like "TEXT", you should always add "sanitize=1". Formhandler hooks into stdWrap and adds the submitted form data to the GET/POST array.

The better way to do it is the way @denvercoder suggested using just the name of the input field:

plugin.Tx_Formhandler.settings.predef.projektform {
    finishers {
        1.class = Tx_Formhandler_Finisher_Mail
        1.config {
            limitMailsToUser = 5
            admin {
                subject = subject
            }
        }
    }
}

Upvotes: 0

denvercoder
denvercoder

Reputation: 133

Or you could use the mechanism that is meant to do that for you:

In your template you specified the fieldname by name="formhandler[subject]". This means your value will be stored under the key "subject". Most, if not all, finishers are able to deal with this key/value pairs just like this:

plugin.Tx_Formhandler.settings {
  finishers {
    1 {
        class = Tx_Formhandler_Finisher_Mail
        config.admin.subject = subject
    }
  }
}

Also another reason why your code might not work is that you did not specify your form to use a predef and configured most of it outside a predef. But the config for the subject that you found uses predef. Just changing the predef name is not enough do associate the config with your form. It has to be on the same level as your other config. This might just work as well:

plugin.Tx_Formhandler.settings {
  finishers {
    1.class = Tx_Formhandler_Finisher_Mail
    1.config {
      limitMailsToUser = 5
      admin {
        subject = TEXT
        subject.data = GP:formhandler|subject 
      }
    }
  }
}

Sidenote: Even though it is okay not to use predef (because you have no need for multiple different forms) it is not recommended, you should consider changing your config to using predefs.

Upvotes: 0

Leon de Rijke
Leon de Rijke

Reputation: 158

The use of GPvar is deprecated, use GP instead:

subject.data = GP:formhandler|subject

More information can be found in this howto: How to access Formhandler values in TypoScript

Upvotes: 6

Related Questions