I have a Service (DependencyInjection) that i create, and i use that on my controllers as:
$this->get("service_name")->someMethod()
I want to know how to use that on my Form classes.
Here my example form of class:
namespace Company\SampleBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class AnswerType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options) {
// I want use: $this->get("service") here, how can i use that?
$builder->add('answer', 'textarea');
}
public function getName() {
return 'answer';
}
public function getDefaultOptions(array $options) {
return array(
'data_class' => 'Company\SampleBundle\Entity\Answer',
);
}
}
Thanks
You can use the
$options
to achieve this. This implies you modify thegetDefaultOptions
accordingly.In your controller, when you call
createForm()
use the$options
argument, like this: