Elemental with forms

Silverstripe Version: 4.10

Question:

I’m looking for some pointers to get started with a form built using elemental (custom block).

My end goal is to make a form that submits using ajax, but I’d just be happy to have a working form for now.

I have the form setup in my block controller and the controller is defined in the block itself. This works fine. But when using the form function, nothing is returned and the function itself isn’t triggered.

Here’s my code:

Element Block


class CTABlock extends BaseElement
{
	private static $singular_name 	= 'Call to action';
	private static $description 		= 'Call to action bar with customised pop up form or link';
	private static $icon 				= 'font-icon-block-bell';
	private static $controller_class 	= CTABlockController::class; /** Class is defined here */
	private static $inline_editable 	= false;

Controller

protected function init()
    {
        parent::init();
    }

    public function ContactForm(HTTPRequest $request)
    {
        /** I've stripped a lot of it out, but you get the idea, hopefully! */

        $form = Form::create(
            $this,
            __FUNCTION__,
            FieldList::create(
                TextField::create('Name', 'Your Name')
            ),
            FieldList::create(
                FormAction::create('sendForm', 'Send')
                    ->setUseButtonTag(true)
            ),
            RequiredFields::create('Name')
        )->setTemplate('Forms/Form');

        return $form;
    }

Template

	<div class="modal fade" id="modalForm{$ID}" tabindex="-1" aria-labelledby="modalForm{$ID}Label" aria-hidden="true">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title" id="modalForm{$ID}Label">{$ButtonText}</h5>
					<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
				</div>
				<div class="modal-body">
					{$ContactForm}
				</div>
			</div>
		</div>
	</div>

So, my question is - firstly, any ideas why no form is actually returned?

Secondly, will this set up just work as expected given it’s elemental, or would it need something a bit different?

Thanks in advance for any help!