Uniform Contact form plugin

Hello.

I want to use the Uniform Contact plugin.

This plugin works fine for site/template/contact.php and site/controllers/contact.php, but not for other templates and controllers at all.

Should I do something wrong or am I supposed to fix it?

site/templates/projects.php


    <input<?php e($form->hasError('name'), ' class="erroneous"')?> type="text" name="name" id="name" value="<?php $form->echoValue('name') ?>" required placeholder="Name or Company 이름 혹은 회사명">

    <input name="email" type="email" value="<?php echo $form->old('email') ?>" required placeholder="E-mail 이메일">

    <input type="tel" name="phone" id="phone" value="<?php $form->echoValue('phone') ?>" placeholder="Phone Number 휴대폰 번호">

    <input type="text" name="inquiring_product" id="inquiring_product" value="<?php $form->echoValue('inquiring_product') ?>" placeholder="Inquiring Product 문의 제품">

    <input type="text" name="product_use" id="product_use" value="<?php $form->echoValue('product_use') ?>" placeholder="Product Use 제품 용도">

    <input type="text" name="quantity" id="quantity" value="<?php $form->echoValue('quantity') ?>" placeholder="Quantity 수량">

    <input type="text" name="qna" id="qna" value="<?php $form->echoValue('qna') ?>" placeholder="Q&A 기타 문의 사항">

    <?php echo csrf_field() ?>
    <?php echo honeypot_field() ?>
    <input type="submit" value="Send">
</form>

/site/controllers/project.php

<?php

return function ($page) {

$gallery = $page->images()->sortBy('sort', 'filename');

return [
    'gallery' => $gallery
];

};



use Uniform\Form;

return function ($kirby)
{
$form = new Form([
    'name' => [
        'rules' => ['required'],
        'message' => 'Name is required',
    ],
    'email' => [
        'rules' => ['required', 'email'],
        'message' => 'Email is required',
    ],
    'phone' => [
        'rules' => ['required'],
        'message' => 'Phone is required',
    ],
    'inquiring_product' => [
        'message' => 'Inquiring product is required',
    ],
    'product_use' => [
        'message' => 'Product use is required',
    ],
    'quantity' => [
        'message' => 'Quantity is required',
    ],
    'qna' => [
        'message' => 'Q&A is required',
    ],

]);

if ($kirby->request()->is('POST')) {
    $form->emailAction([
        'to' => 'sonahyong@gmail.com',
        'from' => 'sonahyong@gmail.com',
    ]);
}

return compact('form');
};

You form handling code is not within the controller return function, instead, you create a second return statement, which will be completely ignored, so you have to put your form definition within that first function statement and integrate what you return from that function

<?php
use Uniform\Form;

return function ($page) {

    $gallery = $page->images()->sortBy('sort', 'filename');

    $form = new Form([
        'name' => [
            'rules' => ['required'],
            'message' => 'Name is required',
        ],
        'email' => [
            'rules' => ['required', 'email'],
            'message' => 'Email is required',
        ],
        'phone' => [
            'rules' => ['required'],
            'message' => 'Phone is required',
        ],
        'inquiring_product' => [
            'message' => 'Inquiring product is required',
        ],
        'product_use' => [
            'message' => 'Product use is required',
        ],
        'quantity' => [
            'message' => 'Quantity is required',
        ],
        'qna' => [
            'message' => 'Q&A is required',
        ],

    ]);

    if ($kirby->request()->is('POST')) {
        $form->emailAction([
            'to' => 'sonahyong@gmail.com',
            'from' => 'sonahyong@gmail.com',
        ]);
    }

    return [
        'gallery' => $gallery,
        'form'    => $form,
    ];

};

On a side note: A placeholder is no replacement for input labels. To make your form accessible for users of assistive technology, consider using labels.

I modified the code as you said, but an error message appears. :cry:

<?php
use Uniform\Form;

return function ($page) {

$gallery = $page->images()->sortBy('sort', 'filename');

$form = new Form([
    'name' => [
        'rules' => ['required'],
        'message' => 'Name is required',
    ],
    'email' => [
        'rules' => ['required', 'email'],
        'message' => 'Email is required',
    ],
    'phone' => [
        'rules' => ['required'],
        'message' => 'Phone is required',
    ],
    'inquiring_product' => [
        'message' => 'Inquiring product is required',
    ],
    'product_use' => [
        'message' => 'Product use is required',
    ],
    'quantity' => [
        'message' => 'Quantity is required',
    ],
    'qna' => [
        'message' => 'Q&A is required',
    ],

]);

if ($kirby->request()->is('POST')) {
    $form->emailAction([
        'to' => 'sonahyong@gmail.com',
        'from' => 'sonahyong@gmail.com',
    ]);
}

return [
    'gallery' => $gallery,
    'form'    => $form,
];

};

You need to pass $kirby as an argument to the function

return function($page, $kirby)
1 Like

Thank you very much!! :heart_eyes: :heart_eyes: :heart_eyes: :heart_eyes: :smiling_face_with_three_hearts: :smiling_face_with_three_hearts: :smiling_face_with_three_hearts: