You need to sign in to do that
Don't have an account?
Add captcha for vf web2lead form
Greetings! i'm stuck trying to add the captcha to my visualforce page for web2lead... i'm not an expert with the code so i'll really appreciate some help.
I'm using this class for the captcha
001 public class reCAPTCHA {
002
003 /* Configuration */
004
005 // The API endpoint for the reCAPTCHA service
006 private static String baseUrl = 'https://www.google.com/recaptcha/api/verify';
007
008 // The keys you get by signing up for reCAPTCHA for your domain
009 private static String privateKey = 'your_private_key';
010 public String publicKey {
011 get { return 'your_public_key'; }
012 }
013
014 /* Implementation */
015
016 // Simple form fields for the example form
017 public String myName { get; set; }
018 public String myEmail { get; set; }
019
020 // Create properties for the non-VF component input fields generated
021 // by the reCAPTCHA JavaScript.
022 public String challenge {
023 get {
024 return ApexPages.currentPage().getParameters().get('recaptcha_challenge_field');
025 }
026 }
027 public String response {
028 get {
029 return ApexPages.currentPage().getParameters().get('recaptcha_response_field');
030 }
031 }
032
033 // Whether the submission has passed reCAPTCHA validation or not
034 public Boolean verified { get; private set; }
035
036 public reCAPTCHA() {
037 this.verified = false;
038 }
039
040 public PageReference verify() {
041 System.debug('reCAPTCHA verification attempt');
042 // On first page load, form is empty, so no request to make yet
043 if ( challenge == null || response == null ) {
044 System.debug('reCAPTCHA verification attempt with empty form');
045 return null;
046 }
047
048 HttpResponse r = makeRequest(baseUrl,
049 'privatekey=' + privateKey +
050 '&remoteip=' + remoteHost +
051 '&challenge=' + challenge +
052 '&response=' + response
053 );
054
055 if ( r!= null ) {
056 this.verified = (r.getBody().startsWithIgnoreCase('true'));
057 }
058
059 if(this.verified) {
060 // If they pass verification, you might do something interesting here
061 // Or simply return a PageReference to the "next" page
062 return null;
063 }
064 else {
065 // stay on page to re-try reCAPTCHA
066 return null;
067 }
068 }
069
070 public PageReference reset() {
071 return null;
072 }
073
074 /* Private helper methods */
075
076 private static HttpResponse makeRequest(string url, string body) {
077 HttpResponse response = null;
078 HttpRequest req = new HttpRequest();
079 req.setEndpoint(url);
080 req.setMethod('POST');
081 req.setBody (body);
082 try {
083 Http http = new Http();
084 response = http.send(req);
085 System.debug('reCAPTCHA response: ' + response);
086 System.debug('reCAPTCHA body: ' + response.getBody());
087 } catch(System.Exception e) {
088 System.debug('ERROR: ' + e);
089 }
090 return response;
091 }
092
093 private String remoteHost {
094 get {
095 String ret = '127.0.0.1';
096 // also could use x-original-remote-host
097 Map<String, String> hdrs = ApexPages.currentPage().getHeaders();
098 if (hdrs.get('x-original-remote-addr')!= null)
099 ret = hdrs.get('x-original-remote-addr');
100 else if (hdrs.get('X-Salesforce-SIP')!= null)
101 ret = hdrs.get('X-Salesforce-SIP');
102 return ret;
103 }
104 }
105 }
And here is my web2lead vf page
<apex:page standardController="Lead" extensions="myWeb2LeadExtension" title="Interesado en Contratar Servicios de MCM" showHeader="false" standardStylesheets="true">
<apex:composition template="{!$Site.Template}">
<apex:define name="body">
<apex:form >
<apex:messages id="error" styleClass="errorMsg" layout="table" style="margin-top:1em; background-color: #FF9300" />
<apex:pageBlock title="" mode="edit" id="sider2_header">
<apex:pageBlockButtons >
<apex:commandButton value="Enviar" action="{!saveLead}" />
</apex:pageBlockButtons>
<apex:pageBlockSection title="Solicitud de Información" collapsible="true" columns="3" >
<apex:inputField value="{!Lead.FirstName}" label="Nombre (s)" required="True" style="width: 300px; height: auto; background-color: #E0FFFF" styleclass="input"/>
<apex:inputField value="{!Lead.LastName}" label="Apellido (s)" required="True" style="width: 300px; height: auto; background-color: #E0FFFF" styleclass="input"/>
<apex:inputField value="{!Lead.Email}" required="True" style="width: 300px; height: auto; background-color: #E0FFFF" styleclass="input"/>
<apex:inputField value="{!Lead.Phone}" required="True" style="width: 300px; height: auto; background-color: #E0FFFF" styleclass="input"/>
<apex:inputField value="{!Lead.Company}" required="True" style="width: 300px; height: auto; background-color: #E0FFFF" styleclass="input"/>
<apex:inputField value="{!Lead.NumberOfEmployees}" Label="Número Aproximado de Empleados" style="width: 300px; height: auto; background-color: #E0FFFF" styleclass="input"/>
<apex:inputField value="{!Lead.Industry}" style="width: 300px; height: auto; background-color: #E0FFFF" styleclass="input" />
<apex:inputField value="{!Lead.Street}" style="width: 300px; height: auto; background-color: #E0FFFF" styleclass="input"/>
<apex:inputField value="{!Lead.City}" style="width: 300px; height: auto; background-color: #E0FFFF" styleclass="input"/>
<apex:inputField value="{!Lead.State}" style="width: 300px; height: auto; background-color: #E0FFFF" styleclass="input"/>
<apex:inputField value="{!Lead.PostalCode}" style="width: 300px; height: aut; background-color: #E0FFFF" styleclass="input"/>
<apex:inputField value="{!Lead.ProductoInteres__c}" style="width: Auto; height: auto; background-color: #E0FFFF" styleclass="input"/>
<apex:inputField value="{!Lead.ProveedoresVozActual__c}" label="Proveedor Actual de Voz" style="width: Auto; height: auto;background-color: #E0FFFF" styleclass="input"/>
<apex:inputField value="{!Lead.ProveedoresDatosActual__c}" label="Proveedor Actual de Datos" style="width: Auto; height: auto;background-color: #E0FFFF" styleclass="input" />
<apex:inputField value="{!Lead.Description}" Label="Comentarios" style="width: 300px; height: 600; background-color: #E0FFFF" styleclass="input"/> </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:define>
</apex:composition>
</apex:page>
So i don't know where i need to star!!!
What i would like to do is, when load the page, hide the send button, add the captcha to the form... if the captcha is correct, then show the Send button and send the information to salesforce, if the captcha is not valid then send a message to try again but without reload the page...
Again, thank you so much... and i'll really appreciate your help!!!
If you have followed the steps on the URL http://wiki.developerforce.com/page/Adding_CAPTCHA_to_Force.com_Sites then it should work fine.
Hiding the Send button can be little tricky because Recaptcha will re-render in its own panel only. But your other requirement will be fulfilled, it wont submit the form unless the captcha is correct.
i try to followed the article, but i'm not an expert with the code, maybe it's is working ok, but i don't know how or where i need to put the code of my web2lead form into the captcha page or how or where to put the code from captcha into my form... if you can help me i'll really appreciate !!!