symfony: Can't create a child form type of the SubmitType
In my HTML I want to create a button which has HTML as a label (button text) because I want to use the material-icons
. The problem is, that the SubmitType
doesn’t transform the HTML to HTML but just prints it onto the button, so I wanted to create a custom form type which inherits from the SubmitType
and renders the HTML. So I created a new FormType
<?php
namespace Acme\FrontendBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
/**
* Class HtmlSubmitType
*
* This form type is used for submit buttons which need HTML for the data (e.g. label). The html rendering is defined
* in the fields.html.twig file
*
* @category Form Type
* @package Acme\FrontendBundle\Form\Type
* @link http://www.example.com
* @author Theo Tzaferis <email@example.com>
*/
class HtmlSubmitType extends AbstractType
{
/**
* @inheritdoc
*/
public function getParent()
{
return SubmitType::class;
}
}
However, when using this form field in a FormType
which has this as the default data_class 'data_class' => ResolvedVendor::class
and adding it
->add(sprintf('quote_%s', $vendor->getToken()), HtmlSubmitType::class)
I’m always getting the error
Neither the property “quote_25v9nt1puvms” nor one of the methods “getQuote25v9nt1puvms()”, “quote25v9nt1puvms()”, “isQuote25v9nt1puvms()”, “hasQuote25v9nt1puvms()”, “__get()” exist and have public access in class “Acme\FrontendBundle\Model\Cart\ResolvedVendor”.
I don’t really understand it, since the documentation says
Here, the return value of the getParent function indicates that you’re extending the ChoiceType field. This means that, by default, you inherit all of the logic and rendering of that field type
and also it’s a submit button, why does it need to be mapped? Why does this error not get thrown when using the SubmitType
but when extending from it? I also can’t set the option mapped
to false, since it’s not a valid option for the SubmitType
which makes sense, but then why am I getting the error?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (16 by maintainers)
OK, found it. Buttons have a special handling in the Form tree. To tell the type resolver that your type creates a submit button rather than standard form element, you need to implement the
Symfony\Component\Form\SubmitButtonTypeInterface
marker interface on your form type, as far as I can tell. Can you try this ?You can customize
getBlockPrefix
Another solution is to extend the
SubmitType
using the PHP inheritance instead of the component’s one.