php - How to generate absolute URL in custom form type? -
i have example custom form type:
namespace acme\simplebundle\form\type; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolverinterface; class exampletype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder->setaction(****-----generate-route-absolute-url-here-----****) ->add('email', 'text') ->add('send', 'submit'); } public function getname() { return 'example'; } public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'error_bubbling' => true )); } }
how can generate absolute url (like http://example.com/admin/check_form) route?
$this->generateurl()
doesn't work, becuse it's not controller, , $this->get('router')->generate()
doesn't work, don't know why.
there 2 ways it:
- register form type service , inject router in it: doc
pass action option when create form in controller:
$form = $this->createform(new formtype(), null, array('action' => $this->generateurl('your_route', array(/* route parameters */), true));
note must pass
true
last argument in order force router generate absolute url (as asked for symfony < 2.8). if you're using symfony3, useurlgeneratorinterface::absolute_url
instead of true suggested emilie in comments.
Comments
Post a Comment