php - Symfony3 create form without entity in type file -
i have created form in controller :
$data = array (); $formbuilder = $this->createformbuilder ( $data ); $formbuilder->add( 'field1', numbertype::class, array( 'constraints' => array( new notblank(), ), ) ) ->add ( 'field2', numbertype::class, array( 'constraints' => array( new notblank(), ) ...); $form = $formbuilder->getform ();
i trying put form creation in type file. did form not created , can't display form fields in view.i don't understand why.
#in controlcontroller $data = array (); $formbuilder= $this->createformbuilder(controltype::class, $data); $form = $formbuilder->getform (); #in controltype public function buildform(formbuilderinterface $builder, array $options) { $builder->add( 'field1', numbertype::class, array( 'constraints' => array( new notblank(), ), ) ) ->add ( 'field2', numbertype::class, array( 'constraints' => array( new notblank(), )....; }
edit 1 : have tried things tell me, still doesn't work. code looks :
#in controlcontroller $data = ['field1' => null, 'field2' => null]; $formbuilder= $this->createformbuilder(controltype::class, $data); $form = $formbuilder->getform (); return $this->render ( 'metestbundle:control:index.html.twig', array ( 'form' => $form->createview () ) ); #in controltype public function buildform(formbuilderinterface $builder, array $options) { $builder->add( 'field1', numbertype::class, array( 'mapped' => false, ) ) ->add ( 'field2', numbertype::class, array( 'constraints' => array( new notblank(), ), 'mapped' => false )) ->add ( 'save', submittype::class, array ( 'label' => 'control' )); } public function configureoptions(optionsresolver $resolver) { $resolver->setdefaults(array( 'data_class' => control::class, )); }
but error have options "field1", "field2" not exist. defined options are: "action", "allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "inherit_data", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "method", "post_max_size_message", "property_path", "required", "translation_domain", "trim", "upload_max_size_message", "validation_groups".
your controller code create form seems wrong.
the signature createformbuilder in controller defined like:
public function createformbuilder($data = null, array $options = array())
what want should be:
$data = ['field1' => null, 'field2' => null]; $form = $this->createform(controltype::class, $data); return $this->render ( 'metestbundle:control:index.html.twig', array ( 'form' => $form->createview () ));
edit: should not set data_class
if you're not gonna use data object. leave configureoptions
empty in case. if want use entity should pass instance of $data
instead of simple array.
Comments
Post a Comment