Alexander Schranz
Alexander Schranz

Reputation: 2450

Symfony form add field without prefix

I want one hidden field in my symfony form without an prefix. This field stores some information I needed to recreate the form type in my listener. I dont want to iterate over all $request parameters to get the right form so I want add simple field without any prefix. This need to be handled in the form builder, because the Frontend is not part of the bundle.

Current simplified code:

$builder->add('firstName'; TextType::class);
$builder->add('lastName'; TextType::class);
// ...
// field without prefix
$builder->add('someValue', HiddenType::class, ['mapped' => false]);

Expected result:

<input type="text" name="form_name[firstName]" />
<input type="text" name="form_name[lastName]" />
<!-- ... -->
<input type="hidden" name="someValue" /> <!-- without prefix -->

The thing is I need to access it with $request->request->get('someValue'). Because my form name is dynamically I cant access the array.

Is this possible?

Upvotes: 1

Views: 6577

Answers (2)

Dan Costinel
Dan Costinel

Reputation: 1736

Yes you can. Look here

Example:

use Symfony\Component\OptionsResolver\OptionsResolver;

class TaskType extends AbstractType
{
    // buildForm() method: add your fields here

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'      => 'AppBundle\Entity\Task',
            'csrf_protection' => true,
            'csrf_field_name' => 'formKey',
            // a unique key to help generate the secret token
            'csrf_token_id'   => 'task_item',
        ));
    }

    // ...
}

LE:

I dug a little to find out more about this problem. And I failed to find a proper way to override only the name attribute for a specific form field. So I end up with:

1. Add a new attribute name for that field, but you will end up with two name attributes, and I didn't test out to see which one will be used. I guess the first one.

{{ form_row(form.someValue, { attr:{ name:'someValue' } } }}

//This will look like:
 <input type="hidden" id="category_someValue" name="category[someValue]" name="someValue" />

And if you look at the source page, you'll see the last name attribute in red color. Not so good I guess.

2. Use, in your Type class, the getBlockPrefix() method, which will override the whole form prefix:

// AppBundle/Form/FormType.php
public function getBlockPrefix()
{
    return ''; // return an empty string here
}

And now remove the attr added for your field, and put just this:

{{ form_row(form.someValue) }}

But now all the form fields, will no longer have that form_name[first_name] like name attribute, but just name="first_name". So your hidden field will have: name="someValue".

But please, let us know if you find a better solution (ideally, the proper one).

Upvotes: 2

W0rma
W0rma

Reputation: 309

I don't know if this is possible (I guess it is not). Could your issue be fixed by using the getName() method of your FormType? Example:

// Controller
$form = $this->createForm(YourType::class, $yourObject);
// Get all parameters related to the form
$data = $request->request->get($form->getName());
// Output 'someValue'
echo $data['someValue'];

Upvotes: 0

Related Questions