Skip to content
Advertisement

Hidden no longer works with symfony update

Currently, I am updating the system running on the existing Symfony 2.3 (currently 3.0.9), and I am verifying the operation.

HiddenType is used for the function that when you select a value in the search form, only the data that is the status is displayed.

I changed from 'hidden' toHiddenType :: class in the Symfony update, but the feature doesn’t work and all the data is displayed. I am seeking advice.

Code

BrandSerachType.php

namespace AhiSpAdminBundleFormTypeBrand;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeHiddenType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormExtensionCoreTypeSubmitType;

/**
 * Form Type
 */
class BrandSearchType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // brandsex
        $builder->add('brandSex', HiddenType::class, array(
            'required' => false,
        ));

        // flag
        $builder->add("brandDispFlg", HiddenType::class, array(
            "required" => false,
        ));

        $builder->add("brandName", TextType::class, array(
            "required" => false,
        ));

        // searchbutton
        $builder->add('search', SubmitType::class);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            "method" => "GET",
            "csrf_protection" => false,
            "validation_groups" => false,
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return "q";
    }
}

BrandController.php

use AhiSpAdminBundleControllerBaseController;
use AhiSpAdminBundleFormTypeBrandBrandType;
use AhiSpAdminBundleFormTypeBrandBrandSearchType;
use AhiSpCommonBundleUtilPagination;

/**
 * Brand Controller
 *
 * @Route("/hq/brand")
 */
class BrandController extends BaseController
{
    /**
     * List Screen
     *
     * @Method("GET")
     * @Route("/")
     * @Secure(roles="ROLE_HQ_MANAGE")
     * @Template("AhiSpAdminBundle:Hq/Brand:index.html.twig")
     */
    public function indexAction(Request $request)
    {
        // Create a search form
        $searchForm = $this->createForm(BrandSearchType::class, null, array(
            "action" => $this->generateUrl("ahi_sp_admin_hq_brand_index"),
        ));

        // Get search criteria
        $params = $this->getSearchParameter($searchForm, $request);

        // Sortable if only the target gender is specified in the search conditions
        $sortable = false;
        if ($request->query->get('sortable') && isset($params["brandSex"])) {
            $paramCount = count(array_filter($params, function ($var) {
                return ($var !== null);
            }));
            if ($paramCount == 1) {
                $sortable = true;
            }
        }
        return array(
            "searchForm" => $searchForm->createView(),
            "brands" => $brands,
            "pagination" => $pagination,
            "sortable" => $sortable,
        );
    }

Services.yml

# Target gender
     target_sex_mens: 0
     target_sex_ladies: 1
     target_sex_others: 2
     target_sex:
         % target_sex_mens%: "Men's"
         % target_sex_ladies%: "Ladies"
         % target_sex_others%: "Men's & Women's"

index.html.twig

                {# Target gender #}
                <tr>
                    <td>{{ form_label(searchForm.brandSex, 'Target gender') }}</td>
                    <td>
                        {% if searchForm.brandSex.vars.data is null %}
                            <strong>All</strong>
                        {% else %}
                            <a href="{{ path(app.request.get('_route'), params|merge({"q": q|merge({"brandSex": null})})) }}">All</a>
                        {% endif %}
                        |
                        {% if searchForm.brandSex.vars.data is not null and searchForm.brandSex.vars.data == target_sex_mens %}
                            <strong>Men's</strong>
                        {% else %}
                            <a href="{{ path(app.request.get('_route'), params|merge({"q": q|merge({"brandSex": target_sex_mens})})) }}">Men's</a>
                        {% endif %}
                        |
                        {% if searchForm.brandSex.vars.data is not null and searchForm.brandSex.vars.data == target_sex_ladies %}
                            <strong>Women's</strong>
                        {% else %}
                            <a href="{{ path(app.request.get('_route'), params|merge({"q": q|merge({"brandSex": target_sex_ladies})})) }}">Women's</a>
                        {% endif %}
                        |
                        {% if searchForm.brandSex.vars.data is not null and searchForm.brandSex.vars.data == target_sex_others %}
                            <strong>Men's & Women's</strong>
                        {% else %}
                            <a href="{{ path(app.request.get('_route'), params|merge({"q": q|merge({"brandSex": target_sex_others})})) }}">Men's & Women's</a>
                        {% endif %}
                        {{ form_widget(searchForm.brandSex) }}
                    </td>
                </tr>

Version Cent OS 6.7 PHP 5.6 Symfony3.0.9

Advertisement

Answer

Usually I set hidden type like this, try, maybe it helps you:

$builder
     ->add('attributeName', TextType::class, [
        'label' => false,
        'attr'  => ['hidden' => 'hidden', 'value' => 'myHiddenValue']
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement