2017-06-06 10 views
1

日付フィールドを含むフォームを更新したいと思います。問題なくフォームを作成できますが、エラーが発生したため編集できませんでした。日付フィールドを含むフォームを更新するエラー(symfony、php)

<?php 

namespace AppBundle\Form; 

use AppBundle\Entity\Contract; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Component\Form\Extension\Core\Type\DateType; 
use Symfony\Component\Form\Extension\Core\Type\TextType; 

class ContractType extends AbstractType { 
    public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('firstDay', DateType::class, array(
     'format' => 'yyyy-MM-dd', 
    )) 
     ->add('lastDay', DateType::class, array(
     'format' => 'yyyy-MM-dd', 
    )) 
     ; 
} 

public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => Contract::class, 
    )); 
} 

そして、ここでは私の更新コード(コントローラ部)は次のとおりです:だからここに私の最初のクラスは、契約ある

public function updateAction($id, Request $request) { 

$encoder = new JsonEncoder(); 
$normalizer = new GetSetMethodNormalizer(); 
$callback = function ($dateTime) { 
    return $dateTime instanceof \DateTime ? $dateTime->format ('Y-m-d') : ''; 
}; 

$normalizer->setCallbacks (array (
     'firstDay' => $callback 
    )); 

$normalizer->setCallbacks (array (
      'lastDay' => $callback 
      )); 


$serializer = new Serializer (array (
    new \AppBundle\DateTimeNormalizer(), $normalizer 
       ), array (
        $encoder 
       )); 
$headers = array('Accept' => 'application/json'); 
$response = Unirest\Request::get('link/contracts/'.$id 
,$headers); 
$contract = 
$serializer->deserialize($response->raw_body,Contract::class, 'json'); // Here when I dump my variable it appears good 
$form = $this->createForm(ContractType::class,$contract); // In this line I got my error 
$form->handleRequest($request); 
if ($form->isSubmitted()) { 
    $headers = array('Content-Type' => 'application/json'); 
    $data = $serializer->serialize($contract, 'json'); 
    $response = Unirest\Request::put('link/contracts'.$id, 
    $headers,$data); 
    return $this->redirect ($this->generateUrl ('contracts_list')); 
} 
return $this->render('AppBundle:Contract:ContractCreate.html.twig', array(
    'form' => $form->createView(), 
)); 

}

私が得たエラー:

Unable to transform value for property path "firstDay": Expected a \DateTimeInterface. 
は、

新しいContract()で$ serialize-> deserialize ...という行を変更すると、それは動作しますが、私は同じフォームを使用するためにserializeを使用する必要があります。

答えて

0

$builder 
    ->add('firstDay', DateType::class, array(
    'format' => 'yyyy-MM-dd', 
)) 
+0

に代わりDateTypeDateTimeTypeクラスを使用してみてください、私はすでに試してみましたが、それは動作しません@spiilアイデアをありがとう – bsm

関連する問題