に実体せずにフォームを作成します。私はタイプのファイルで私のフォーム作成を入れしようとしていますSymfony3は、私はこのように私のコントローラでフォームを作成したタイプのファイル
$data = array();
$formBuilder = $this->createFormBuilder ($data);
$formBuilder->add('Field1', NumberType::class, array(
'constraints' => array(
new NotBlank(),
),
))
->add ('Field2', NumberType::class, array(
'constraints' => array(
new NotBlank(),
)
...);
$form = $formBuilder->getForm();
。私はこれをこのようにしましたが、フォームが作成されず、ビュー内にフォームフィールドを表示できません。理由はわかりません。
#in ControlController
$data = array();
$formBuilder= $this->createFormBuilder(ControlType::class, $data);
$form = $formBuilder->getForm();
#in ControlType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('Field1', NumberType::class, array(
'constraints' => array(
new NotBlank(),
),
))
->add ('Field2', NumberType::class, array(
'constraints' => array(
new NotBlank(),
)....;
}
編集1: 私はあなたが私に教えてすべてのものを試してみましたが、それはまだ動作しません。 私のコードは次のようになります。
#in ControlController
$data = ['Field1' => null, 'Field2' => null];
$formBuilder= $this->createFormBuilder(ControlType::class, $data);
$form = $formBuilder->getForm();
return $this->render ('MeTestBundle:Control:index.html.twig', array (
'form' => $form->createView()
));
#in ControlType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('Field1', NumberType::class, array(
'mapped' => false,
))
->add ('Field2', NumberType::class, array(
'constraints' => array(
new NotBlank(),
),
'mapped' => false
))
->add ('save', SubmitType::class, array (
'label' => 'Control'
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Control::class,
));
}
しかし、今、私が持っているエラーは、オプションの「フィールド1」、「フィールド2」は存在しないです。定義されたオプションは、 "action"、 "allow_extra_fields"、 "attr"、 "auto_initialize"、 "block_name"、 "by_reference"、 "compound"、 "constraints"、 "csrf_field_name"、 "csrf_message"、 "csrf_protection"、 "csrf_token_id 「データ」、「無効」、「空データ」、「エラー_バブリング」、「エラー_マッピング」、「余分なフィールドメッセージ」、「インヘリ_データ」、「無効なメッセージ」、「無効なメッセージパラメータ」、「ラベル」、 "label_attr"、 "label_format"、 "mapped"、 "method"、 "post_max_size_message"、 "property_path"、 "required"、 "translation_domain"、 "trim"、 "upload_max_size_message"、 "validation_groups"
$ data = ['Field1' => null、 'Field2' => null];また、エラーメッセージが表示されるようにdevモードになっていることを確認してください。もちろん、$ formがあなたの小枝のテンプレートに渡されていることを確認して、テンプレートが必要に応じて動作していることを確認します。 – Cerad
私が持っているエラーメッセージは次のとおりです:プロパティ "Field1"とメソッド "Field1()"、 "getField1()"/"isField1()"または "__call()"のいずれも存在せず、クラス " Symfony \ Component \ Form \ FormView " – Etienne
したがって、$ data = ... suggestionです。 – Cerad