How update dynamically a form when we add a new property to the entity related - symfony

I just read the documentation of Symfony about Form Events, but I don't really understand.
How to add dynamically fields in a form when whe add or edit a new property in the entity class ?
For example
I have a entity named Product with two properties : $name and $price.
I create a form related to this entity with all properties.
I add a new property $createdAt in the entity
The form added automatically a new line : ->add('createdAt')

Related

Doctrine inheritance and associations

I have entity Image with properties:
class Image {
private $id
private $name;
private $mimeType;
private $size; }
Now, I need to add new image type which will have all properties as existing class and some new properties like author name, external url, url to author... Let's call it ExternalImage. Existing images don't need these properties.
Then I need to add image property to Event class and it can be either Image or ExternalImage.
One other use case is that I need to list all images (both types at once, on some API endpoint).
My solution would be:
ExternalImage extends Image
.. add new properties
and use SINGLE TABLE Doctrine's inheritance. And then add OneToOne association to Event class (adding image_id property).
<one-to-one field="image" target-entity="App\Common\Entity\Image">
is inheritance appropriate for this example? I don't see any other solution from my perspective. We have strong "is a" relation between Image and ExternalImage.
I am aware of performance issue with using STI entity as target entity of one-to-one association - image entity won't be lazily loaded when I fetch Event object. Can I do something about that?

Symfony forms ChoiceType how to add new choices from user's input

I am using js library that allows user to add new values into html select element but I cannot find a way handing that new values from symfony side and dynamically add new option.
As one of the solutions I was trying to use 'choice_loader' options with default ChoiceType field and implementing ChoiceLoaderInterface like in this post but there should be a simpler way.
I don't think you can do it in an other way with a ChoiceType but maybe you can create you own custom type :
You can create your select element by customize your form rendering or/and default classes set in your custom FormType.
You can use a ModelTransformer in order to retrieve your existing values and create new ones
You can use a simple TextType in inheritance format the data send by the form with a delimiter and create a ViewTransformer in order to retrieve a collection on the other side

How to "allow edit" newly added fields in a form? for Dynamics AX (AX7/D365)

I've added a new field to my form EcoResProductDetailsExtended, but when I click edit it does not allow me to edit it. The properties for allowing edit is already set to Yes. The form contains the method setAllowEditFields() and setAllowEditField() but it is private so it means I can't make an extension of it nor call it.
Is there anyway or method that I can allow the form to edit my newly added fields?
Check the AllowEdit property in 3 locations:
The table field
\Data Dictionary\Tables\InventTable\Fields\ABCValue
The form datasource field
\Forms\EcoResProductDetailsExtended\Data Sources\InventTable\Fields\ABCValue
The form control
\Forms\EcoResProductDetailsExtended\Designs\DesignList\CostABC_ABCValue
Also, the datasource should allow edit, the Edit button be activated, permissions allow edit etc.
Although method setAllowEditFields is private, it is called from public method setItemStockedDependentObjects. You can create a post-event handler for setItemStockedDependentObjects and make your field editable there.
[PostHandlerFor(formStr(EcoResProductDetailsExtended), formMethodStr(EcoResProductDetailsExtended, setItemStockedDependentObjects))]
public static void Post_setItemStockedDependentObjects(XppPrePostArgs args)
{
// your code here
}

Symfony2 - Saving child and parent from the same form

I'm new to Symfony2 and i've been struggling with an issue handling a form submission. I can't help feeling i'm missing something stupid, but after googling and not findind any solutions I thought i'd post my problem here.
So, I have a form to create a child entity and edit a couple fields on a parent entity. To build the form i've used the FormBuilder, where i added two form types (one for the child and one for the parent). The parent type includes the ID field for the parent.
On the action that handles the form submission i'm calling handleRequest which fails because:
Neither the property "id" nor one of the methods "setId()", "_set()" or "_call()" exist and have public access in class "Ahms\MyBundle\Entity\Parent".
This happens when the parent id field is part of the form, when it's not part of the form this runs smoothly.
What am I missing?
Thanks!
If you create the Form from child Controller, you have to declare id in child class and have to writ public function setId($id) and public function getId() method.

Entity reference field and dependent dropdown

I have a content type with an entity reference field referencing to a custom entity. I need to use a select box because an autocomplete widget is not suitable in my case. However, I cannot load all the entities at once as selectable values because they are too many (72000+ the form won't even load). So I default the entity reference select box to a limited number of values using a views filter and then hide it by default. Then I use an ajax dependent dropdown to show and populate the entity reference select box with filtered down values (I'm using a module that implements hook_form_alter).
My problem is that the form won't validate because now I can select entity reference values which are not the default ones in the select box. So I guess I should control in some way the validation rules of the entity reference field. Is there an easy way to do this? Which hook should I use?
Set the entity reference field to autocomplete and take it out of the process entirely in your form alter with $form['field_entity_ref']['#access'] = FALSE. This should fix the validation problem. (of course, "field_entity_ref" is what I'm calling your actual reference field.
Add your own validation to the form, if that is still necessary.
Finally, implement hook_node_presave() to manually put the value of your custom ajax drop down box.
So if your custom ajax select box was named my_custom_ref, then it would look something like this:
function mymodule_node_presave($node) {
if (isset($node->my_custom_ref)) {
$node->field_entity_ref[$node->language][0]['target_id'] = $node->my_custom_ref;
}
}

Resources