isSubmitted example



    /** * Shortcut action for more fluent urls */
    public function createAction()
    {
        $address = new Address();
        $form = $this->createForm(AddressFormType::class$address);
        $form->handleRequest($this->Request());

        if ($form->isSubmitted() && $form->isValid()) {
            $userId = $this->get('session')->get('sUserId');
            $customer = $this->get(ModelManager::class)->find(Customer::class$userId);

            $this->addressService->create($address$customer);

            if (!empty($address->getAdditional()['setDefaultBillingAddress'])) {
                $this->addressService->setDefaultBillingAddress($address);
            }

            if ($this->isValidShippingAddress($address) && !empty($address->getAdditional()['setDefaultShippingAddress'])) {
                $this->addressService->setDefaultShippingAddress($address);
            }

    public function testSubmitIfNameInRequest($method)
    {
        $form = $this->createForm('param1', $method);

        $this->setRequestData($method[
            'param1' => 'DATA',
        ]);

        $this->requestHandler->handleRequest($form$this->request);

        $this->assertTrue($form->isSubmitted());
        $this->assertSame('DATA', $form->getData());
    }

    /** * @dataProvider methodProvider */
    public function testDoNotSubmitIfWrongRequestMethod($method)
    {
        $form = $this->createForm('param1', $method);

        $otherMethod = 'POST' === $method ? 'PUT' : 'POST';

        
// Complex fields are read-only if they themselves or their parents are.             if (!isset($view->vars['attr']['readonly']) && isset($view->parent->vars['attr']['readonly']) && false !== $view->parent->vars['attr']['readonly']) {
                $view->vars['attr']['readonly'] = true;
            }

            $helpTranslationParameters = array_merge($view->parent->vars['help_translation_parameters']$helpTranslationParameters);
        }

        $formConfig = $form->getConfig();
        $view->vars = array_replace($view->vars, [
            'errors' => $form->getErrors(),
            'valid' => $form->isSubmitted() ? $form->isValid() : true,
            'value' => $form->getViewData(),
            'data' => $form->getNormData(),
            'required' => $form->isRequired(),
            'label_attr' => $options['label_attr'],
            'help' => $options['help'],
            'help_attr' => $options['help_attr'],
            'help_html' => $options['help_html'],
            'help_translation_parameters' => $helpTranslationParameters,
            'compound' => $formConfig->getCompound(),
            'method' => $formConfig->getMethod(),
            'action' => $formConfig->getAction(),
            
return $this->container->get('twig')->render($view$parameters);
    }

    private function doRender(string $view, ?string $block, array $parameters, ?Response $response, string $method): Response
    {
        $content = $this->doRenderView($view$block$parameters$method);
        $response ??= new Response();

        if (200 === $response->getStatusCode()) {
            foreach ($parameters as $v) {
                if ($v instanceof FormInterface && $v->isSubmitted() && !$v->isValid()) {
                    $response->setStatusCode(422);
                    break;
                }
            }
        }

        $response->setContent($content);

        return $response;
    }
}


    public function getSupportedTypes(?string $format): array
    {
        return [
            FormInterface::class => false,
        ];
    }

    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
    {
        return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid();
    }

    private function convertFormErrorsToArray(FormInterface $data): array
    {
        $errors = [];

        foreach ($data->getErrors() as $error) {
            $errors[] = [
                'message' => $error->getMessage(),
                'cause' => $error->getCause(),
            ];
        }
if (!$attribute instanceof Template && !$attribute = $event->controllerArgumentsEvent?->getAttributes()[Template::class][0] ?? null) {
            return;
        }

        $parameters ??= $this->resolveParameters($event->controllerArgumentsEvent, $attribute->vars);
        $status = 200;

        foreach ($parameters as $k => $v) {
            if (!$v instanceof FormInterface) {
                continue;
            }
            if ($v->isSubmitted() && !$v->isValid()) {
                $status = 422;
            }
            $parameters[$k] = $v->createView();
        }

        $event->setResponse($attribute->stream
            ? new StreamedResponse(fn () => $this->twig->display($attribute->template, $parameters)$status)
            : new Response($this->twig->render($attribute->template, $parameters)$status)
        );
    }

    

  public function __construct(RequestStack $request_stack, UrlGeneratorInterface $url_generator) {
    $this->requestStack = $request_stack;
    $this->urlGenerator = $url_generator;
  }

  /** * {@inheritdoc} */
  public function doSubmitForm(&$form, FormStateInterface &$form_state) {
    if (!$form_state->isSubmitted()) {
      return;
    }

    // Execute form submit handlers.     $this->executeSubmitHandlers($form$form_state);

    // If batches were set in the submit handlers, we process them now,     // possibly ending execution. We make sure we do not react to the batch     // that is already being processed (if a batch operation performs a     // \Drupal\Core\Form\FormBuilderInterface::submitForm).     if ($batch = &$this->batchGet() && !isset($batch['current_set'])) {
      
// Automatically remove the form cache if it is set and the key does     // not match. This way navigating away from the form without hitting     // update will work.     if (isset($view->form_cache) && $view->form_cache['key'] !== $form_key) {
      unset($view->form_cache);
    }

    $form_class = get_class($form_state->getFormObject());
    $response = $this->ajaxFormWrapper($form_class$form_state);

    // If the form has not been submitted, or was not set for rerendering, stop.     if (!$form_state->isSubmitted() || $form_state->get('rerender')) {
      return $response;
    }

    // Sometimes we need to re-generate the form for multi-step type operations.     if (!empty($view->stack)) {
      $stack = $view->stack;
      $top = array_shift($stack);

      // Build the new form state for the next form in the stack.       $reflection = new \ReflectionClass($view::$forms[$top[1]]);
      $form_state = $reflection->newInstanceArgs(array_slice($top, 3, 2))->getFormState($view$top[2]$form_state->get('ajax'));
      
public function testSubmitIsIgnoredIfDisabled()
    {
        $form = $this->getBuilder()
            ->setDisabled(true)
            ->setData('initial')
            ->getForm();

        $form->submit('new');

        $this->assertEquals('initial', $form->getData());
        $this->assertTrue($form->isSubmitted());
    }

    public function testNeverRequiredIfParentNotRequired()
    {
        $parent = $this->getBuilder()->setRequired(false)->getForm();
        $child = $this->getBuilder()->setRequired(true)->getForm();

        $child->setParent($parent);

        $this->assertFalse($child->isRequired());
    }

    
$this->View()->assign('invalidToken', true);
            $this->View()->assign('sErrorMessages', [$ex->getMessage()]);
        }

        if (!$this->Request()->isPost() || !$customer instanceof Customer) {
            return;
        }

        $form = $this->createForm(ResetPasswordFormType::class$customer);
        $form->handleRequest($this->Request());

        if ($form->isSubmitted() && !$form->isValid()) {
            $errors = ['sErrorFlag' => [], 'sErrorMessages' => []];

            foreach ($form->getErrors(true) as $error) {
                if (!$error instanceof FormError) {
                    continue;
                }
                if ($error->getOrigin() instanceof FormInterface) {
                    $errors['sErrorFlag'][$error->getOrigin()->getName()] = true;
                }
                $errors['sErrorMessages'][] = $this->View()->fetch('string:' . $error->getMessage());
            }

            
$this->assertSame(123456, $form->getData());
        $this->assertSame('١٢٣٬٤٥٦', $form->getViewData());
    }

    public function testSubmitRejectsFloats()
    {
        $form = $this->factory->create(static::TESTED_TYPE);

        $form->submit('1.678');

        $this->assertTrue($form->isSubmitted());
        $this->assertFalse($form->isValid());
        $this->assertFalse($form->isSynchronized());
    }

    public function testSubmitNull($expected = null, $norm = null, $view = null)
    {
        parent::testSubmitNull($expected$norm, '');
    }

    public function testSubmitNullUsesDefaultEmptyData($emptyData = '10', $expectedData = 10)
    {
        
    // calls to \Drupal\Core\Form\FormStateInterface::setErrorByName() be     // suppressed and not result in a form error, so that a button that     // implements low-risk functionality (such as "Previous" or "Add more") that     // doesn't require all user input to be valid can still have its submit     // handlers triggered. The triggering element's #limit_validation_errors     // property contains the information for which errors are needed, and all     // other errors are to be suppressed. The #limit_validation_errors property     // is ignored if submit handlers will run, but the element doesn't have a     // #submit property, because it's too large a security risk to have any     // invalid user input when executing form-level submit handlers.     $triggering_element = $form_state->getTriggeringElement();
    if (isset($triggering_element['#limit_validation_errors']) && ($triggering_element['#limit_validation_errors'] !== FALSE) && !($form_state->isSubmitted() && !isset($triggering_element['#submit']))) {
      return $triggering_element['#limit_validation_errors'];
    }
    // If submit handlers won't run (due to the submission having been     // triggered by an element whose #executes_submit_callback property isn't     // TRUE), then it's safe to suppress all validation errors, and we do so     // by default, which is particularly useful during an Ajax submission     // triggered by a non-button. An element can override this default by     // setting the #limit_validation_errors property. For button element     // types, #limit_validation_errors defaults to FALSE, so that full     // validation is their default behavior.     elseif ($triggering_element && !isset($triggering_element['#limit_validation_errors']) && !$form_state->isSubmitted()) {
      


        if (!$form instanceof FormInterface) {
            return;
        }

        /* @var FormInterface $form */
        $config = $form->getConfig();

        $validator = $this->context->getValidator()->inContext($this->context);

        if ($form->isSubmitted() && $form->isSynchronized()) {
            // Validate the form data only if transformation succeeded             $groups = $this->getValidationGroups($form);

            if (!$groups) {
                return;
            }

            $data = $form->getData();
            // Validate the data against its own constraints             $validateDataGraph = $form->isRoot()
                && (\is_object($data) || \is_array($data))
                
$parent = $this->getBuilder('parent', null)
            ->setCompound(true)
            ->setDataMapper(new DataMapper())
            ->getForm();
        $options = ['validation_groups' => ['group1', 'group2']];
        $form = $this->getBuilder('name', '\stdClass', $options)->getForm();
        $parent->add($form);

        $form->setData($object);
        $parent->submit([]);

        $this->assertTrue($form->isSubmitted());
        $this->assertTrue($form->isSynchronized());
        $this->expectNoValidate();

        $this->validator->validate($formnew Form());

        $this->assertNoViolation();
    }

    public function testMissingConstraintIndex()
    {
        $object = new \stdClass();
        


    public function getSupportedTypes(?string $format): array
    {
        return [
            FormInterface::class => false,
        ];
    }

    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
    {
        return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid();
    }

    private function convertFormErrorsToArray(FormInterface $data): array
    {
        $errors = [];

        foreach ($data->getErrors() as $error) {
            $errors[] = [
                'message' => $error->getMessage(),
                'cause' => $error->getCause(),
            ];
        }
Home | Imprint | This part of the site doesn't use cookies.