Bug #40064

Multiselect is not getting persisted

Added by Christoph Gross about 7 years ago. Updated over 5 years ago.

Status:
New
Priority:
Must have
Assignee:
-
Category:
ViewHelpers
Target version:
-
Start date:
2012-08-22
Due date:
% Done:

0%

Has patch:
No

Description

I have a statement and want to assign multiple users to it using a multiselect box. The mulitselect box displays already selected users(selected by a workaround) correctly, but fails to change the selected users, they remain unchanged.

I attached a screenshot of the page.

The template code:

<f:form controller="Statement" arguments="{statement: statement}" action="updateUsers" name="Statement" object="{statement}">
  <f:form.select property="users" options="{users}" optionLabelField="displayName" optionValueField="uid" multiple="5" />
  ...

The statement controller:

/**
     * STEP 2: Select users
     *
     * @param \Drinkaccounting\Domain\Model\Statement $statement The statement
     * @return void
     */
    public function selectUsersAction(Statement $statement) {
        $this -> view -> assign('statement', $statement);
        $this -> view -> assign('users', $this -> userRepository -> findAll());
    }

    /**
     * STEP 2: Update selected users
     *
     * @param \Drinkaccounting\Domain\Model\Statement $statement The statement
     * @param array $selectedUsers
     * @return void
     */
    public function updateUsersAction(Statement $statement) {
        $this -> statementRepository -> update($statement);
        $this -> addFlashMessage('Updated users');
        $this -> redirect('selectUsers', 'Statement', NULL, array('statement' => $statement));
    }

The statement model:

/**
 * A Statement
 *
 * @FLOW3\Entity
 */
class Statement {

    /**
     * The products
     * @var \Doctrine\Common\Collections\Collection<\Drinkaccounting\Domain\Model\Product>
     * @ORM\ManyToMany
     */
    protected $products;
...

My workaround to select users without using the property attribute:
This workaround fails with an Exception if you try to update with no item selected, because then flow3 thinks selectedUsers is a string instead of an array. I haven't figured out a way to read and change the content of an argument in the initializeUpdateUsersAction. Anyway this can't be a final solution.

The template:

<f:form controller="Statement" arguments="{statement: statement, selectedUsers: selectedUsers}" action="updateUsers" name="Statement" object="{statement}">    

        <f:form.select name="selectedUsers" value="{statement.users}" options="{users}" optionLabelField="displayName" multiple="20" />

The statement controller:

/**
     * STEP 2: Select users
     *
     * @param \Drinkaccounting\Domain\Model\Statement $statement The statement
     * @return void
     */
    public function selectUsersAction(Statement $statement) {
        $this -> view -> assign('statement', $statement);
        $this -> view -> assign('users', $this -> userRepository -> findAll());
    }

    /**
     * STEP 2: Update selected users
     *
     * @param \Drinkaccounting\Domain\Model\Statement $statement The statement
     * @param array $selectedUsers
     * @return void
     */
    public function updateUsersAction(Statement $statement, $selectedUsers) {
        // Clear users
        $statement -> clearUsers();
        // Get user objects by identifier
        foreach ($selectedUsers as $userID) {
            $user = $this -> persistenceManager -> getObjectByIdentifier($userID, '\Drinkaccounting\Domain\Model\User');
            $statement -> addUser($user);
        }
        $this -> statementRepository -> update($statement);
        $this -> addFlashMessage('Updated users');
        $this -> redirect('selectUsers', 'Statement', NULL, array('statement' => $statement));
    }

Bildschirmfoto_2012-08-22_um_16.53.48.png View - The multiselect box (21.5 KB) Christoph Gross, 2012-08-22 16:55

History

#2 Updated by Rik Willems over 5 years ago

This seems to work out of the box for me, using Flow 2.2.

Also available in: Atom PDF