Skip to content
Advertisement

Slim framework 4; return Formr-form

In Slim framework 4; How can I return a Formr-form in my controller as a response to a get-request?

$app->group('/timer', function (Group $group) {
    $group->get('/', function (Request $request, Response $response) {

        $form = new FormrFormr();

        // $form->create_form('Name')
        // die();
        $response->getBody()->write($form->create_form('Name'));
        return $response;
    });
});

The code outputs nothing. However, if I uncomment the two lines, it outputs (as expected):

<form action="/index.php" id="myFormr" method="post" accept-charset="utf-8">
<input type="hidden" name="FormrID" value="myFormr"><label for="name">Name</label> 
<input type="text" name="name" id="name" />


<button type="submit" name="button" id="button">Submit</button>


</form>

Advertisement

Answer

From Formr documentations:

Formr will automatically echo form elements and messages to the screen, and this is usually fine. However, in some instances – such as when using a templating framework – this is not an option. In these cases simply pass the word hush and then manually echo your elements and messages.

$form = new FormrFormr('bootstrap', 'hush');

The default value for the first parameter of FormrFormr constructor is an empty string, so in your case you should create new Formr instance with an empty string '' as the first parameter ann 'hush' as the second parameter:

$app->group('/timer', function (Group $group) {
    $group->get('/', function (Request $request, Response $response) {
        
        // required change
        $form = new FormrFormr('', 'hush');
             
        $response->getBody()->write($form->create_form('Name'));
        return $response;
    });
});
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement