Ask your Symfony questions! Pay money and get answers fast! (more info)

How to get a checkbox in a generated admin module? Symfony


I would think this would be obvious but I can not find it anywhere. I've got a table with some fields that are true/false. For instance, regarding wine, there is a field "tasted" which should be marked true once the editors have sampled the wine. This is best handled as a checkbox in the generated form. But what am I suppose to put in generator.yml to get a checkbox?


I'm using Symfony 1.4.

I do this to create the admin module:

./symfony doctrine:generate-admin tastingnotes Importer --module=importer

In schema.yml, for Importer, I have this:

actAs: [Timestampable]
tableName: importer
type: integer
primary: true
autoincrement: true
found_count_from_tastings_sql: string(255)
address_sql: string(255)
address2_sql: string(255)
address2and_sql: string(255)
address2more_sql: string(255)
addressand_sql: string(255)
addressmore_sql: string(255)
alsace_sql: boolean
argentina_sql: boolean
australia_sql: boolean
austria_sql: boolean
bordeaux_sql: boolean
burgundy_sql: boolean
champagne_sql: boolean
chile_sql: boolean
city_sql: string(255)
city2_sql: string(255)
contact_sql: string(255)
contact2_sql: string(255)
country_sql: string(255)
country2_sql: string(255)
dear_sql: string(255)
email_sql: string(255)
email2_sql: string(255)
extension_sql: string(255)
extension2_sql: string(255)
fax_sql: string(255)
fax2_sql: string(255)
germany_sql: boolean
goto_global_sql: string(255)
importer_sql: string(255)
importer_alpha_sql: string(255)
languedoc_sql: boolean
loire_sql: boolean
new_zealand_sql: boolean
notes_sql: string(255)
other_france_sql: boolean
other_italy_sql: boolean
phone_sql: string(255)
phone2_sql: string(255)
piedmont_sql: boolean
portugal_sql: boolean
rhone_sql: boolean
south_africa_sql: boolean
spain_sql: boolean
state_sql: string(255)
state2_sql: string(255)
title_sql: string(255)
title2_sql: string(255)
total_r_sql: string(255)
tuscany_sql: boolean
venetone_sql: boolean
website_sql: string(255)
winery_sql: string(255)
wineryimporter_sql: string(255)
zip_sql: string(255)
zip2_sql: string(255)
all1_sql: string(255)
all2_sql: string(255)
areacode_sql: string(255)
areacode2_sql: string(255)
france_sql: boolean
impcode_sql: string(255)
implog_sql: string(255)
impover_sql: string(255)
italy_sql: boolean
layoutname_calc_sql: string(255)
message_calc_sql: string(255)
message_global_sql: string(255)
partimp_sql: string(255)
partimp2_sql: string(255)
recno_sql: string(255)
record_id_sql: string(255)
relation_constant_sql: string(255)
subject_global_sql: string(255)
wineryimporter_check_sql: string(255)
wineryimporter_name_sql: string(255)
export_record_id_sql: string(255)

Look at the screenshot. You can see that some of the fields marked "boolean" are showing up as text fields, instead of checkboxes.

Answers (5)


Arturo Linares answers:

If the field is boolean it should be rendered automatically as a checkbox. If not, you can always change the widget you are using in the WineForm class, in the configure() method.


Joshua Estes answers:

Let's say you are working with the WineForm class. The widget is called "hasTasted". Look for the form in lib/form/doctrine/WineForm.class.php

class WineForm extends BaseWineForm
public function configure()
$this->widgetSchema['hasTasted'] = new sfWidgetFormInputCheckbox();

Since you already have the widget defined, you just want to change the type of widget. There are a few other ways to create checkboxes but since you just want something that is true/false this should be the easiest.


Florian Klein answers:

As said above, you need to set the type BOOLEAN on the fields of your schema.

Doctrine or Propel will save them as an integer on mysql, but they will generate checkbox widgets for the forms and filters classes.

To do so, you will need to:
* modify your schema to set boolean on the fields
* run the command:
php symfony doctrine:build --all-classes --sql
php symfony propel:build --all-classes --sql

Then, as the admin generator uses form classes to display the form, you will just need to php symfony cc and refresh your screen.

Florian Klein comments:

Another good way to help us debug your problem is to post the code of your ImporterFormFilter.class.php and BaseImporterFormFilter.class.php and ImporterForm.class.php and BaseImporterForm.class.php

Your filter widget schema should look like this:

// ...
'alsace_sql' => new sfWidgetFormChoice(array('choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no'))),
// ...

You form widget schema should be like this

// ...
'alsace_sql' => new sfWidgetFormInputCheckbox(),
// ...

Lawrence Krubner comments:

I have this in my schema.yml:

alsace_sql: boolean

argentina_sql: boolean

australia_sql: boolean

austria_sql: boolean

Look at the screenshot. I'm getting text inputs instead of checkboxes.


Damian Martinelli answers:

If you set the field as boolean, the checkbox will be shown automatically. If not, you can set the field to sfWidgetFormInputCheckbox on your form class.


Marcos IbaƱez answers:

Please publish the part of your schema.yml that corresponds to the field you want to get the checkbox, so we can look deeper into it, since as Arturo and Damian correctly pointed out, if the field is boolean, the checkbox should be automatically generated.