Php 隐藏不再适用于symfony更新
目前,我正在更新现有Symfony 2.3(目前为3.0.9)上运行的系统,并正在验证操作 HiddenType用于在搜索表单中选择值时只显示状态数据的函数 在Symfony更新中,我从“hidden”更改为hiddentype::class,但该功能不起作用,并且显示了所有数据。 我在征求意见 代码 BrandSerachType.phpPhp 隐藏不再适用于symfony更新,php,symfony,Php,Symfony,目前,我正在更新现有Symfony 2.3(目前为3.0.9)上运行的系统,并正在验证操作 HiddenType用于在搜索表单中选择值时只显示状态数据的函数 在Symfony更新中,我从“hidden”更改为hiddentype::class,但该功能不起作用,并且显示了所有数据。 我在征求意见 代码 BrandSerachType.php namespace Ahi\Sp\AdminBundle\Form\Type\Brand; use Symfony\Component\Form\Abst
namespace Ahi\Sp\AdminBundle\Form\Type\Brand;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
/**
* 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 Ahi\Sp\AdminBundle\Controller\BaseController;
use Ahi\Sp\AdminBundle\Form\Type\Brand\BrandType;
use Ahi\Sp\AdminBundle\Form\Type\Brand\BrandSearchType;
use Ahi\Sp\CommonBundle\Util\Pagination;
/**
* 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,
);
}
服务.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>
版本
Cent OS 6.7 PHP 5.6 Symfony3.0.9通常我会这样设置隐藏类型,试试看,也许它会帮助您:
$builder
->add('attributeName', TextType::class, [
'label' => false,
'attr' => ['hidden' => 'hidden', 'value' => 'myHiddenValue']
谢谢你的回答。这是第一种写作风格。我试过了,但没用。请检查下面的代码是否匹配。``$builder->add'brandSex',TextType::class,['label'=>false,['attr'=>['hidden'=>hidden',required'=>false]```