Connecting `new \Cake\View\ViewBuilder()` to the main `\Cake\Http\ServerRequest`
Asked Answered
C

1

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?

Cuckoo answered 8/4, 2021 at 16:10 Comment(2)
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?Agaric
@ahoffner, that was it, thank you! Had to pass $this->getRequest() as a second argument to viewBuilder::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.Stripe
A
1

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

Agaric answered 10/4, 2021 at 2:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.