表单不将数据发送到数据库

亚历克斯

我是Symfony的新手,我已经阅读了几乎所有有关将数据发送到数据库的文章。我已包含所有答案/解决方案,但仍未发送到数据库。有人可以帮忙吗?

我已经创建了一个表单,我想发送数据库中的所有值,我还创建了带有“ Profile”类的实体

public function createProfile(Request $request)
{

        $em = $this->getDoctrine()->getManager();
        $profile = new Profile();

        $profile->setUserName('Name');
        $profile->setUserPosition('Position');
        $profile->setUserPhone('Phone');   
        $profile->setUserEmail('E-mail');   
        $profile->setUserDepartment('Department');



    $form = $this->createFormBuilder($profile)
        ->add('userName', TextType::class)
        ->add('userDepartment', ChoiceType::class, array(
             'placeholder' => 'Select department',
                'choices' => array(
                    'HR' => 'hr',
                    'Marketing' => 'marketing',
                    'Development' => 'development',
                    'Finance'   => 'finance',
                    'Logistics' => 'logistics',
                ),
        )) 
        ->add('userPosition', TextType::class)
        ->add('userPhone', TextType::class)
        ->add('userEmail', TextType::class)
        ->add('save', SubmitType::class, array('label' => 'Create Profile'))
        ->getForm();


     $form->handleRequest($request);

     // the if statement does not work :(
     if ($form->isSubmitted() && $form->isValid())  {

        dump($profile); // this doesn't return
        $form->bind($request);

        $form['userName']->getData();
        $form['userDepartment']->getData();
        $form['userPosition']->getData();
        $form['userPhone']->getData();
        $form['userEmail']->getData();

        $em->persist($profile);
        $em->flush();

    }

    return $this->render('dashboard/new_profile.html.twig', array(
        'form' => $form->createView(),
    ));
}

//new_profile.html.twig

{{ form_start(form) }}
{{ form_label(form.userName, 'Name') }}

{{ form_widget(form.userName, {'attr': {'placeholder': 'Your name', 'value':''} }) }}

{{ form_label(form.userPosition, 'Position') }}
{{ form_widget(form.userPosition, {'attr': {'placeholder': 'Your position', 'value':''} }) }}

{{ form_label(form.userPhone, 'Phone') }}
{{ form_widget(form.userPhone, {'attr': {'placeholder': 'Your phone', 'value':''} }) }}

{{ form_label(form.userEmail, 'E-mail') }}
{{ form_widget(form.userEmail, {'attr': {'placeholder': 'Your e-mail', 'value':''} }) }}

{{ form_label(form.userDepartment, 'Department') }}
{{ form_widget(form.userDepartment, {'attr': {'placeholder': 'Your department', 'value':''} }) }}

{{ form_rest(form) }}
{{ form_end(form) }}
特里·贝克

首先,您应该分拆代码。您的控制文件应该控制发生的事情-不执行并构建所有内容。

Src/Form目录下生成表单创建一个名为Profile的新文件夹,并在名为AddType.php的文件下创建一个文件-完整路径为Src/Form/Profile/AddType.php

# Src/Form/Profile/AddType.php

<?php
    namespace App\Form\Profile;

    use App\Entity\Profile;
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\Extension\Core\Type\CheckType;
    use Symfony\Component\Form\Extension\Core\Type\SubmitType;
    use Symfony\Component\Form\Extension\Core\Type\TextType;

    class AddType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('userName', TextType::class)
                ->add('userDepartment', ChoiceType::class, array(
                    'placeholder' => 'Select department',
                    'choices' => array(
                        'HR' => 'hr',
                        'Marketing' => 'marketing',
                        'Development' => 'development',
                        'Finance'   => 'finance',
                        'Logistics' => 'logistics',
                    ),
                ))
                ->add('userPosition', TextType::class)
                ->add('userPhone', TextType::class)
                ->add('userEmail', TextType::class)
                ->add('save', SubmitType::class, array('label' => 'Create Profile'));
        }

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

然后在您的控制器中:

<?php
    class CreateController extends Controller
    {
        public function index(Request $request)
        {
            $profile = new Profile();

            $form = $this->createForm(AddType::class, $profile);
            $form->handleRequest($request);

            if ($form->isSubmitted() && $form->isValid()) {
                $em = $this->getDoctrine()->getManager();

                $em->persist($profile);
                $em->flush();
                # what other actions you need. e.g redirect to success page
            }

            return $this->render('product/add.html.twig', ['form' => $form->createView()]);
        }
    }

然后在嫩枝中渲染表单:

# templates/product/add.html.twig

{{ form_start(form) }}
    {# your custom code for rendering the form #} 
    {# if you leave default then it should render with bad styles etc. #}
{{ form_end(form) }}

注意:您可能必须更改上面的代码以适合您的编码需求,但是(理论上)应该可以工作-这是在用树枝文件生成的页面上工作的-如果您不打算在控制器中没有表格,使用表格。只需手动传递数据...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

发送到数据库时的Android GPS延迟

将html表单数据发送到mysql数据库

如何将数据从用户注册表单发送到Firebase数据库

如何将csv格式的数据从内存发送到数据库而不将csv保存到磁盘?

来自论坛的信息未发送到数据库

使用Update时如何不将Null发送到数据库

ReactJS:表单将错误的日期值发送到数据库

如何将表单值发送到节点/表达式以搜索数据库?

无法将数据从表单发送到Firestore数据库

将XML数据发送到MySQL数据库

用户信息未发送到数据库

将textarea数据发送到数据库的麻烦

通过URL发送到SQL数据库

为什么我的HTML表单不将数据发送到我的JS文件?

Bootstrap表单将数据错误发送到MongoDB数据库

复选框不将检查的值发送到数据库C#Asp

将下拉列表的信息发送到数据库

PHP表单将除图像外的所有内容发送到数据库

如何在Rails提交的表单上将画布图像发送到数据库

如何将表单输入值发送到在本地创建的Empty数据库?

无需切换页面,通过php将html表单数据发送到数据库

将数据从表单发送到数据库

无法将数据从用户表单发送到 PHP 中的数据库

如何将出生日期从 html 表单发送到数据库

Drupal 8、打印表单并将数据发送到数据库

我如何获取表单数据使用 javascript 验证它并使用 php 将其发送到数据库

将json数据发送到mysql数据库?

空对象被发送到数据库

Php 将表单数据发送到 Wordpress 中的特定 Mysql 数据库中