use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension; use Symfony\Component\Form\Forms; use Symfony\Component\HttpFoundation\Request; $formFactory = Forms::createFormFactoryBuilder() ->addExtension(new HttpFoundationExtension()) ->getFormFactory(); $form = $formFactory->createBuilder(FormType::class) ->add('name', TextType::class) ->add('save', SubmitType::class, array('label' => 'Submit')) ->getForm(); $request = Request::createFromGlobals(); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { // do something here }
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension; use Symfony\Component\Form\Forms; use Symfony\Component\HttpFoundation\Request; $formFactory = Forms::createFormFactoryBuilder() ->addExtension(new HttpFoundationExtension()) ->getFormFactory(); $form = $formFactory->createBuilder(FormType::class, null, array( 'method' => 'GET', 'action' => '/search', )) ->add('name', TextType::class, array( 'required' => true, )) ->add('age', ChoiceType::class, array( 'choices' => array( '18-25' => '18-25', '26-35' => '26-35', '36-45' => '36-45', ), )) ->add('gender', ChoiceType::class, array( 'choices' => array( 'Male' => 'male', 'Female' => 'female', ), )) ->add('submit', SubmitType::class, array('label' => 'Search')) ->getForm(); $request = Request::createFromGlobals(); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $data = $form->getData(); // do something here }This code creates a form with three fields "name", "age", and "gender". It also sets form options like method, action and choice options for the "age" and "gender" fields. It handles form submission and validation and retrieves form data. The package library used in these examples is "symfony/form".