Ascherer
Ascherer

Reputation: 8093

Overwriting Core Symfony2 Components

Alright, Gonna be honest. Dont like some of the decisions symfony decided to take with their core files, so im trying to overwrite them. For example

Symfony/Component/Form/Extension/Core/Type/FieldType.php

im Trying to change the name that gets rendered if a FormView has a parent because they do some cool string formating...

Im just trying to make it so the $fullName and $id are both $form->getName();

 public function buildView(FormView $view, FormInterface $form)
    {
        $name = $form->getName();

        if ($view->hasParent()) {
            $parentId = $view->getParent()->get('id');
            $parentFullName = $view->getParent()->get('full_name');

            // Custom Logic
            //$id = sprintf('%s_%s', $parentId, $name);
            //$fullName = sprintf('%s[%s]', $parentFullName, $name);
            $id = $form->getName();
            $fullName = $form->getName();
        } else {
            $id = $name;
            $fullName = $name;
        }

        $types = array();
        foreach ($form->getTypes() as $type) {
            $types[] = $type->getName();
        }

        $view
            ->set('form', $view)
            ->set('id', $id)
            ->set('name', $name)
            ->set('full_name', $fullName)
            ->set('errors', $form->getErrors())
            ->set('value', $form->getClientData())
            ->set('read_only', $form->isReadOnly())
            ->set('required', $form->isRequired())
            ->set('max_length', $form->getAttribute('max_length'))
            ->set('pattern', $form->getAttribute('pattern'))
            ->set('size', null)
            ->set('label', $form->getAttribute('label'))
            ->set('multipart', false)
            ->set('attr', $form->getAttribute('attr'))
            ->set('types', $types)
        ;
    }

Upvotes: 1

Views: 470

Answers (2)

Ascherer
Ascherer

Reputation: 8093

Built a helper function.

public function fixForm(\Symfony\Component\Form\FormView $form)
{
    foreach($form as &$child)
    {
        $name = $child->get('full_name');
        $id = $child->get('id');
        $matches = array();
        preg_match('/^(?<form>.+)\[(?<ele>.+)\]/', $name, $matches);
        if(isset($matches['ele']))
            $child->set('full_name', $matches['ele']);
        $matches = array();
        preg_match('/^(?<first>.+)_(?<second>.+)/', $id, $matches);
        if(isset($matches['second']))
            $child->set('id', $matches['second']);
    }
    return $form;
}

and its working. Just call this when you need to fix the form

Upvotes: 0

Ren&#233; H&#246;hle
Ren&#233; H&#246;hle

Reputation: 27325

I think what you can try is to build your own Form Type.

You can put your own Form Type to your Bundle that you have a clean structure something like ("TestBundle/Form/Type").

In this fieldtype you can make your changes you need.

How can I make a custom field type in symfony2?

Here is a helpfull post that shows hot to make a custom Field Type.

Its a short hint and i hope you find a good solution and can tell us if its working and how you solved it.

Upvotes: 1

Related Questions