dongsheng1698 2018-05-21 12:55
浏览 52

为什么transform方法的参数始终为null

I have a task to show the user an expiration date for a page. To do this I created a formtype that contains two fields, the first one allows to choose between 1 week 1month and 1 year and custom. if the customer chooses custom, another calendar field is displayed. after I used the datatransformer because the first field returns a string ('1week', '1year' ...) and the second field it will be either null or a date the problem is that the reverse transform method works perfectly but the parameter of the transform method is null ie when the user wants to edit the page, he does not see the last values of the two fields.

class ExpiryDateType extends AbstractType {

public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options) {
    $builder     ->add("chosen",  \Symfony\Component\Form\Extension\Core\Type\ChoiceType::class,array(
                'choices' => array(
                    '1 week' => '7',
                    '1 month' => '30',
                    "1 year"=>"365",
                    "custom"=>"custom"
                )))


            ->add('ExpiryDate', 'sonata_type_date_picker', array('format' => 'yyyy-MM-dd', 'label' => '', 'required' => false))

    ->addModelTransformer(new \ADS\CoreBundle\Form\DataTransformer\ExpiryDateTransformer());
}

public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver) {}


public function buildView(\Symfony\Component\Form\FormView $view, \Symfony\Component\Form\FormInterface $form, array $options) {
    parent::buildView($view, $form, $options);  
}
 public function getBlockPrefix()
{
    return 'expirydate';
}

}

and that is transformer

class ExpiryDateTransformer implements DataTransformerInterface {

public function transform($date) {
    //date is null !!!!
}

public function reverseTransform($array) {

    if ($array["chosen"] == "7") {
        $date = new \DateTime("now");
        $date->modify("+1 week");
        $date->format('Y-m-d H:i:s');
        return($date);
    } else if ($array["chosen"] == "30") {
        $date = new \DateTime("now");
        $date->modify("+1 month");
        $date->format('Y-m-d H:i:s');
        return($date);
    } else if ($array["chosen"] == "365") {
        $date = new \DateTime("now");
        $date->modify("+1 year");
        $date->format('Y-m-d H:i:s');
        return($date);
    } else {
        return($array["ExpiryDate"]);
    }
}

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法