this is the code excerpt in the controller
$service = $this->get('questions_service');
$form_question = $service->createQuestionForm($question, $this->generateUrl('create_question', array('adId' => $ad->getId())));
this is my function in service
public function createQuestionForm($entity, $route)
{
$form = $this->createForm(new QuestionType(), $entity, array(
'action' => $route,
'method' => 'POST',
));
$form
->add('submit', 'submit', array('label' => '>', 'attr' => array('class' => 'button button-question button-message')));
return $form;
}
The createForm()
function is an alias in Symfony's Controller class. You will not have access to it from within your service. You'll want to either inject the Symfony container into your service or inject the form.factory
service. For example:
services:
questions_service:
class: AppBundle\Service\QuestionsService
arguments: [form.factory]
and then in your class:
use Symfony\Component\Form\FormFactory;
class QuestionsService
{
private $formFactory;
public function __construct(FormFactory $formFactory)
{
$this->formFactory = $formFactory;
}
public function createQuestionForm($entity, $route)
{
$form = $this->formFactory->createForm(new QuestionType(), $entity, array(
'action' => $route,
'method' => 'POST',
));
$form
->add('submit', 'submit', array(
'label' => '>',
'attr' => array('class' => 'button button-question button-message')
));
return $form;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With