I’m trying to implement the sfCaptchaGDPlugin for Symfony 1.4.
I’ve created a CommentForm Class which extends the 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);
}
}
I can see the Captcha and enter a the Code. Now i’m trying to submit the Form via a AJAX request.
$('.btnsmall').click(function() {
$.post('<?php echo url_for('forum/saveComment') ?>', {
data: $('.form').serializeArray()
}, function(data) {
console.log(data);
});
return false;
});
the following action is looks like this:
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());
}
the result is the Following:
bool(true)
bool(false)
I was following this tutorial
The Problem was the Cross Site Protection…
Had to implement the csrf token via
$form[‘_csrf_token’];
Problem solved!
I rendered each
Formfieldseparate byecho $form['fieldname']. I forgot thecsrfToken. After I addedecho $form['_csrf_token']the Form passed the Validators inclusive the Captcha.Thanks for your help.