last day (15 days later) » 

10:11 PM
2
A: Merging custom and default options in Symfony form AbstractType defined in setOptionDefaults()

RamyDefault options are not made to merge. That's the expected behavior. They are called default because they only exist if you did not define them. You can read more about the interface here Here is the part of the code that overrides your options in OptionResolver: // Make sure this method can be...

 
1. If I don't define my values in an options array, how can I pass them into $form = $this->createForm(new ViewType, $view, $myOptions);? The options I pass there, end up as $options['options'] in my formType. I need to be able to pass options from my controller to the custom formType.
2. I read about the OptionsResolver component. Can this be used in any object? It seems very powerful, but still there is no method for merging options? How is that not a common requirement for others? I need to set default values so that my formType can check if they exist without giving errors, but I need to be able to overwrite the defaults (from the controller or a parent formType) on a case by case basis. How "should" I do that?
 
You can always consider passing your options in the ViewType __construct($options) { $this->options = $options; }. $form = $this->createForm(new ViewType($myOptions), $view);
OptionsResolver is the one used by forms in symfony btw. that's where setDefaults() come from. You have access to all the interface there.
 
I still can't understand why it's so difficult to merge custom options with default options. I must be structuring something incorrectly if no one else has this problem, i.e. there must be a better way to set default options for the formType and pass in custom options (from controller or other parent form types) to overwrite specific defaults without changing others. What is that better method?
I can easily custom code something, but I want to do it the "Symfony" recommended way. If I pass in options into the construct as you suggested, they aren't related to the resolvers default options. So I'm bypassing the built in formType methods and the existing options parameter which seems like it was created for passing options into form types...
Also, per your statement "They are called default because they only exist if you did not define them." This is the behavior I want. So I think the problem is that I am defining my defaults in an array (to keep them sanely grouped), the optionsResolver#setDefaults() doesn't do an array_replace_recursive() it just does a loop on the values and overwrites them at the top level... So... I could write my own custom setDefaults() that did a recursive merge. Is that a bad idea? Should I change it and submit a pull request?
FYI, Evidently there has been discussion about this problem on gitbhub for over a year: github.com/symfony/symfony/issues/4833
 
I updated the two main code issues that I believe is non standard and can be the main reason behind the issues you are having.
 
My ViewType extends AbstractType, and has the standard fields for View. The reason I then create ContentSeoEditType extends ViewType is so that my controller can load the formType that is the same name as the controller (to do this dynamically), AND so that I can add more fields if I need to. So I am using this like a normal PHP extend. It works well, but you are saying this confuses symfony somehow? I tried to make it do what you say but when I make it extend AbstractType instead of ViewType and getParent returns ViewType, it gives error "Could not load type 'ViewType'".
I don't understand how to use anything other than 'options', since that is the only value that I can pass in from the controller when I load the form type. Can you show me some code to illustrate what you are referring to?
Even if I change options to 'myOptions', and make them one level (not an array), it still doesn't merge them...
 
10:11 PM
$form = $this->createForm(new ViewType(), $view, array('customOption' => $somethingImportant));
getParent() should return the same that the parent form is returning in getName() .. so if viewType is returning viewform in getName() .. then to create a child form you need getParent() to return viewform. Check how symfony form types are created to get an idea how the symfony way works
in setDefaults() add customOptions => null and it will be considered a known option and will allow you to add it.
 
when I use the ContentSeoEditType extends AbstractType along with getParent() { return 'view'; }, I do not get all the code defined in ViewType#buildForm() which is the entire reason why I wanted to extend ViewType (because all my content editing pages are editing different aspects of the View and ViewVersion entities, so we just reuse the same code).
 

last day (15 days later) »