我正在尝试为 Symfony 1.4 实现 sfCaptchaGDPlugin。
我创建了一个CommentForm
扩展BaseForm
.
class CommentForm extends BaseForm
{
public function configure()
{
$stars = array('star1', 'star2', 'star3', 'star4', 'star5');
$fields = array();
$fields['Subject'] = new sfWidgetFormInputText(array('default' => 'Bewertung'));
$fields['Voting'] = new btRatingStars(array('choices' => $stars), array('class' => 'star'));
$fields['Username'] = new sfWidgetFormInputText();
$fields['Captcha'] = new sfWidgetCaptchaGD();
$fields['Text'] = new sfWidgetFormTextarea();
$fields['ObjectId'] = new sfWidgetFormInputHidden();
$fields['Userid'] = new sfWidgetFormInputHidden();
$fields['Type'] = new sfWidgetFormInputHidden();
$this->setWidgets($fields);
}
}
我可以看到验证码并输入代码。现在我正在尝试通过 AJAX 请求提交表单。
$('.btnsmall').click(function() {
$.post('<?php echo url_for('forum/saveComment') ?>', {
data: $('.form').serializeArray()
}, function(data) {
console.log(data);
});
return false;
});
以下动作如下所示:
if ($request->isXmlHttpRequest())
{
$data = $request->getParameter('data');
$formdata = array();
foreach ($data as $field)
{
$formdata[$field['name']] = $field['value'];
}
$form = new CommentForm();
$form->setDefaults($formdata);
$form->setValidator('Captcha', new sfCaptchaGDValidator());
$form->bind(array(
'Captcha' => $formdata['Captcha'],
));
var_dump($form->hasErrors());
var_dump($form->valid());
}
结果如下:
bool(true)
bool(false)
我正在关注本教程
问题是跨站点保护...必须通过 $form['_csrf_token']; 实现 csrf 令牌;