php - Creating a Form class doesnt work in Symfony, shows no error -
i trying create simple form in symfony
form class. code shows no error not displayed in browser. has idea doing wrong?
controller
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) { } }
form class
<?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'; } }
the entity generated , set methods doctrine
namespace reusebundle\entity; use doctrine\orm\mapping orm; use symfony\component\validator\constraints 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; /** * 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; } /** * 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; } /** * 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; } /** * 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; } /** * 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; } /** * 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; } /** * 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; } /** * 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; } /** * website * * @return string */ public function getwebsite() { return $this->website; } }
html.twig
create company
{{ form(form) }}
routing reuse_company: path: /new defaults: {_controller : reusebundle:company:new}
reuse_create: path: /create defaults: { _controller: reusebundle:company:create} requirements: { _method: post }
because must invoke form::handlerequest() , form::isvalid() in action controller method, this:
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()) { // validation passed, $company object } # show empty form or form errors (if any) return $this->render('reusebundle:default:create.html.twig', array( 'form'=>$form->createview() )); }
when invoking form:handlerequest() object validated. handlerequest()writes inputed data $company object.f invalid (validation covered in next section), isvalid() returns false again, form rendered validation errors;
Comments
Post a Comment