2011-02-25 52 views
1

我使用symfony 1.4.8。我想驗證碼添加到我的形式,我用SfExtraFormPlugin,我用Readme問題與SfExtraFormPlugin(ReCaptcha)

因此,根據自述,我加

$this->widgetSchema['captcha'] = new sfWidgetFormReCaptcha(array(
    'public_key' => sfConfig::get('app_recaptcha_public_key') 
)); 

$this->validatorSchema['captcha'] = new sfValidatorReCaptcha(array(
    'private_key' => sfConfig::get('app_recaptcha_private_key') 
)); 

我action.class

protected function processForm(sfWebRequest $request, sfForm $form) 
    { 
    $form->bind(

    $request->getParameter($form->getName()), 
    $request->getFiles($form->getName())    
        ); 
    if ($form->isValid()) 
    { 
     $voice_post = $form->save(); 

     $this->redirect('post/index', $post); 
    } 
    } 

比我必須添加到我的行動。類

$captcha = array(
    'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'), 
    'recaptcha_response_field' => $request->getParameter('recaptcha_response_field'), 
); 
$this->form->bind(array_merge($request->getParameter('contact'), array('captcha' => $captcha))); 

所以,我不知道如何做到這一點。

當我做

protected function processForm(sfWebRequest $request, sfForm $form) 
    { 

    $captcha = array(
    'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'), 
    'recaptcha_response_field' => $request->getParameter('recaptcha_response_field'), 
); 

    $form->bind(

      array_merge (

      $request->getParameter($form->getName()), 
      $request->getFiles($form->getName()), 
      array('captcha' => $captcha) 

        )); 
    if ($form->isValid()) 
    { 
     $voice_post = $form->save(); 

     $this->redirect('post/index', $post); 
    } 
    } 

我有一個錯誤

這種形式的多,這意味着需要提供一個文件陣列作爲bind()方法第二個參數。頁面中_form.php這個

驗證碼

<?php echo $form['captcha']->renderRow() ?> 

的源代碼:

<tr> 
    <th><label for="voice_post_captcha">Captcha</label></th> 
    <td> 
    <script type="text/javascript"> 
    var RecaptchaOptions = { 
    theme : 'clean', 
    lang : 'en' 
    }; 
    </script> 
    <script type="text/javascript" src="http://api.recaptcha.net/challenge?k=6LcS4MESAAAAADg7FPXAuPfIwvIeFS7LhSGEhUdb"></script> 
    <noscript> 
     <iframe src="http://api.recaptcha.net/noscript?k=6LcS4MESAAAAADg7FPXAuPfIwvIeFS7LhSGEhUdb" height="300" width="500" frameborder="0"></iframe><br /> 
     <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea> 
     <input type="hidden" name="recaptcha_response_field" value="manual_challenge" /> 
    </noscript> 
    </td> 
</tr> 

P.S對不起,我英文不好

回答

3

替換:

$form->bind(
    array_merge (
     $request->getParameter($form->getName()), 
     $request->getFiles($form->getName()), 
     array('captcha' => $captcha) 
    ) 
); 

With:

$form->bind(
    array_merge (
     $request->getParameter($form->getName()), 
     array('captcha' => $captcha) 
    ), 
    $request->getFiles($form->getName()) 
); 

bind函數的第二個參數需要是文件數組。 array_merge調用將驗證碼結果合併到表單值數組中。

+0

非常感謝你的回覆和解釋! – denys281 2011-02-25 20:58:51