Php Symfont Twig将月-年设置为表单上的选定值

Php Symfont Twig将月-年设置为表单上的选定值,php,forms,symfony,twig,Php,Forms,Symfony,Twig,我跟随这篇文章,为信用卡到期日设置了一个月-年小部件: 现在我尝试将字段设置为表单提交期间选择的值。我该怎么做 我的表格类型: $builder->add('card_expiration_date', 'date', array( 'mapped' => false, 'widget' => 'choice', 'empty_value' => array('year' => 'Year',

我跟随这篇文章,为信用卡到期日设置了一个月-年小部件:

现在我尝试将字段设置为表单提交期间选择的值。我该怎么做

我的表格类型:

$builder->add('card_expiration_date', 'date', array(
            'mapped' => false,
            'widget' => 'choice',
            'empty_value' => array('year' => 'Year', 'month' => 'Month', 'day' => 'Day'),
            'format' => 'dd-MM-yyyy',
            'input' => 'string',
            'data' => date('Y-m-d'),
            'years' => range(date('Y'), date('Y') + 10)
        ));
我一直想做的一些事情是:

试验1

{{ form_widget(form.card_expiration_date.month, {'value': ccMonth}) }}
{{ form_widget(form.card_expiration_date.year, {'value': ccYear}) }}
这会正确设置值,但问题是它仍然会显示form.card\u expiration\u日期

试用版2:

{%  if ccMonth == '' and ccYear == ''  %}
    {{ form_widget(form.card_expiration_date, {'date_pattern': '<span style="display: none;">{{ day }}</span> {{ month }} <span class="delim">&#47;</span> {{ year }}'}) }}
{% else %}
    {{ form_widget(form.card_expiration_date, {'date_pattern': '<span style="display: none;">{{ day }}</span> {{ month }} <span class="delim">&#47;</span> {{ year }}', 'value':  cmonth~'/'~cyear  }) }}
{% endif %}
{%  if cmonth == '' and cyear == ''  %}
    {{ form_widget(form.card_expiration_date, {'date_pattern': '<span style="display: none;">{{ day }}</span> {{ month }} <span class="delim">&#47;</span> {{ year }}'}) }}
{% else %}
    {{ form_widget(form.card_expiration_date, {'date_pattern': '<span style="display: none;">{{ day }}</span> {{ month }} <span class="delim">&#47;</span> {{ year }}'}) }}
    {{ form_widget(form.card_expiration_date.month, {'value': cmonth}) }}
    {{ form_widget(form.card_expiration_date.year, {'value': cyear}) }}
{% endif %}
BillingType:

function __construct(EntityManager $em, $ccInfo)
{
    $this->em = $em;
    $this->ccInfo = $ccInfo;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $date = date('Y-m-d');

    if( ! is_null($this->ccInfo['card']))
    {
        if (substr($this->ccInfo['card']['expdate'], -2, 1) == '0')
        {
            $ccMonth = substr($this->ccInfo['card']['expdate'], -1, 1);
        }
        else
        {
            $ccMonth = substr($this->ccInfo['card']['expdate'], -2, 2);
        }
        $ccYear = '20'.substr($this->ccInfo['card']['expdate'], 0, 2);

        $date = date('Y-m-d', strtotime($ccYear.'-'.$ccMonth.'-01'));
    }

$builder->add('card_expiration_date', 'date', array(
            'mapped' => false,
            'widget' => 'choice',
            'empty_value' => array('year' => 'Year', 'month' => 'Month', 'day' => 'Day'),
            'format' => 'dd-MM-yyyy',
            'input' => 'string',
            'data' => $date,
            'years' => range(date('Y'), date('Y') + 10)
        ));
}

都不是。您应该设置基础对象(或数组)的数据。
form_小部件
view助手已经呈现了值(如果存在)。如果您处于控制器上下文中,那么已经有了一种方法来执行此操作

$form->handleRequest($this->getRequest());

这将把请求数据绑定到表单的数据对象。如果您想分享更多详细信息,我可以帮助您确定具体的实现。

不太清楚控制器上下文是什么意思。正如您在表单类型中看到的,该字段未映射。请参阅我文章中的编辑2。似乎无法在此发布所有代码。未映射的字段意味着无法将其持久化到数据库。但这并不意味着它不能通过请求访问。@Peter Bailey的意思是,将当前请求绑定到表单(提交后),表单字段应自动填充提交前保存的数据。我正在尝试使用twig设置值。那么,我将如何从控制器上下文执行此操作?举个例子吧?有没有一种方法可以在twig一侧明确地设置card\u expiration\u date.day、card\u expiration\u date.month和card\u expiration\u date.year?您可能可以在调用
表单小部件之前执行
{%set form.field.vars.value='value%}
,但您确实不应该这样做。设置对象值并不是一个模板问题,而且违反了这个分离要求,这是维护方面的难题。
function __construct(EntityManager $em, $ccInfo)
{
    $this->em = $em;
    $this->ccInfo = $ccInfo;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $date = date('Y-m-d');

    if( ! is_null($this->ccInfo['card']))
    {
        if (substr($this->ccInfo['card']['expdate'], -2, 1) == '0')
        {
            $ccMonth = substr($this->ccInfo['card']['expdate'], -1, 1);
        }
        else
        {
            $ccMonth = substr($this->ccInfo['card']['expdate'], -2, 2);
        }
        $ccYear = '20'.substr($this->ccInfo['card']['expdate'], 0, 2);

        $date = date('Y-m-d', strtotime($ccYear.'-'.$ccMonth.'-01'));
    }

$builder->add('card_expiration_date', 'date', array(
            'mapped' => false,
            'widget' => 'choice',
            'empty_value' => array('year' => 'Year', 'month' => 'Month', 'day' => 'Day'),
            'format' => 'dd-MM-yyyy',
            'input' => 'string',
            'data' => $date,
            'years' => range(date('Y'), date('Y') + 10)
        ));
}
$form->handleRequest($this->getRequest());