PHP code example of inspiredminds / contao-backend-forms

1. Go to this page and download the library: Download inspiredminds/contao-backend-forms library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

inspiredminds / contao-backend-forms example snippets


// src/Controller/MyFormController.php
namespace App\Controller;

use Contao\CoreBundle\Controller\AbstractBackendController;
use InspiredMinds\ContaoBackendFormsBundle\Form\BackendForm;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('%contao.backend.route_prefix%/my-form', name: self::class, defaults: ['_scope' => 'backend'])]
class MyFormController extends AbstractBackendController
{
    public function __invoke(Request $request): Response
    {
        $form = new BackendForm('my-form', 'POST');

        $form->addFormField('lorem', [
            'label' => ['Lorem', 'Lorem description.'],
            'inputType' => 'text',
            'eval' => ['tl_class' => 'w50', 'maxlength' => 255],
        ]);

        $form->addFormField('ipsum', [
            'label' => ['Ipsum', 'Ipsum description.'],
            'inputType' => 'text',
            'eval' => ['tl_class' => 'w50', 'maxlength' => 255],
        ]);

        $form->addSubmitFormField('Submit');

        if ($form->validate()) {
            // Do something …

            return new RedirectResponse($request->getUriForPath($request->getPathInfo()));
        }

        return $this->render('my-form.html.twig', [
            'title' => 'My form',
            'headline' => 'My form',
            'form' => $form,
        ]);
    }
}