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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.