I have a form with multi-select dropdown list. Faced a problem with its saving. There are 3 enitities: Client, Client Tag, Tag.
Part of Client Tag entity:
/**
* @ORM\Column(
* type="date",
* nullable=true
* )
* @JMS\Groups({"list", "single"})
*
* @var \DateTime
*/
protected $dateExpiry;
/**
* @ORM\ManyToOne(targetEntity="Client")
* @ORM\JoinColumn(name="client_id", referencedColumnName="id")
* @JMS\Groups("list")
*
* @var Client
*/
protected $client;
/**
* @ORM\ManyToOne(targetEntity="Tag")
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
* @JMS\Groups({"list", "single"})
*
* @var Tag
*/
protected $tag;
Then I have a form on Client Tag
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('client')
->add('tag')
->add('dateExpiry', DateTimeType::class, array(
'widget' => 'single_text',
'format' => 'yyyy-MM-dd', ))
;
}
With single select dropdown list I was sending values like:
{tag: 1, client: 1, dateExpire: 2000-10-10}
And it worked fine.
Now when I switched to multiselect dropdown it is not working anymore, so Im sending data like:
{tag: [1, 2], client: 1, dateExpire: 2000-10-10}
Error "tag:["This value is not valid."]".
I already spend many hours with this problem, please help.
UPD: Im rendering the form like:
{% verbatim %}
<md-input-container >
<label>Tag</label>
<md-content >
<md-select multiple class="select-position" required ng-model="formData.tags" placeholder="Select a tag">
<md-option ng-value="tag.id" ng-repeat="tag in allTags">{{ tag.name }}
</md-option>
</md-select>
</md-content>
</md-input-container>
<mdp-date-picker mdp-placeholder="Expire date" mdp-min-date="minTagExpireDate"
mdp-format="YYYY-MM-DD" ng-model="formData.dateExpiry"></mdp-date-picker>
<div data-ng-messages="errors" role="alert" data-ng-show="form.$submitted">
<div data-ng-message="tag" class="autocomplete-error">Tag not found.</div>
</div>
{% endverbatim %}
And sending it like (part of code):
formVm.customData = angular.copy(formVm.formData);
formVm.customData.client = clientId;
formVm.customData.dateExpiry = $filter('date')(formVm.customData.dateExpiry, 'yyyy-MM-dd');
return $http.post(router.generate('api_client_tag_create'), formVm.customData);
Well, problem is he is using Angular. In that code to me it looks like the values for the tags are coming from a DB and it's not the easy case in the docs where the values are hardcoded.
My 2 cents suggestion for this case would be to use in the controller:
to collect all form's data or
That kind of syntax to collect just the data coming from a single field.
Then he would have to run a query against the entity manager, gather the data with all the possible options from the db, and then use
http://php.net/manual/ro/function.in-array.php
https://symfony.com/doc/current/doctrine.html
This is how I would solve it should you need to get the tags from the db. Because you need to verify in the backend if the user for example with Google Chrome has modified the HTML inputting other values.