Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

added $options field and get() set() methods for it #191

Merged
merged 9 commits into from

3 participants

@redexp

with this changes we can simply set form type option

$comment = new Model\Comment();
$type = new Type\CommentType();
$type->set('csrf_protection', false);
$form = $this->createForm($type, $comment);

I just think, maybe it will be better to name methods setOption() and getOption() or it's good enough?

@willdurand
Owner

Yes please, could you change the name of these two methods?

@redexp

added getOptions()

@redexp

One more feature, I added "name" option and rewrote getName() method to return it.
If you don't want to wrap params in user[%s] like

[POST] /users?name=max&age=24

instead of

[POST] /users?user[name]=max&user[age]=24

you can just set

$type->setOption('name', '');

or if you want process many forms with one request

$type1->setOption('name', 'user1');
$type2->setOption('name', 'user2');
[POST] /users?user1[name]=max&user2[name]=max
@redexp

Last modification - added class Propel\PropelBundle\Model\Form\BaseAbstractType (maybe it should be not in Model folder Propel\PropelBundle\Form\BaseAbstractType ?) with two more methods setOptions() and mergeOptions(), also there __construct($mergeOptions) so you can create type like here

$type = new Type\UserType(array(
     'name'            => '',
     'csrf_protection' => false,
));
@redexp

changed namespace from Propel\PropelBundle\Model\Form\BaseAbstractType to Propel\PropelBundle\Form\BaseAbstractType

@willdurand willdurand merged commit 1aae1fc into from
@c33s

wow, that was a lot of rewriting. thanks for your effort!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 68 additions and 21 deletions.
  1. +61 −0 Form/BaseAbstractType.php
  2. +7 −21 Resources/skeleton/FormType.php
View
61 Form/BaseAbstractType.php
@@ -0,0 +1,61 @@
+<?php
+
+namespace Propel\PropelBundle\Form;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\OptionsResolver\OptionsResolverInterface;
+
+abstract class BaseAbstractType extends AbstractType
+{
+ protected $options = array(
+ 'name' => '',
+ );
+
+ function __construct($mergeOptions = null)
+ {
+ if ($mergeOptions) {
+ $this->mergeOptions($mergeOptions);
+ }
+ }
+
+ public function setOption($name, $value)
+ {
+ $this->options[$name] = $value;
+ }
+
+ public function getOption($name)
+ {
+ return $this->options[$name];
+ }
+
+ public function setOptions($options)
+ {
+ $this->options = $options;
+ }
+
+ public function getOptions()
+ {
+ return $this->options;
+ }
+
+ public function mergeOptions($options)
+ {
+ $this->options = array_merge($this->options, $options);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function setDefaultOptions(OptionsResolverInterface $resolver)
+ {
+ $resolver->setDefaults($this->options);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getName()
+ {
+ return $this->getOption('name');
+ }
+}
View
28 Resources/skeleton/FormType.php
@@ -2,34 +2,20 @@
namespace ##NAMESPACE##;
-use Symfony\Component\Form\AbstractType;
+use Propel\PropelBundle\Form\BaseAbstractType;
use Symfony\Component\Form\FormBuilderInterface;
-use Symfony\Component\OptionsResolver\OptionsResolverInterface;
-class ##CLASS## extends AbstractType
+class ##CLASS## extends BaseAbstractType
{
+ protected $options = array(
+ 'data_class' => '##FQCN##',
+ 'name' => '##TYPE_NAME##',
+ );
+
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{##BUILD_CODE##
}
-
- /**
- * {@inheritdoc}
- */
- public function setDefaultOptions(OptionsResolverInterface $resolver)
- {
- $resolver->setDefaults(array(
- 'data_class' => '##FQCN##',
- ));
- }
-
- /**
- * {@inheritdoc}
- */
- public function getName()
- {
- return '##TYPE_NAME##';
- }
}
Something went wrong with that request. Please try again.