$page or page('contact') does not work with Uniform

I would like to add the info contained in the page but it does not work.

I try with :

    $to    = $page->to();
    $from  = $page->from();
or
    $mailto  = page('contact')->mailto();
    $sender  = page('contact')->sender();

Or with return function ($page, $kirby) {
but it still does not work… :thinking:

My complete controller:

<?php

use Uniform\Form;

return function ($kirby) {

  $form = new Form([
    'name' => [
      'rules' => ['required'],
      'message' => 'Please enter a valid name',
    ],
    'email' => [
      'rules' => ['required', 'email'],
      'message' => 'Please enter a valid email address',
    ],
    'subject' => [],
    'message' => [
      'rules' => ['required'],
      'message' => 'Please enter a message',
    ],
  ]);

  if ($kirby->request()->is('POST')) {

    $to    = $page->to();
    $from  = $page->from();

    $form->emailAction([
      'to'       => $to,
      'from'     => $from,
      'subject'  => '{{subject}} - from {{name}}.',
      'template' => 'contact',
    ]);
  }

  return compact('form');

};

Try

return function ($kirby, $page) 

I already tried.
It does not work and does not send any error… (which is weird)
In addition, the mail is not sent and the function:

<?php if ($form->success()): ?>
<div class="alert success">
	<?= $page->success()->kt() ?>
</div>
<?php endif ?>

Is not displayed.

It’s possibly not working because $to and $from are Field objects. You may need to cast them to string:

    $to    = $page->to()->value();
    $from  = $page->from()->value();

It does not work… :disappointed_relieved:
Will this be a problem with kirby?

Have you tried it with @texnixe suggestion? Because you definitely need to make the $page variable available inside the function scope.

Are you submitting the form with the POST action?

Yes and yes.
I have no errors
That’s what’s weird…

Does it work if you hardcode the sender and recipient?

Does the controller receive the post request?

 if ($kirby->request()->is('POST')) {
  dump('yep, all good');
//...

Yes, and with this code it works:

    $form->emailAction([
      'to'       => mail@mailto.ltd,
      'from'     => mail@mailfrom.ltd,
      'subject'  => '{{subject}} - from {{name}}.',
      'template' => 'contact',
    ]);

If hard coding those values works, you could try:

    $to    = $page->to()->value();
    $from  = $page->from()->value();
    dump($to); dump($from); die();

What do you have on those variables?

It returns a blank page…

That’s all the files:

If the page is blank when you dump those fields, their values may be empty. Have you checked the fields in the contact text files inside the content folder?

I just emptied the cache and the code works:

    $to    = $page->to()->value();
    $from  = $page->from()->value();

I do not know what happened but thank you very much!

1 Like