Sylius Stack
  • Sylius Stack Documentation
  • Getting started
  • Cookbook
    • How to customize your admin panel
      • Basic operations
      • Customizing your grids
      • Customizing the logo
      • Customizing the menu
      • Configuring the security access
      • Customizing the page titles
    • How to use in a DDD architecture
      • Architecture overview
      • Resource configuration
      • Basic operations
      • Operation using a grid
  • Admin UI
    • Getting started
  • Bootstrap Admin UI
    • Getting started
  • Resource
    • Resource Bundle documentation
      • Installation
      • Create new resource
      • Configure your resource
      • Configure your operations
      • Validation
      • Redirect
      • Resource factories
      • Providers
      • Processors
      • Responders
      • Legacy Resource Documentation
        • Configuration
        • Services
        • Routing
        • Forms
        • Getting a Single Resource
        • Getting a Collection of Resources
        • Creating Resources
        • Updating Resources
        • Deleting Resources
        • Configuring a state machine
        • Configuration Reference
  • Grid
    • Grid Bundle documentation
      • Installation
      • Creating your first grid
      • Configuring Fields
      • Field types
      • Creating a custom Field Type
      • Creating a custom Action
      • Creating a custom Bulk Action
      • Filters
      • Creating a custom Filter
      • Advanced configuration
      • Configuration Reference
  • 🍀Twig Extra
    • Getting started
  • 🌱Twig Hooks
    • Getting started
    • Passing data to your hookables
    • Making your hookables configurable
    • Autoprefixing feature
    • Composable Layouts with a predictable structure
    • Advanced
      • Ergonomic work with hooks
      • Metadata objects
      • Multiple hooks inside a single template
      • Overriding hookables
Powered by GitBook
On this page
  • Custom Resource Form
  • Create a FormType class for your resource
  • Note
  • Register the FormType as a service
  • Warning
  • Configure the form for your resource
  1. Resource
  2. Resource Bundle documentation
  3. Legacy Resource Documentation

Forms

This section is deprecated. However, as of now, the Sylius E-Commerce project is still resorting to this configuration so you might want to check it out.

Have you noticed how Sylius generates forms for you? Of course, for many use-cases you may want to create a custom form.

Custom Resource Form

Create a FormType class for your resource

src/Form/Type/BookType.php
namespace App\Form\Type;

use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\FormBuilderInterface;

class BookType extends AbstractResourceType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Build your custom form, with all fields that you need
        $builder->add('title', TextType::class);
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'app_book';
    }
}

Note

The getBlockPrefix method returns the prefix of the template block name for this type.

Register the FormType as a service

Warning

the registration of a form type is only needed when the form is extending the AbstractResourceType or when it has some custom constructor dependencies.

app.book.form.type:
    class: App\Form\Type\BookType
    tags:
        - { name: form.type }
    arguments: ['%app.model.book.class%', '%app.book.form.type.validation_groups%']

Configure the form for your resource

config/routes/sylius_resource.yaml
sylius_resource:
    resources:
        app.book:
            classes:
                model: App\Entity\Book
                form: App\Form\Type\BookType

That's it. Your new class will be used for all forms!

PreviousRoutingNextGetting a Single Resource

Last updated 3 months ago

Go back to the documentation's index