1

I was searching for a solution for this issue for some time. I found a lot of questions on Stackoverflow and tried almost all but it is not helping me with my problem.

I've a form in the controller and twig view which will render the form. I also have an entity for validations.

/**
 * @Route("/buy", name="buy")
 */
public function buyAction(Request $request)
{
    $priceArray = $this->container->getParameter('price');
    $options = [];
    foreach ($priceArray as $billingCycle => $array) {

        $options[$array['description'].' $'.$array['price']] = $billingCycle;
    }

    $orderData = new OrderData();
    // $orderData->setNickName('Enter Server NickName');

    $form = $this->createFormBuilder($orderData)
        ->add(
            'billingCycle',
            ChoiceType::class,
            array(
                'choices'           => $options,
                'choices_as_values' => true,
                'expanded'          => true,
                'label'             => 'Select Billing Cycle',
            )
        )
        ->add('nickName', TextType::class, ['label' => 'Nick name for your server'])
        ->add('ipAddress', TextType::class, ['label' => 'IP Address of your server'])
        ->add(
            'save',
            SubmitType::class,
            array(
                'label' => 'Proceed to Payment',
                'attr'  => [
                    'formnovalidate' => 'formnovalidate',
                ],
            )
        )
        ->getForm();

    $options = [
        'priceArray' => $priceArray,
        'form'       => $form->createView(),
    ];

    $form->handleRequest($request);
    if ($form->isSubmitted()) {

        if ($form->isValid()) {
            die('form is valid');
        } else {
            $errors = $form->getErrors(true, false);
            dump($errors);
            exit;
        }
    }


    return $this->render('default/buy.html.twig', $options);
}

AppBundle/Entity/OrderData.php

    <?php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;

class OrderData
{
    /**
     * @Assert\NotBlank(message="missing_billingcycle");
     */
    protected $billingCycle;

    /**
     * @Assert\NotBlank(message="missing_nickname")
     */
    protected $nickName;

    /**
     * @Assert\NotBlank(message="missing_ipaddress");
     * @Assert\Ip(message="invalid_ipaddress");     
     */
    protected $ipAddress;

    /**
     * @return mixed
     */
    public function getBillingCycle()
    {
        return $this->billingCycle;
    }

    /**
     * @param mixed $billingCycle
     */
    public function setBillingCycle($billingCycle)
    {
        $this->billingCycle = $billingCycle;
    }

    /**
     * @return mixed
     */
    public function getIpAddress()
    {
        return $this->ipAddress;
    }

    /**
     * @param mixed $ipAddress
     */
    public function setIpAddress($ipAddress)
    {
        $this->ipAddress = $ipAddress;
    }

    /**
     * @return mixed
     */
    public function getNickName()
    {
        return $this->nickName;
    }

    /**
     * @param mixed $nickName
     */
    public function setNickName($nickName)
    {
        $this->nickName = $nickName;
    }
}

buy.html.twig

{% extends 'base.html.twig' %}

{% block body %}
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">Purchase</h3>
        </div>
        <div class="panel-body">

            {{ form_start(form) }}
            {{ form_row(form.billingCycle) }}
            {{ form_row(form.nickName) }}
            {{ form_row(form.ipAddress) }}
            {{ form_end(form) }}

        </div>
    </div>
{% endblock %}

The form renders correctly. However, if I submit it, I am expecting the validation message to be visible on the page, but it is not visible.

$errors = $form->getErrors(true, false);
dump($errors);

This is showing me the validation errors translated. However, not quite sure what am I missing here so that it is not displaying on the view

1 Answer 1

2

After a lot of digging, I found where I was wrong.

$options = [
        'priceArray' => $priceArray,
        'form'       => $form->createView(),
    ];

$form->handleRequest($request);

'form' assignment must be after the handleRequest

$form->handleRequest($request);
$options = [
        'priceArray' => $priceArray,
        'form'       => $form->createView(),
    ];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.