validation attempt with assert\callback: still doesn't work

This commit is contained in:
Julie Lenaerts 2021-12-02 15:31:08 +01:00
parent acacef936c
commit c527b1b3cf
2 changed files with 28 additions and 7 deletions

View File

@ -49,10 +49,6 @@ use UnexpectedValueException;
* "accompanying_period": AccompanyingPeriod::class
* })
* @Assert\GroupSequenceProvider
* @Assert\Expression(
* "this.isConfidential && this.getUser && this.getStep === 'CONFIRMED'",
* message="If the accompanying course is confirmed and confidential, a referrer must remain assigned."
* )
*/
class AccompanyingPeriod implements
TrackCreationInterface,
@ -860,7 +856,7 @@ class AccompanyingPeriod implements
}
/**
* Validation function.
* Validation functions.
*/
public function isDateConsistent(ExecutionContextInterface $context)
{
@ -875,6 +871,21 @@ class AccompanyingPeriod implements
}
}
/**
* @Assert\Callback
*/
public function canUserBeNull(ExecutionContextInterface $context)
{
if ($this->getStep() === self::STEP_CONFIRMED && $this->isConfidential() === true)
{
if (!$this->getUser()) {
$context->buildViolation('User cannot be null for an accompanying period that is confirmed and confidential')
->atPath('user')
->addViolation();
}
}
}
public function isEmergency(): bool
{
return $this->emergency;

View File

@ -10,6 +10,8 @@
namespace Chill\PersonBundle\Tests\AccompanyingPeriod;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Scope;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Origin;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Validator\Validator\ValidatorInterface;
@ -34,8 +36,13 @@ class AccompanyingPeriodConfidentialTest extends KernelTestCase
$period = new AccompanyingPeriod();
$period->setConfidential(true);
$period->setStep('CONFIRMED');
$period->setIntensity('regular');
$period->setOrigin(new Origin());
$period->addScope(new Scope());
$violations = self::$validator->validate($period, []);
var_dump($period);
$violations = self::$validator->validate($period);
$this->assertCount(1, $violations);
}
@ -45,9 +52,12 @@ class AccompanyingPeriodConfidentialTest extends KernelTestCase
$period = new AccompanyingPeriod();
$period->setConfidential(true);
$period->setStep('CONFIRMED');
$period->setIntensity('regular');
$period->setOrigin(new Origin());
$period->addScope(new Scope());
$period->setUser(new User());
$violations = self::$validator->validate($period, []);
$violations = self::$validator->validate($period);
$this->assertCount(0, $violations);
}