• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1510
  • Last Modified:

zend form with captcha problem: please help

Hi
i have a zend view helper that has a contact form within it. the form include a captcha field. i atach the code.

i echo the view helper and the form is there with the captcha. the form is submitted with ajax.

so in my controller i have the code for proccesing the form.

the problem:
there is no validation on the captcha, but all the other fields does get the validation. so if i fill the form correctly but do not enter any captcha or a wrong captcha the form is passing validation as if it was good.

i have no idea what else i can do. hope someone might help me.


the form that is a view helper:
<?php
class Zend_View_Helper_Contact extends Zend_View_Helper_Abstract
{
     public function contact()
    {
       $contactform = new Zend_Form();
       $contactform->setMethod('post')
        ->setName('contactform')
        ->setAction(Zend_Controller_Front::getInstance()->getBaseUrl() . '/emails/contactform')
        ->setAttrib('class','clean')
        ;

        

        //validators       
        // customize the errors
        $notEmpty = new Zend_Validate_NotEmpty();
        $notEmpty->setMessages(
        array(Zend_Validate_NotEmpty::IS_EMPTY => 'ÔéÓÔ ÐÙàÕ ÙÛÕÜ ÜÔÙÕê èÙç')
        );
        
        $alphanumeric = new Zend_Validate_Alnum();
        $alphanumeric->setMessages(array(
        Zend_Validate_Alnum::NOT_ALNUM => 'èç ÐÕêÙÕê ÕÞáäèÙÝ ÙêçÑÜÕ'
        ));
        
        $email = new Zend_Validate_EmailAddress();
        $email->setMessages(array(
        Zend_Validate_EmailAddress::INVALID_FORMAT => 'ÔÓÕÐÜ ÐÙàÕ êçã èÐÔ ÔÓÕÒÞÔ'
        ));
        
        
        
        $length = new Zend_Validate_StringLength(4,20);
        $length->setMessages(array(
        Zend_Validate_StringLength::TOO_LONG => 'ÔâèÚ ÐèÕÚ ÞÓÙ',
        Zend_Validate_StringLength::TOO_SHORT => 'ÔâèÚ çæè ÞÓÙ'
        ));
        
        //filters
        $allowedTags = array('br','b','i');
        
        $alnumfilter = new Zend_Filter_Alnum();
        $htmlentities = new Zend_Filter_HtmlEntities();
        $stringtrim = new Zend_Filter_StringTrim();
        $striptags = new Zend_Filter_StripTags($allowedTags);
        
        
        //create the elements
        //first name
        $contactform->addElement(new Zend_Form_Element_Text('name'));
        //its attributes
        $contactform->name->setLabel('éÝ:')
        ->setRequired(true)
        ->setAttrib('class','validate[required,length[4,20]]')
        ->addValidator($notEmpty,false)
        ->addValidator($length,false)
        ->addFilter($alnumfilter)
        ->addFilter($stringtrim)
        ;

        
        //email
        $contactform->addElement(new Zend_Form_Element_Text('email'));
        //its attributes
        $contactform->email->setLabel('ÓÕÐÜ:')
                    ->addFilter($stringtrim)
                    ->addValidator($notEmpty,false)
                    ->addValidator($email,false)
                    ->setRequired(true)
                    ->setDescription('example@example.com')
                    ;
        
        //phone
        $contactform->addElement(new Zend_Form_Element_Text('phone'));
        //its attributes
        $contactform->phone->setLabel('ØÜäÕß:')
        ->setRequired(true)
        ->setAttrib('class','validate[required]')
        ->addValidator($notEmpty,false)
        ->addFilter($stringtrim)
        
        ;
                    
                    
        //message
        $contactform->addElement(new Zend_Form_Element_Textarea('message'));
        //its attributes
        $contactform->message->setLabel('ÔÔÕÓâÔ:')
        ->addValidator($notEmpty,false)
        ->setAttrib('cols',20)->setAttrib('rows',10)
        ->setRequired(true)
        ->setAttrib('id','message')
        ->addFilter($striptags)
        ->addFilter($htmlentities)
        ->addFilter($stringtrim)
        ;
        

        

          $contactform->addElement(new Zend_Form_Element_Captcha('captcha',array(
          'label' => 'ÐàÐ ÛêÕÑ Ðê ÞÔ éÛêÕÑ ÑêÞÕàÔ',
          'required' => true,
          'captcha' => array(
          'captcha' => 'image',
          'wordLen' => 4,
          'font' => 'captcha/ARIAL.TTF',
          'fontSize' => 30,
          'imgDir' => 'captcha',
          'imgUrl' => '/captcha/',
          'timeout' => 300,
          'height' => 50,
          'fontSize' => 22,
          'DotNoiseLevel' => 0.5,
          'LineNoiseLevel' => 0.5,
          'Expiration' => 20   
          ))
      ));
      
      
      
        
                                        
        //submit button
        $contactform->addElement(new Zend_Form_Element_Submit('submit'));
        $contactform->submit->setLabel('éÜ×');
        
        $contactform->addDisplayGroup(
        array('name', 'email','phone','message','captcha','submit'), 
        'contacts',
        array('legend' => 'æÕè çéè')
        );
        
        return $contactform;
    }
}



the controller code:
    public function contactformAction()
    {
        
        
        $contactform = $this->view->Contact();
        $request = $this->getRequest();
  
               
            
                 if ($request->isXmlHttpRequest()) {
                  $this->_helper->layout->disableLayout();
                  $this->getHelper('viewRenderer')->setNoRender();
                  
                 
                  $response = $contactform->processAjax($request->getPost()); 
                  
                  
                  if ($response == 'true')
                  {
                     
                      
                      // $name = $contactform->getValue('name');
                     // $email = $contactform->getValue('email');
                     //$captchainput = $contactform->getValue('captcha[input]');
                     //echo $captchainput;
                     $this->getResponse()->setHeader('Content-type','application/json')->setBody($response);
                      
                        
                  }else{
                     $this->getResponse()->setHeader('Content-type','application/json')->setBody($response);
                    
                  }
                  

               
            }
        }

and the jquery:
	$("#contactform input#submit").click(function() {
		
		
		var name = $("#contactform input#name").val();
		var email = $("#contactform input#email").val();
		var phone = $("#contactform input#phone").val();
		var message = $("#contactform textarea#message").val();
		var captchainput = $("#contactform input#captcha-input").val();
		var captchaid = $("#contactform input#captcha-id").val();
		
		alert(captchaid);
		
		/*if("ul.errors"){
			$("ul.errors").remove();
			$("br").remove();
			}*/
		
			sendContactInfo(name,email,phone,message,captchainput,captchaid);
	        return false;
	   });


function sendContactInfo(name,email,phone,message,captchainput,captchaid)
{
	
	$.post(
		"/emails/contactform",
		{
		"name" : name,
		"email" : email,
		"phone" : phone,
		"message" : message,
		"captchainput" : captchainput,
		"captchaid" : captchaid
		},
		function(result){
			//alert(result.name);
			if(result != true)
			{
				for ( var i in result.name )
	   			 {
	       			
	   			 //alert( result.name[i] );
	   			 $("#contactform dd#name-element").append("<ul class='errors'><li>" + result.name[i] + "</li></ul><br/>");
	   			 
	   			 }
				for ( var i in result.email )
	   			 {
	       			
	   			 //alert( result.name[i] );
	   			 $("#contactform dd#email-element").append("<ul class='errors'><li>" + result.email[i] + "</li></ul><br/>");
	   			 
	   			 }
				for ( var i in result.phone )
	   			 {
	       			
	   			 //alert( result.name[i] );
	   			 $("#contactform dd#phone-element").append("<ul class='errors'><li>" + result.phone[i] + "</li></ul><br/><br/>");
	   			 
	   			 }
				for ( var i in result.message )
	   			 {
	       			
	   			 //alert( result.name[i] );
	   			 $("#contactform dd#message-element").append("<ul class='errors'><li>" + result.message[i] + "</li></ul><br/><br/>");
	   			 
	   			 }
				for ( var i in result.cpatchainput )
	   			 {
	       			
	   			 //alert( result.name[i] );
	   			 $("#contactform dd#captcha-element").append("<ul class='errors'><li>" + result.captachainput[i] + "</li></ul><br/><br/>");
	   			 
	   			 }
			}else{
				$('#contactform').hide();
				$('#resultmsg').append('<h3>êÕÓÔ âÜ ÔÓÕÐÜ. Ð×ÖÕè ÐÜÙÚ ÑÔçÓÝ ÔÐäéèÙ!</h3>')
			}
		},'json'
		

	);
}

Open in new window

0
derrida
Asked:
derrida
  • 7
  • 4
1 Solution
 
Richard QuadlingSenior Software DeverloperCommented:
'expiration', NOT 'Expiration'

"All of the above options may be passed to the constructor by simply removing the 'set' method prefix and casting the initial letter to lowercase: "suffix", "height", "imgUrl", etc."

http://framework.zend.com/manual/en/zend.captcha.adapters.html#zend.captcha.adapters.image


And can you show the HTML for the <form> tag so we can see that it is all tying together correctly.
0
 
derridaAuthor Commented:
hi
i attach the html that zend generate.
ron

<!-- contact form -->
<form id="contactform" enctype="application/x-www-form-urlencoded" method="post" action="/emails/contactform" class="clean"><dl class="zend_form">
<dt id="contacts-label">&nbsp;</dt><dd id="contacts-element"><fieldset id="fieldset-contacts"><legend>æÕè çéè</legend>
<dl>
<dt id="name-label"><label for="name" class="required">éÝ:</label></dt>
<dd id="name-element">
<input type="text" name="name" id="name" value="" class="validate[required,length[4,20]]"></dd>
<dt id="email-label"><label for="email" class="required">ÓÕÐÜ:</label></dt>
<dd id="email-element">
<input type="text" name="email" id="email" value="">

<p class="description">example@example.com</p></dd>
<dt id="phone-label"><label for="phone" class="required">ØÜäÕß:</label></dt>
<dd id="phone-element">
<input type="text" name="phone" id="phone" value="" class="validate[required]"></dd>
<dt id="message-label"><label for="message" class="required">ÔÔÕÓâÔ:</label></dt>
<dd id="message-element">
<textarea name="message" id="message" cols="20" rows="10"></textarea></dd>
<dt id="captcha-label"><label for="captcha-input" class="required">ÐàÐ ÛêÕÑ Ðê ÞÔ éÛêÕÑ ÑêÞÕàÔ</label></dt>
<dd>
<img width="200" height="50" alt="" src="/captcha/10149f01db736a887e042612bbe343b0.png"/><br/>
<input type="hidden" name="captcha[id]" value="10149f01db736a887e042612bbe343b0" id="captcha-id">
<input type="text" name="captcha[input]" id="captcha-input" value=""></dd>

<dt id="submit-label">&nbsp;</dt><dd id="submit-element">
<input type="submit" name="submit" id="submit" value="éÜ×"></dd></dl></fieldset></dd></dl></form><!-- end contact form -->

Open in new window

0
 
Richard QuadlingSenior Software DeverloperCommented:
Can you confirm (using FireBug and watching the AJAX call) to make sure that the JS side is sending the right information?

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
derridaAuthor Commented:
hi
yes it does. in firebug on the post tab i get the right info i entered the form.
 
ron
0
 
derridaAuthor Commented:
hi
and whe i alert the variables they all go well:

var captchainput = $("#contactform input#captcha-input").val();
var captchaid = $("#contactform input#captcha-id").val();

alert(captchaid);
ron
0
 
Richard QuadlingSenior Software DeverloperCommented:
The form generated captcha[id] and captcha[input].

You are sending, via AJAX, captchaid and captchainput.

These aren't the same things.

Try ...


      $.post(
            "/emails/contactform",
            {
            "name" : name,
            "email" : email,
            "phone" : phone,
            "message" : message,
            "captcha[input]" : captchainput,
            "captcha[id]" : captchaid
            },
...


(UNTESTED).

0
 
derridaAuthor Commented:
hi
 
first let me thank you, cause that was indeed the problem. now i do get true when the captcha is fine and i get an error when its not true.
but still there is one problem:
when there is a problem in firebug i get this in the json tab:
{"captcha":{"badCaptcha":"Captcha value is wrong"}}
and i get the error that result.captcha is undefined.
in my jquery i have this to show the error:
 

for ( var i in result.captcha )
{

$("#contactform dd#captcha-element").append("<ul class='errors'><li>" + result.captacha[i] + "</li></ul><br/><br/>");

}
 
ron
 
0
 
derridaAuthor Commented:
hi
i have done this for every error messages but it does not work for the captch.

for ( var i in result.message )
{

$("#contactform dd#message-element").append("<ul class='errors'><li>" + result.message[i] + "</li></ul><br/><br/>");

}
 
ron
0
 
derridaAuthor Commented:
hi
when i set validatoer error message i use:

$email = new Zend_Validate_EmailAddress();
$email->setMessages(array(
Zend_Validate_EmailAddress::INVALID_FORMAT => 'ÔÓÕÐÜ ÐÙàÕ êçã èÐÔ ÔÓÕÒÞÔ'
));
now when i try to set custom error messages for the captcha i get nothing like:
Zend_Validate_Captcha::BAD_CAPTCHA => 'this is bad captcha'
am i missing something here?
ron
0
 
derridaAuthor Commented:
hi
 
ok manage to fix the problem now i have just the problem of the custom message.
 
in any case. i want to thank you for your great help.
i needed it:)
 
best regards
 
ron
0
 
Richard QuadlingSenior Software DeverloperCommented:
Glad to have helped.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now