2017-06-22 7 views
2

symfonyプロジェクトで新しいDateIntervalTypeフィールドを実装する方法は?フォームにDateIntervalTypeクラスを使用する

$builder->add('remindEvery', DateIntervalType::class, array(
    'widget'  => 'integer', // render a text field for each part 
    // 'input' => 'string', // if you want the field to return a ISO 8601 string back to you 

    // customize which text boxes are shown 
    'with_years' => false, 
    'with_months' => false, 
    'with_days' => true, 
    'with_hours' => true, 
));   

DateIntervalTypeフィールドタイプは、Symfony 3.2で導入されました。 このフィールドでは、ユーザーは時間の間隔を選択できます。たとえば、ステータスメールを受信する頻度をユーザーが選択できるようにする場合は、このフィールドを使用して「10分」または「3日」ごとの間隔を選択できます。

答えて

3

エンティティWorkingTime内のフィールドを作成します。

コントローラーで
class WorkingTime 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(
    *  name="duration_task", type="string", length=25 
    *) 
    */ 
private $durationTask; 

// ...more fields 

/** 
* ################################################## 
* Getter & Setter 
* ################################################## 
*/ 


/** 
* Get id 
* 
* @return int 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* Get durationTask 
* 
* @return string 
*/ 
public function getDurationTask() 
{ 
    return $this->durationTask; 
} 

/** 
* Set durationTask 
* 
* @param string $durationTask 
* 
* @return WorkingTime 
*/ 
public function setDurationTask($durationTask) 
{ 
    $this->durationTask = $durationTask; 

    return $this; 
} 

// ... 

$newWorkingTime = new WorkingTime(); 

// you can set default duration: 0 hour 1 minute 
$newWorkingTime 
     ->setDurationZz('PT0H1M'); 

$form = $this->createForm(WorkingTimeType::class, $newWorkingTime); 
$form->handleRequest($request)->getData($newWorkingTime); 

// save to database 
if ($form->isSubmitted()) { 
     $em2 = $this->getDoctrine()->getManager(); 
     $em2->persist($newWorkingTime); 
     $em2->flush(); 
} 

return $this->render(
     'AppBundle:WorkingTime:new.html.twig', [ 
       'form'  =>  $form->createView(), 
     ]           ] 
    ); 

クラスWorkingTimeType:new.html.twigで

namespace AppBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\Extension\Core\Type\DateIntervalType; 
use Symfony\Component\Form\Extension\Core\Type\SubmitType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Component\Validator\Constraints\Length; 


/** 
* Class WorkingTimeType 
* 
* @package AppBundle\Form 
*/ 
class WorkingTimeType extends AbstractType 
{ 
    /** 
    * @param FormBuilderInterface $builder 
    * @param array    $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('durationTask', DateIntervalType::class, [ 
            'input'  => 'string', 
            'widget'  => 'choice', 
            // choice fields to display 
            'with_years' => false, 
            'with_months' => false, 
            'with_days' => false, 
            'with_minutes' => true, 
            'with_hours' => true, 
            'with_seconds' => false, 
           ] 
      ) 
      ->add('save', SubmitType::class); 
    } 


    /** 
    * @param OptionsResolver $resolver 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults([ 'data_class' => 'AppBundle\Entity\WorkingTime' ]); 

    } 


    /** 
    * @return string 
    */ 
    public function getBlockPrefix() 
    { 
     return 'success_form'; 
    } 
} 

{{ form(form) }} 
を10
関連する問題