Search code examples
symfony1symfony-1.4

Calling "delete" action: I get _csrf_token [Required.] error


I generated the module "picture" and now I'm trying to call the action "delete" this way:

frontend_dev.php/picture/delete/id/1

But I get this error:

500 | Internal Server Error | sfValidatorErrorSchema

_csrf_token [Required.]

stack trace at () in SF_ROOT_DIR/lib/vendor/symfony/lib/validator/ sfValidatorSchema.class.php line 110 ...

$clean  = array();

$unused = array_keys($this->fields);

$errorSchema = new sfValidatorErrorSchema($this);

// check that post_max_size has not been reached

if (isset($_SERVER['CONTENT_LENGTH']) && (int) $_SERVER['CONTENT_LENGTH'] > $this-

getBytes(ini_get('post_max_size')))

at sfValidatorSchema->doClean(array('_csrf_token' => null)) in SF_ROOT_DIR/lib/vendor/symfony/lib/validator/ sfValidatorSchema.class.php line 90 ... */

public function clean($values)

{

return $this->doClean($values);

}

/** at sfValidatorSchema->clean(array('_csrf_token' => null)) in SF_ROOT_DIR/lib/vendor/symfony/lib/form/sfForm.class.php line 247 ...

Any idea?

Regards

Javi

sf 1.4


Solution

  • If you call delete action through "Delete" button in form or list, then you can try to update buggy function _method_javascript_function() in UrlHelper.php to this version

    function _method_javascript_function($method)
    {
        $function = "var f = document.createElement('form'); f.style.display = 'none';     this.parentNode.appendChild(f); f.method = 'post'; f.action = this.href;";
        $varFlag = false;
    
        if ('post' != strtolower($method))
        {
            $varFlag = true;
            $function .= "var m = document.createElement('input'); m.setAttribute('type', 'hidden'); ";
            $function .= sprintf("m.setAttribute('name', 'sf_method'); m.setAttribute('value', '%s'); f.appendChild(m);", strtolower($method));
        }
    
        // CSRF protection
        $form = new BaseForm();
        if ($form->isCSRFProtected())
        {
            $function .= ($varFlag ? '' : 'var ')."m = document.createElement('input'); m.setAttribute('type', 'hidden'); ";
            $function .= sprintf("m.setAttribute('name', '%s'); m.setAttribute('value', '%s'); f.appendChild(m);", $form->getCSRFFieldName(), $form->getCSRFToken());
        }
    
        $function .= "f.submit();";
    
        return $function;
    }