1

I'm rendering a view template into a variable via a new, standalone ViewBuilder.

$builder = new \Cake\View\ViewBuilder();
$builder->setLayout('my_layout');
$builder->setTemplate('my_template');

The above template contains a form.

echo $this->Form->create(null, array(
    'type' => 'POST',
    'url' => '/',
)).PHP_EOL;
$this->Form->unlockField('my_input');
echo $this->Form->end();

When submitted, it results in the below error.

'_Token' was not found in request data.

As far as I understand, the tokens aren't being added because the FormHelper receives false when it checks for $this->_View->getRequest()->getParam('_Token'), and that is because this new ViewBuilder isn't connected to the actual request the application operates on.

Is there a way to connect my new ViewBuilder to the main Cake\Http\ServerRequest of the application?

ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57
  • It's normally passed via $builder->build(), 2nd arg. You sure you need to be making a new View builder, rather than just rendering a Cell or Element inside the normal View's template? How are you rendering this now? – Andy Hoffner Apr 08 '21 at 20:57
  • @ahoffner, that was it, thank you! Had to pass `$this->getRequest()` as a second argument to [`viewBuilder::build()`](https://api.cakephp.org/4.2/class-Cake.View.ViewBuilder.html#build()). You're right that this needs refactoring, but will have to make it work in its current state anyway. Do you mind posting this as an answer? That way I can accept it and close the question. – ᴍᴇʜᴏᴠ Apr 09 '21 at 07:12

1 Answers1

1

You can pass it to $builder->build, it's the the 2nd argument, you can normally get it from $this->getRequest().

Andy Hoffner
  • 3,267
  • 2
  • 21
  • 22