Express object creation without specifying association object (Many-to-Many)

Hi everyone,

I am trying to add some express objects and in my use case I need to use Many to Many relation for two object types like Teachers and Students… So I created object types on settings/express and defined association as many to many. On form, however, I want to give user flexibility to select associated objects or do not select at all. So, for example, I create a teacher object but dont want to assign any students yet. Its because maybe we do not have any students there yet…

The problem is when I do not select any student object on my teacher object creation, I receive error like “You must select a valid Student”.

Any advice is welcome

Thanks

Murat

@muratyil Can you describe your express setup for the teachers in a little more detail?
For Example what are the details, associations for the teachers.

Thanks!
Jess

First of all, important update that I realize: This is happening only on v8. On v9 I got no problem about this.

You reproduce this problem like this: Create two express objects on dashboard both of which has many to many relation with each other, maybe add extra attributes, I added name for example.

In forms part, you add association and it shouldnt be required. So now, we should have two data object types with no entries. Lets say you want to add a teacher object, you got ‘There is no valid students’ error message…

I attach some screenshots, if you need more details, please just ask.

Thanks in advance


It’s Express bug.
If it wasn’t fixed in latest 8.x version, then you have to do it by yourself.
Here is the file that I was using.
Read description for instructions.

<?php

/*
Bug: You can't add Express entry with empty association in 8.x version
https://github.com/concrete5/concrete5/issues/9428
Solution
https://github.com/concrete5/concrete5/commit/b5375bfa54a8d58d5a424784f7e127e9ab538b2a#diff-816ae1f5e226b6ab522591fa6bfef766851d181742d350b32c49ecaab9bff25f

1) Copy this modified file to:
application/src/Express/Form/Control/Validator/AssociationControlValidator.php

2) Skip this part if you are auto-loading classes from src folder:
application/bootstrap/app.php
$classLoader = new \Symfony\Component\ClassLoader\Psr4ClassLoader();
$classLoader->addPrefix('Application', DIR_APPLICATION . '/' . DIRNAME_CLASSES);
$classLoader->register();

3) Override core file
application/bootstrap/app.php
$app->bind('\Concrete\Core\Express\Form\Control\Validator\AssociationControlValidator', function() {
    return new Application\Express\Form\Control\Validator\AssociationControlValidator(new Concrete\Core\Error\ErrorList\ErrorList());
});
*/
namespace Application\Express\Form\Control\Validator;

use Concrete\Core\Entity\Express\Control\AssociationControl;
use Concrete\Core\Entity\Express\Control\Control;
use Concrete\Core\Error\ErrorList\ErrorList;
use Symfony\Component\HttpFoundation\Request;

class AssociationControlValidator implements \Concrete\Core\Express\Form\Control\Validator\ValidatorInterface
{
    /**
     * @var ErrorList
     */
    protected $errorList;

    public function __construct(ErrorList $errorList)
    {
        $this->errorList = $errorList;
    }

    public function validateRequest(Control $control, Request $request)
    {
        if ($control->isRequired()) {
            $associationValue = $request->request->get('express_association_' . $control->getId());
            if (!$associationValue) {
                /**
                 * @var AssociationControl
                 */
                $this->errorList->add(t('You must select a valid %s', $control->getAssociation()->getTargetEntity()->getName()));
            }
        }

        return $this->errorList;
    }
}

Thank you for post the bug!