Skip to content

Instantly share code, notes, and snippets.

View webmozart's full-sized avatar

Bernhard Schussek webmozart

View GitHub Profile
<?php
protected function _editAction(Post $post)
{
$em = $this->get('doctrine.orm.default_entity_manager');
$factory = $this->get('form.factory');
$form = $factory->create(new PostFormType());
$form->setData($post);
if ($this->get('request')->getMethod() === 'POST') {
class CustomRenderer extends \Symfony\Component\Form\Renderer\DefaultRenderer
{
public function getHelp($text, array $vars = array())
{
$vars['text'] = $text;
return $this->render('help', $vars);
}
}
<form method="post" action="..." {{ form.enctype }}>
{{ form.errors }}
{% for field in form.visible_fields %}
{{ field.label('My label') }}
{{ field.errors }}
{{ field.widget }}
The ID of this field is: {{ field.id }}
And the value: {{ field.value }}
{% endfor %}
protected function editAction(Post $post)
{
$factory = new HelloFormFactory($this->get('form.factory'));
$form = $factory->getPostForm($post);
if ($this->get('request')->getMethod() === 'POST') {
$form->bindRequest($this->get('request'));
if ($form->isValid()) {
$em = $this->get('doctrine.orm.default_entity_manager');
<?php
namespace Sensio\HelloBundle;
use Symfony\Component\Form\FormFactory;
use Sensio\HelloBundle\Entity\Post;
use Sensio\HelloBundle\Entity\Comment;
class HelloFormFactory
{
git clone git://github.com/bschussek/symfony-sandbox.git
cd symfony-sandbox
git checkout -b hacking workshop-start
cp app/config/doctrine.yml-dist app/config/doctrine.yml
app/console doctrine:database:create
app/console doctrine:schema:create
app/console doctrine:data:load
Using a static OptionSupport class that automatically writes options into properties AND fields extend it.
+ Options are set in the very beginning of the constructor
+ Concise
+ Error if option is not supported
+ Everything is a property, no special treatment of options
+ Good performance
+ Static options can be hidden from public access
- Private properties wouldn't work, access not allowed from parent classes.
doctrine:
dbal:
dbname: xxxxxxxx
user: xxxxxxxx
password: ~
logging: %kernel.debug%
# Services for a bundle are always loaded
# hello configLoad() will be called automatically (if defined)
# Namespaces can be simplified
doctrine:
dbal:
dbname: xxxxxxxx
user: xxxxxxxx
password: ~
The current way, using addOption(), addRequiredOption() and getOption()
+ Concise
+ Error if option is not supported
- Slow, because the addOption() calls are executed for every new instance
- Base class Configurable needed
- Overriding options in parent fields very intransparent
- Options need special treatment ($this->required vs. $this->getOption('required'))
- Problems with accessing required options before parent::__construct()