php - Symfony 2 - Checkbox with default Value unless Data From Database -
i have check box renders correctly . if there no data database want check box checked default.
if set "data" = true displays checkbox checked. if data database won't override checkbox boolean value of false. if remove data" => true able correct checkbox database not able set default checkbox.
public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('isfieldfirstname', 'checkbox', array( 'label' => 'show first name', 'required' => false, "data" => true, ) ) just recap
- database - no record found - show default checkbox checked
- if record found use value database ex isfieldfirstname = false checkbox unchecked
you implement buildform method describe structure of form, not values. values plain array or data object. symfony uses dataaccess component map them.
- drop "data" option in checkbox
- call checkbox "fieldfirstname" (no "is")
// myformtype.php
public function buildform(formbuilderinterface $builder, array $options) { $builder ->add( 'fieldfirstname', 'checkbox', [ 'label' => 'show first name', 'required' => false ] ); } - create data class
// myformdata.php
class myformdata { private $fieldfirstname; public function isfieldfirstname() { return $this->fieldfirstname; } public function setfieldfirstname($fieldfirstname) { $this->fieldfirstname = $fieldfirstname; } } - create action
// mycontroller.php
// ... public function myaction() { $data = new myformdata(); $data->setfieldfirstname( ! $myservice->isdataindatabase() ); $form = $this->createform( new myformtype(), $data ); // ... } didn't test right now, should work.
Comments
Post a Comment