【问题标题】:Creating a Form class doesnt work in Symfony, shows no error创建表单类在 Symfony 中不起作用,显示没有错误
【发布时间】:2015-07-27 12:33:07
【问题描述】:

我正在尝试从表单类在Symfony 中创建一个简单的表单。该代码未显示错误,但未显示在浏览器中。有人知道我做错了什么吗?

控制器

namespace ReuseBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use ReuseBundle\Entity\Company;
use ReuseBundle\ReuseForms\CreateCompany;

class CompanyController extends Controller
{
public function newAction()
{
    $company = new Company();
    $form = $this->createForm(new CreateCompany(),$company, array(
        'action'=>$this->generateUrl('reuse_create'),
        'method'=>'POST'

    ));

    $form->add('submit', 'submit', array('label'=>'Create Company'));
    return $this->render('ReuseBundle:Default:create.html.twig', array(
        'form'=>$form->createView()
    ));
}

public function createAction(Request $request)
{

}
}

表单类

<?php

namespace ReuseBundle\ReuseForms;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;


class CreateCompany extends AbstractType
{
    public function buildForms(FormBuilderInterface $builder, array    $options){
        $builder
            ->add('Company_Name')
        ->add('address')
        ->add('Zip')
        ->add('City')
        ->add('Country')
        ->add('phone')
        ->add('fax')
        ->add('com_email', 'email')
        ->add('website');
}

public function getName()
{
    return 'ReuseBundle_Company';
}
}

我使用教义生成 get 和 set 方法的实体

namespace ReuseBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table{name="Company"}
 */
class Company{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
protected $id;
/**
 * @ORM\Column(type="string",length=250)
 * @Assert\NotBlank()
 * @Assert\Length(min=2)
 */
protected $Company_name;
/**
 * @ORM\Column(type="string",length=300)
 * @Assert\NotBlank()
 * @Assert\Length(min=15)
 *
 */
protected $address;
/**
 * @ORM\Column(type="integer",length=7)
 * @Assert\NotBlank()
 * @Assert\Length(min=3)
 * @Assert\Type(type="integer")
 */
protected $Zip;
/**
 * @ORM\Column(type="string",length=50)
 * @Assert\NotBlank()
 */
protected $City;
/**
 * @ORM\Column(type="string",length=50)
 * @Assert\NotBlank()
 */
protected $Country;

/**
 * @ORM\Column(type="string",length=25)
 * @Assert\NotBlank()
 */
protected $phone;
/**
 * @ORM\Column(type="string",length=25)
 */
protected $fax;
/**
 * @Assert\NotBlank()
 */
protected $com_email;
/**
 * @ORM\Column(type="string",length=50)
 * @Assert\NotBlank()
 */
protected $website;


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

/**
 * Set Company_name
 *
 * @param string $companyName
 * @return Company
 */
public function setCompanyName($companyName)
{
    $this->Company_name = $companyName;

    return $this;
}

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

/**
 * Set address
 *
 * @param string $address
 * @return Company
 */
public function setAddress($address)
{
    $this->address = $address;

    return $this;
}

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

/**
 * Set Zip
 *
 * @param integer $zip
 * @return Company
 */
public function setZip($zip)
{
    $this->Zip = $zip;

    return $this;
}

/**
 * Get Zip
 *
 * @return integer 
 */
public function getZip()
{
    return $this->Zip;
}

/**
 * Set City
 *
 * @param string $city
 * @return Company
 */
public function setCity($city)
{
    $this->City = $city;

    return $this;
}

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

/**
 * Set Country
 *
 * @param string $country
 * @return Company
 */
public function setCountry($country)
{
    $this->Country = $country;

    return $this;
}

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

/**
 * Set phone
 *
 * @param string $phone
 * @return Company
 */
public function setPhone($phone)
{
    $this->phone = $phone;

    return $this;
}

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

/**
 * Set fax
 *
 * @param string $fax
 * @return Company
 */
public function setFax($fax)
{
    $this->fax = $fax;

    return $this;
}

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

/**
 * Set website
 *
 * @param string $website
 * @return Company
 */
public function setWebsite($website)
{
    $this->website = $website;

    return $this;
}

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

html.twig

创建公司

{{ form(form) }}

路由 重用公司: 路径:/新 默认值:{_controller : ReuseBundle:Company:new}

reuse_create:
    path:      /create
    defaults: { _controller: ReuseBundle:Company:create}
    requirements: { _method: post }

【问题讨论】:

  • 如果您只显示静态 html 而不是表单,它是否有效? IE。你看到页面了吗?

标签: php forms class symfony


【解决方案1】:

因为必须在动作控制器方法中调用 Form::handleRequest() 和 Form::isValid(),就像这样:

public function newAction(Request $request)
{
    $company = new Company();
    $form = $this->createForm(new CreateCompany(),$company, array(
        'action'=>$this->generateUrl('reuse_create'),
        'method'=>'POST'

    ));
    $form->add('submit', 'submit', array('label'=>'Create Company'));
    # Must have
    $form->handleRequest($request);

    if ($form->isValid()) {
        // the validation passed, do something with the $company object        
    }
    # Show empty form or form with errors (if any)
    return $this->render('ReuseBundle:Default:create.html.twig', array(
        'form'=>$form->createView()
    ));
}

当调用 Form:handleRequest() 对象时被验证。 handleRequest() 将输入的数据写回 $company 对象。如果无效(验证在下一节中介绍),isValid() 再次返回 false,因此表单与所有验证错误一起呈现;

【讨论】:

  • 但是表单handleRequest和Form是在表单创建之后才生效的...表单不显示...
【解决方案2】:

在您的表单类定义中,方法的名称必须是 buildForm 而不是 buildForms:

public function buildForm(FormBuilderInterface $builder, array $options)

【讨论】:

  • 完全没有改变我的表单仍然没有显示
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多