Uniform custom action question

Happy Easter together!

I’ve created a custom action under plugins/uniform-actions.php for the uniform plugin. But whenever I test my Form it fails with an error saying Uniform\Actions\SaveFormData does not exist. What am I doing wrong?

Custom Action

<?php

namespace Uniform\Actions;

class SaveFormData extends Action
{
		/**
		 * Dump the form data.
		 */
		public function perform()
		{
			$form = $this->form->data();

			// Create a page title
			$data = array_merge($form, [
				'title' => $form['name'] . ' ' . $form['firstname'],
			]);

			$uid = 'contact/' . md5($data['email']);

			// Check if the page already exists
			// before trying to create it
			if (! page($uid)) {
				page()->create(
					$uid,
					'contact', // template name
					$data
				);

				// Store the page uid on a session
				// to be used on the success page
				$session->set('formSubmission', $uid);
			}
		}
}

Controller

<?php

    use Uniform\Form;

    return function ($kirby)
    {
        $form = new Form([
            'name' => [],
            'firstname' => [],
            'email' => [],
        ]);

        if ($kirby->request()->is('POST')) {
            $form->action(Uniform\Actions\SaveFormData::class)
            ->logAction([
                'file' => kirby()->roots()->site().'/messages.log'
            ]);        
        }

        return compact('form');
    };

You need a folder with an index.php in it for Kirby 3 plugins.

Perfect, it works now! :slight_smile:

Could yo please explain a little bit more? I can’t make it work

What do you have at the moment?

I’m getting the same error \Uniform\Actions\CsvLogAction does not exist.

I have a file named uniform-actions.php in the plugins folder. This is the content:

<?php

namespace Uniform\Actions;

class CsvLogAction extends Action
{
    public function perform()
    {
        try {
            $lista = array (
			    array('aaa', 'bbb', 'ccc', 'dddd'),
			    array('123', '456', '789'),
			    array('"aaa"', '"bbb"')
			);
			$file = $this->requireOption('file');
			$fp = fopen($file, 'w');

			foreach ($lista as $campos) {
			    fputcsv($fp, $campos);
			}

			fclose($fp);
        } catch (\Exception $e) {
            $this->fail($e->getMessage());
        }
    }
}

And in the controller I’m trying to use it with this:

if ($kirby->request()->is('POST')) {
    $form->emailAction([
        'to' => get('emailto'),
        'from' => get('emailfrom'),
    ])->csvLogAction([
        'file' => $formPage->root().'/registro2.csv',
    ]);
}

Thanks

Files in the plugins folder must be named index.php as already mentioned above: Uniform custom action question

yes, but wich file the one named uniform-actions.php? Should I put it inside a folder? I don’t get it, sorry

It works now. the folder must be named uniform-actions and the action code needs to be inside a index.php.

Thanks!

1 Like