-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Fixed option support in Form component #3789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
6D47
|
@@ -282,7 +282,7 @@ | |
``` | ||
|
||
* The options passed to the `getParent()` method of form types no longer | ||
contain default options. | ||
contain default options. They only contain the options passed by the user. | ||
|
||
You should check if options exist before attempting to read their value. | ||
|
||
|
@@ -303,6 +303,42 @@ | |
return isset($options['widget']) && 'single_text' === $options['widget'] ? 'text' : 'choice'; | ||
} | ||
``` | ||
|
||
* The methods `getDefaultOptions()` and `getAllowedOptionValues()` of form | ||
types no longer receive an option array. | ||
|
||
You can specify options that depend on other options using closures instead. | ||
|
||
Before: | ||
|
||
``` | ||
public function getDefaultOptions(array $options) | ||
{ | ||
$defaultOptions = array(); | ||
|
||
if ($options['multiple']) { | ||
$defaultOptions['empty_data'] = array(); | ||
} | ||
|
||
return $defaultOptions; | ||
} | ||
``` | ||
|
||
After: | ||
|
||
``` | ||
public function getDefaultOptions() | ||
{ | ||
$return array( | ||
'empty_data' => function (Options $options, $previousValue) { | ||
return $options['multiple'] ? array() : $previousValue; | ||
} | ||
); | ||
} | ||
``` | ||
|
||
The second argument `$previousValue` does not have to be specified if not | ||
needed. | ||
|
||
* The `add()`, `remove()`, `setParent()`, `bind()` and `setData()` methods in | ||
the Form class now throw an exception if the form is already bound. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
use Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList; | ||
use Symfony\Bridge\Propel1\Form\DataTransformer\CollectionToArrayTransformer; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Options; | ||
use Symfony\Component\Form\FormBuilder; | ||
|
||
/** | ||
|
@@ -30,8 +31,18 @@ public function buildForm(FormBuilder $builder, array $options) | |
} | ||
} | ||
|
||
public function getDefaultOptions(array $options) | ||
public function getDefaultOptions() | ||
{ | ||
$choiceList = function (Options $options) { | ||
return new ModelChoiceList( | ||
$options['class'], | ||
$options['property'], | ||
$options['choices'], | ||
$options['query'], | ||
$options['group_by'] | ||
); | ||
}; | ||
|
||
$defaultOptions = array( | ||
'template' => 'choice', | ||
'multiple' => false, | ||
|
@@ -40,23 +51,10 @@ public function getDefaultOptions(array $options) | |
'property' => null, | ||
'query' => null, | ||
'choices' => null, | ||
'choice_list' => $choiceList, | ||
'group_by' => null, | ||
'by_reference' => false, | ||
); | ||
|
||
$options = array_replace($defaultOptions, $options); | ||
|
||
if (!isset($options['choice_list'])) { | ||
$defaultOptions['choice_list'] = new ModelChoiceList( | ||
$options['class'], | ||
$options['property'], | ||
$options['choices'], | ||
$options['query'], | ||
$options['group_by'] | ||
); | ||
} | ||
|
||
return $defaultOptions; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why removing the return statements for the default options ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, this is a typo. There should have been a return above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
public function getParent(array $options) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo:
return
instead of$return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed.