Reputation: 42775
EDIT. TO SUMMARISE: I just want SomeForm that extends my DoctrineForms to NOT INCLUDE certain fields. They are NOT editable. The fixed value I want to set in the code somewhere. Hopefully this should be enough info that you don't need to read the rest of this post...
Hi there. Here is my situation:
What I want to know is, what is the "correct" way to do this? I can think of a few ways to just hack it in, but whatever I do feels like an awkward workaround.
Is there any proper way to do this extending my Form classes (which BaseFormDoctrine) or somewhere else appropriate?
EDIT: As pointed out in the comments below I am actually using doctrine:generate-module which is different to "CRUD" apparently.
ALSO: While I still haven't solved this ideally, I think I know where the solution lies, I just have to dig deeper into symfony forms: http://www.symfony-project.org/forms/1_4/en/02-Form-Validation
Upvotes: 1
Views: 2324
Reputation: 5349
Create another form in your /lib/form folder that extends your normal form and then override the appropriate field. The following will remove the field from the form, so that it isn't displayed at all.
<?php
class FrontendSomeModelForm extends SomeModelForm {
public function configure()
{
unset($this["some_field"]);
}
}
Or if you were wanting to render the value, but not allow it to be edited, you could do the following:
<?php
class FrontendSomeModelForm extends SomeModelForm {
public function configure()
{
$this->setWidget("some_field", new sfWidgetFormPlain());
}
}
And then create a sfWidgetFormPlain
widget that just outputs the value and stick it somewhere that symfony can find it (lib/form/widget or something).
<?php
class sfWidgetFormPlain extends sfWidgetForm
{
/**
* Constructor.
*
* @param array $options An array of options
* @param array $attributes An array of default HTML attributes
*
* @see sfWidgetForm
*/
protected function configure($options = array(), $attributes = array())
{
$this->addOption('value');
}
/**
* @param string $name The element name
* @param string $value The value displayed in this widget
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
* @param array $errors An array of errors for the field
*
* @return string An HTML tag string
*
* @see sfWidgetForm
*/
public function render($name, $value = null, $attributes = array(), $errors = array())
{
//optional - for easy css styling
$attributes['class'] = 'plain';
return $this->renderContentTag('div', $value, $attributes);
}
}
You then use this form rather than your normal one for the one that you don't want to be able to edit. Check the symfony docs for how to do this, depending on whether you are showing it in a module, or through the admin generator.
Upvotes: 3