Vacancy request form for each subpage

Now i have a new problem. After submitting the form the title disappears :frowning:

Any solution?

I don’t quite understand. the problem, which title on which page?

The title I get from the vacany on my form. It disappears after clicking the submit button …

I’m a bit late to the party, but isn’t it a better idea to have only 1 request form which has an URL parameter to match the vacancy so you can fetch that info in a controller?

E.g. :

  • loremipsum.tld/career
  • loremipsum.tld/career/request-form
  • loremipsum.tld/career/vacancytitle-a -> loremipsum.tld/career/request-form/v:vacancytitle-a
  • loremipsum.tld/career/vacancytitle-b -> loremipsum.tld/career/request-form/v:vacancytitle-b

Than the request form is a children from career, right?

And what is the best way to implement your example?

I would setup something like this: a template + controller to handle the form. Then I would use param() in that controller to get the passed parameter like this:

$vacancyUri  = param('v');

In the vacancy template I’ld use something like this to build the URL:

$url = $pages->find("career/request-form")->url() . "/v:" . $page->uri();

The form already works. It has an controller and a template.

I want to display the title from each vacancy on the page with the form, that is already working now but the title disappears when the form was submitted.

I used the code from @texnixe for that.

Can you please post your controller and template code? It’s hard to debug blind :wink:

Of course :smiley:

Template:

<?php snippet('header-raw') ?>

  <div class="container intro-request">
    <?php $path = kirby()->request()->path()->nth(2); ?>
    <?php if($vac = page('career/jobs')->children()->find($path)): ?>
      <h2><?php echo $vac->title(); ?></h2>
    <?php endif ?>
    <p class="sub"><?= $page->text()->html() ?></p>
  </div>

<div class="wrap">
  <form method="post" action="<?php echo $page->url() ?>" class="request">

    <input id="name" name="name" type="text" value="<?php echo $form->old('name') ?>" class="input" placeholder="Name">
    <input<?php if ($form->error('email')): ?> class="error"<?php endif; ?> name="email" type="email" value="<?php echo $form->old('email'); ?>"class="input" placeholder="E-Mail">
    <textarea<?php if ($form->error('message')): ?> class="error"<?php endif; ?> name="message" class="input" placeholder="Nachricht"><?php echo $form->old('message') ?></textarea>
    <input type="file" id="file" name="filefield" class="upload" required />
    <label for="file" class="input">Datei auswählen</label>
    <p aria-hidden="true" class="visually-hidden" id="pot">
      <?php echo csrf_field() ?>
      <?php echo honeypot_field() ?>
    </p>
    <input type="submit" value="Absenden" class="button button-big button-default" />
    <?php if ($form->success()): ?>
       <p>Danke für deine Bewerbung!<br/>
       Wir werden uns umgehend bei dir melden.</p>
    <?php else: ?>
      <?php snippet('uniform/errors', ['form' => $form]) ?>
    <?php endif; ?>
</form>

</div>

Controller:

<?php

use Uniform\Form;

return function ($site, $pages, $page) {

    $form = new Form([
        'email' => [
            'rules' => ['required', 'email'],
            'message' => 'E-Mail scheint ungültig zu sein.',
        ],
        'name' => [],
        'message' => [
            'rules' => ['required'],
            'message' => 'Nachricht fehlt.',
        ],
        'filefield' => [
            'rules' => [
                'required',
                'file',
                'mime' => ['application/pdf'],
                'filesize' => 6000,
            ],
            'message' => [
                'Please choose a file.',
                'Please choose a file.',
                'Please choose a PDF.',
                'Please choose a file that is smaller than 5 MB.',
            ],
        ],
    ]);

    if (r::is('POST')) {
        $to = '';
        $titel = page('job-request')->title();

        $form->emailAction([
            'to' => $to,
            'from' => $to,
            'subject' => 'Bewerbung von {name}',
            'service' => 'phpmailer',
            'service-options' => [
                 'attachment' => $_FILES['filefield'],
           ],
        ]);
    }

    return compact('form');
};

Try this code:

Template:

<?php snippet('header-raw') ?>

  <div class="container intro-request">
    <h2><?= $job->title(); ?></h2>
    <p class="sub"><?= $page->text()->html() ?></p>
  </div>

<div class="wrap">
  <form method="post" action="<?= $page->url() . "/v:" . array_slice(explode('/', $job->uri()), -1)[0]; ?>" class="request">

    <input id="name" name="name" type="text" value="<?php echo $form->old('name') ?>" class="input" placeholder="Name">
    <input<?php if ($form->error('email')): ?> class="error"<?php endif; ?> name="email" type="email" value="<?php echo $form->old('email'); ?>"class="input" placeholder="E-Mail">
    <textarea<?php if ($form->error('message')): ?> class="error"<?php endif; ?> name="message" class="input" placeholder="Nachricht"><?php echo $form->old('message') ?></textarea>
    <input type="file" id="file" name="filefield" class="upload" required />
    <label for="file" class="input">Datei auswählen</label>
    <p aria-hidden="true" class="visually-hidden" id="pot">
      <?php echo csrf_field() ?>
      <?php echo honeypot_field() ?>
    </p>
    <input type="submit" value="Absenden" class="button button-big button-default" />
    <?php if ($form->success()): ?>
       <p>Danke für deine Bewerbung!<br/>
       Wir werden uns umgehend bei dir melden.</p>
    <?php else: ?>
      <?php snippet('uniform/errors', ['form' => $form]) ?>
    <?php endif; ?>
</form>

</div>

Controller:

<?php

use Uniform\Form;

return function ($site, $pages, $page) {

    if (isset(param('v')) {
        $jobUri  = param('v');
        $job = $pages->find("career/jobs/" . $jobUri);
    } else {
        // no job passed via URL -> run your exception code here
    }

    $form = new Form([
        'email' => [
            'rules' => ['required', 'email'],
            'message' => 'E-Mail scheint ungültig zu sein.',
        ],
        'name' => [],
        'message' => [
            'rules' => ['required'],
            'message' => 'Nachricht fehlt.',
        ],
        'filefield' => [
            'rules' => [
                'required',
                'file',
                'mime' => ['application/pdf'],
                'filesize' => 6000,
            ],
            'message' => [
                'Please choose a file.',
                'Please choose a file.',
                'Please choose a PDF.',
                'Please choose a file that is smaller than 5 MB.',
            ],
        ],
    ]);

    if (r::is('POST')) {
        $to = '';

        $form->emailAction([
            'to' => $to,
            'from' => $to,
            'subject' => 'Bewerbung von {name}',
            'service' => 'phpmailer',
            'service-options' => [
                 'attachment' => $_FILES['filefield'],
           ],
        ]);
    }

    return compact('form', 'job');
};

:exclamation:️Please note that the code above will only work if you have setup your URL’s to your form as described in my initial post. There’s a lot of assumptions here (please check this with your content if the paths are correct):

  • Consider all the jobs to be in “career/jobs/”
  • Consider a vacancy: “career/jobs/job-a”
  • Consider the form to be at: “career/request”
  • Then the URL to get a working form should be: "career/request/v:job-a

You should be able to create the link from any job (e.g. “job-a”) to the request form with the following snippet in the “job template”:

<?= $pages->find("career/request")->url() . "/v:" . array_slice(explode('/', $page->uri()), -1)[0]; ?>

:exclamation:️Code is untested

1 Like

It doesnt find the form now. Only the 404 page is displayed :confused:

But i think it’s my fault.

EDIT

This is the error when i open the form:

Whoops\Exception\ErrorException thrown with message

"Cannot use isset() on the result of a function call (you can use "null !== func()" instead)"

and the URL is:/career/request/v:sap-entwickler

Please change

if (isset(param('v')) {

to

if (param('v') != null) {
1 Like

Thank you @bvdputte

Now it is working but the form has no error messages.

Any idea why the error messages dont work now?

That’s probably another issue :sweat_smile:

Ok. I’ll open a new topic for this.

Many thanks @bvdputte :heart_eyes: