我在symfony网站上使用单页模板,其中包含联系表单部分。
我已将我的联系动作(appbundle:contact:new)呈现给我的主页.ContactController由教义CRUD自动生成。
它完美地显示了表单。但它并没有采取任何行动。我错过了什么吗?
我的观点
{% block header %}{% include'header.html.twig' %}{% endblock %}
{% block nav %}{% include'nav.html.twig' %}{% endblock %}
{% block mainHeader %}{% include'mainHeader.html.twig' %}{% endblock %}
{% block body %} {% endblock %}
{% block projetCategories %}{% include'projetCategories.html.twig' %}{% endblock %}
{% block projets %}{% include'projets.html.twig' %}{% endblock %}
{#{% block contact %}{{ render(controller('BlogBundle:Contact:new')) }}{% endblock %}#}
{% block contact %}{% include':contact:new.html.twig' %}{% endblock %}
{% block footer %}{% include'footer.html.twig' %}{% endblock %}
{% block javascripts %}{% include'javascript.html.twig' %}{% endblock %}
我的控制器
class ContactController extends Controller
{
public function newAction(Request $request)
{
$contact = new Contact();
$form = $this->createForm('BlogBundle\Form\ContactType', $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//$contactData = [$form->getValue('nom'), $form->getValue('prenom'),$form->getValue('email'), $form->getValue('sujet'), $form->getValue('message')];
$em = $this->getDoctrine()->getManager();
$em->persist($contact);
$em->flush();
return $this->redirectToRoute('blog_homepage', array('id' => $contact->getId()));
}
return $this->render('contact/new.html.twig', array(
'contact' => $contact,
'contactForm' => $form->createView(),
));
}
}
我的模特
class Contact {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $nom;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $prenom;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $email;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $sujet;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $message;
我的new.html.twig
<div class="col-lg-6 text-center">
{{ form_start(contactForm) }}
{{ form_widget(contactForm) }}
<input type="submit" value="Envoyer" />
{{ form_end(contactForm) }}
我的表单
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nom', TextType::class, array('label'=>false, 'attr'=>array(
'class'=>'form-control',
'placeholder'=>'Nom*',
'style'=>"margin-bottom:5px;"
)));
$builder->add('prenom', TextType::class, array('label'=>false, 'attr'=>array(
'class'=>'form-control',
'placeholder'=>'Prénom*',
'style'=>"margin-bottom:5px;"
)));
$builder->add('email', EmailType::class, array('label'=>false, 'attr'=>array(
'class'=>'form-control',
'placeholder'=>'Email*',
'style'=>"margin-bottom:5px;"
)));
$builder->add('sujet', ChoiceType::class, array( 'label'=>false,
'choices' => array(
'Quel sujet?'=> false,
'1'=>'1',
'2'=>'2',
'3'=>'3',
),
'choices_as_values' => true,
'attr'=>array('class'=>'form-control','style'=>"margin-bottom:5px;"),
'choice_attr' => function($val, $key, $index) {
// adds a class like attending_yes, attending_no, etc
return ['class' => strtolower($key)];
},
));
$builder->add('message', TextareaType::class, array('label'=>false, 'attr'=>array(
'class'=>'form-control',
'placeholder'=>'Message*',
'style'=>"margin-bottom:5px;",
'cols'=>50,
'rows'=>10
)));
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'BlogBundle\Entity\Contact'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'blogbundle_contact';
}
源代码修改后的错误消息
Variable "contactForm" does not exist.
答案 0 :(得分:0)
是的,看起来您创建的是new Contact()
,但实际上并未在其中设置任何值!
尝试这样的事情!
if ($form->isSubmitted() && $form->isValid()) {
$dataArray = $form->getValuesOrWhateverMethod();
$contact->setWhatever($dataArray['whatever']);
// etc
$em = $this->getDoctrine()->getManager();
$em->persist($contact);
$em->flush();
}
此外,您可能更好地将if语句拆分为:
if ($form->isSubmitted()) {
if ($form->isValid()) {
// populate entity and persist
} else {
// populate form with whatever WAS valid
// send form not valid message to view
}
}
答案 1 :(得分:0)
ContactType
的代码吗?