In my application I have a lot of modules and a lot of forms with their own widgets.
I have been trying to Globally 'unset', 'hide' or 'make read only' a widget.
I know that its possible to do it for one widget in the configure() function of the form, but I am looking for a better solution, if possible in a global way. It could be a pain to change all module forms and keep track of them.
The thread could be a bit long but I think that its easy to understand the problem. Thank you so much if you have time to read it :)
I have built a simple news module with the Timestampable behavior to test some options but still none of them worked.
- table:
TbNews
- columns:
id
as primary keyscontent
as a text field for saving the news content.created_at
as date time. (Timestampable behavior)updated_at
as date time. (Timestampable behavior)
TbNews schema.yml:
TbNews: connection: doctrine_master tableName: tb_news columns: id: type: integer(8) fixed: false unsigned: false primary: true autoincrement: true scontent: type: string(64000) fixed: false unsigned: false primary: false notnull: true autoincrement: false actAs: Timestampable: created: name: created_at type: timestamp format: Y-m-d H:i:s options: notnull: false required: false updated: name: updated_at type: timestamp format: Y-m-d H:i:s options: notnull: false required: false
TbNews updated_at in Form
(for the example, only wrote here the updated_at column):
class TbNewsForm extends PluginTbNewsForm { public function configure() { $this->setWidgets(array( 'updated_at' => new sfWidgetFormDateTime(), )); $this->setValidators(array( 'updated_at' => new sfValidatorDateTime(array('required' => false)), )); } }
Template _form.php for TbNews updated_at:
Tested a lot of options, manually rendering updated_at with: "echo $form['updated_at']" and without rendering it.
<?php echo $form->renderHiddenFields(false) ?>
Global tests with update_at
column in: lib/form/doctrine/BaseFormDoctrine.class.php
:
- With
sfWidgetFormInputHidden()
, widget is not rendered in template.
Tested unsetting it, without unsetting it, with setWidget, without it... all possible options but the field updated_at is not rendered in template. ( used renderHiddenFields() )
abstract class BaseFormDoctrine extends sfFormDoctrine { public function setup() { $value_updated_at = $this->getObject()->updated_at; unset($this->widgetSchema['updated_at']); unset($this->validatorSchema['updated_at']); $this->widgetSchema['updated_at'] = new sfWidgetFormInputHidden(); $this->widgetSchema['updated_at']->setOption('is_hidden', 'true'); $this->widgetSchema['updated_at']->setOption('type', 'hidden'); $this->setDefault('updated_at', $value_updated_at); $this->setWidget('updated_at', new sfWidgetFormInputHidden(array('value'=>$value_updated_at))); //$this->setWidget('updated_at', $this->widgetSchema['updated_at']); } }
- With read only:
Added the fieldupdated_at
in the _form.php template, but its not shown as read only. Seems doesnt work withsfWidgetFormDateTime
.$this->widgetSchema['updated_at']->setAttribute('readonly', 'readonly');
Questions and thoughts:
Probably
BaseFormDoctrine
is not the right way to global change a widget in all Form modules or there is probably something wrong in my tests.
As you may probably know, if we use the above code directly in the Form widget
TbNewsForm configure()
, we can easily change the widget from one type to another type, and individually render or hide it, and it works.
- But how can we implement it in a global way?
- Should it be necessary to change core code of symfony Doctrine?
Thank you so much if you could take time to read it, any comment or solution is welcome :)