将随机数拉成symfony形式

时间:2012-06-25 14:50:29

标签: php forms symfony-1.4 symfony-forms

我正在拉两个随机数来创建一个symfony形式的数学方程式。问题是提交表单时,随机数会更新,使表单提交无效。如何保持加载的初始数值,以便表单处理,但在成功处理后,加载一个新的集合?

actions.class

 public function executeIndex(sfWebRequest $request)
  {
    $num1 = rand(0 , 20);
    $num2 = rand(0 , 20);
    $realAnswer = $num1 + $num2;

    $show = $num1 . " + " . $num2 . " = ";

    $this->getResponse()->setSlot("displayNum", $show);

    $this->form = new UserForm();

    echo $num1 . "<br>" . $num2 . "<br>" . $realAnswer;

    if ($request->isMethod('post') && ($request->getPostParameter('captcha[answer]') == $realAnswer))
    {
        $this->processForm($request, $this->form);  
    }
  }

我使用部分渲染表单 - &gt;的 _form.php这个

<tr height="40">
        <td width="100" valign="middle">
            <?php echo get_slot("displayNum", "default value if slot doesn't exist"); ?>
        </td>
        <td width="400" colspan="4" valign="middle">
        <input type="text" id="captcha" class="url" name="captcha[answer]" style="width:100px" />
        </td>
    </tr>

示例:当页面最初加载时,会生成两个随机数(例如10和15)。这呈现 10 + 15 =(输入字段)

用户插入25并单击“保存”。这是正确的,但由于表单再次执行索引操作,因此有一组新的随机数使“if”条件为false。

更新:

根据j0k的建议,我已对行动进行了更改。

public function executeIndex(sfWebRequest $request)
    {
      $user = $this->getUser();

    if (!$request->isMethod('post'))
    {
      // first display of the form, generate nums
      $num1       = rand(0 , 20);
      $num2       = rand(0 , 20);
      $realAnswer = $num1 + $num2;

      // store them in session
      $user->setAttribute('realAnswer', $realAnswer);
      $user->setAttribute('num1', $num1);
      $user->setAttribute('num2', $num2);
    }
    else
    {
      // the form is submitted, retrieve nums from the session
      $num1       = $user->getAttribute('realAnswer', null);
      $num2       = $user->getAttribute('num1', null);
      $realAnswer = $user->getAttribute('num2', null);
    }

    //setup slot
     $show = $num1 . " + " . $num2 . " = ";
     echo $realAnswer . "-Actual<br>" . $request->getPostParameter('captcha[answer]') . "-User submitted";

    $this->form = new UserForm();

    if ($request->isMethod('post') && (($request->getPostParameter('captcha[answer]') == $realAnswer)))
    {
        $this->processForm($request, $this->form);  
    }
  }

哪个应该有效。查看变量,看起来页面正在拉取会话变量而不是在第二个帖子上添加新的随机数。怪异。

分辨

这是代码错误。我把变量交叉了。

else
    {
    // the form is submitted, retrieve nums from the session
     $num1       = $user->getAttribute('num1', null);
     $num2       = $user->getAttribute('num2', null);
     $realAnswer = $user->getAttribute('realAnswer', null);
../

2 个答案:

答案 0 :(得分:0)

您可以将随机数存储为会话变量,这些变量将在重新加载后保留:

session_start();
$_SESSION["num1"] = rand(0 , 20);
$_SESSION["num2"] = rand(0 , 20);

答案 1 :(得分:0)

会议是一个很好的观点。

如果表单未发布,请将结果存储在会话中以便能够在之后进行检查。

public function executeIndex(sfWebRequest $request)
{
  $user = $this->getUser();

  if (! $request->isMethod('post'))
  {
    // first display of the form, generate nums
    $num1       = rand(0 , 20);
    $num2       = rand(0 , 20);
    $realAnswer = $num1 + $num2;

    // store them in session
    $user->setAttribute('realAnswer', $realAnswer, 'captcha');
    $user->setAttribute('num1', $num1, 'captcha');
    $user->setAttribute('num2', $num2, 'captcha');
  }
  else
  {
    // the form is submitted, retrieve nums from the session
    $num1       = $user->getAttribute('num1', null, 'captcha');
    $num2       = $user->getAttribute('num2', null, 'captcha');
    $realAnswer = $user->getAttribute('realAnswer', null, 'captcha');
  }

  $show = $num1 . " + " . $num2 . " = ";

  $this->getResponse()->setSlot("displayNum", $show);

  $this->form = new UserForm();

  if ($request->isMethod('post') && ($request->getPostParameter('captcha[answer]') == $realAnswer))
  {
    $this->processForm($request, $this->form);
  }
}

如果表单有效,请不要忘记在会话中清空相关值。

protected function processForm(sfWebRequest $request, $form)
{
  // bind form

  if ($form->isValid())
  {
    // clear the session
    $this->getUser()->getAttributeHolder()->removeNamespace('captcha');
  }
}

Ps:如果您正在寻找透明验证码,我建议您this method。我测试了它非常成功。