创建一个Form类在Symfony中不起作用,没有显示错误

时间:2015-07-27 12:33:07

标签: php forms class symfony

我正在尝试从表单类在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';
}
}

我使用doctrine生成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) }}

路由     reuse_company:     路径:/ new     默认值:{_ control:ReuseBundle:Company:new}

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

2 个答案:

答案 0 :(得分:0)

因为你必须在动作控制器方法中调用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,因此表单与所有验证错误一起呈现;

答案 1 :(得分:0)

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

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