Dynamic data in config.php

I’m using AJAX to send an email using Uniform, so my ‘to’ and ‘from’ are set in the config file. The ‘to’ email should be set in the panel, but it doesn’t seem to work as I expect.

$form->emailAction([
                'to' => $page('page')->emailField(),
                'from' => 'from@website-form.de',
            ]);

I also tried esc(), but still didn’t work (the form works fine if I simply write an email address here). Is it even possible to use data in a config file like this?
Thanks!

What does this do?

It might be better to store email addresses in the site.txt as an email-field (e.g. contactemail) and make it editable for your editors (or not) via the site.yml blueprint? Then you can retrieve it like this: $site->contactemail() in your form controller

FYI: $page is a variable that refers to the current page, you can’t use it as a function.

There is a page() helper function that takes the path to the page as parameter, e.g. page('blog') or page('notes/across-the-ocean'). So don’t mix this up.

And yes, you can set arbitrary options in your config

return [
  // some other options
  'my.wonderful.email' => 'me@example.com',
  // some more options
];

You can then fetch such options with option('my.wonderful.email').

Doh, just a syntax error. This works:

$form->emailAction([
    'to' => esc(page('kontakt')->emailField()),
    'from' => 'from@website-form.de',
]);

Thanks guys!

Hello all,

I am trying to do something similar (however a little more extended).

I too am using AJAX to send an email via Uniform.

This form is used within a template called “Location”. There are 50+ “Location” pages, each with a different recipient email for the form.

Ideally I would like to do something like the below, however I understand it is probably not possible.

config.php

$form->emailAction([
  'to' => $page->emailField()
]);

Is there any other way around this?

Thank you!

Why in config, that doesn’t make sense. This should be in your controller, just like in the example above.

Hi @texnixe, I am using a route to listen to a POST request like the example in AJAX - Kirby Uniform.

Because it is AJAX I thought I had to set this up in the config file, not the controller.

I see, in that case, you have to pass the page to your route somehow, either in the pattern or via an additional (hidden) post field.

Thank you. In the end I used the hidden post field

template

          <input 
            type="hidden"
            name="page" 
            value="<?= $page->uuid() ?>"
          />

config.php

'action' => function () {
                $form = new \Uniform\Form([
                    'page' => [],
                    'another' => []
                ]);

                // If validation and guards passed, execute the action.
                $form->emailAction([
                    'to' => [
                        // Get current page
                        esc(page(get('page'))->emailAddress())
                    ],
                    'from' => 'example@email.com',
                    'subject' => 'Subject of email',
                ]);
            }
        ]