1

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?

2
  • While most things are possible this one will leave you tearing out your hair. The prefix is used to map posted data by handleRequest. You really can't just make it go away and expect the form component to work. I did not quite follow your use case but if it is a read only value that does not need to be processed by the form component that you might just emit the html code right in your twig template.
    – Cerad
    Commented Dec 7, 2016 at 14:42
  • @Cerad as said the frontend is not part of the bundle and I dont want that every form theme creator need to add this values everytime when they create a form. Commented Dec 7, 2016 at 14:51

2 Answers 2

2

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).

1
  • 1
    I dont want to integrate a csrf token I want just a hidden field without a "prefix". To access it with $request->request->get('someValue'). will rename the field to make it more clear. Commented Dec 7, 2016 at 14:04
0

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'];
1
  • The form is dynamically builded and needs $request->request->get('someValue'); to get created. The name is unknown of the form because its also dynamically. Commented Dec 7, 2016 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.