mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 17:10:53 +00:00
[CoreBundle] Add validator for unique field during creating
This commit is contained in:
parent
3dbab35ad6
commit
641651b75e
6 changed files with 194 additions and 1 deletions
|
|
@ -33,5 +33,8 @@
|
|||
<argument>%currency%</argument>
|
||||
<tag name="validator.constraint_validator" alias="sylius_cannot_disable_currency" />
|
||||
</service>
|
||||
<service id="sylius.validator.unique.image_code" class="Sylius\Bundle\CoreBundle\Validator\Constraints\ImageUniqueCodeValidator">
|
||||
<tag name="validator.constraint_validator" alias="image_unique_code" />
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
||||
|
|
|
|||
|
|
@ -14,7 +14,9 @@
|
|||
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
|
||||
<class name="Sylius\Component\Core\Model\Taxon">
|
||||
<property name="images">
|
||||
<constraint name="Valid" />
|
||||
<constraint name="Sylius\Bundle\CoreBundle\Validator\Constraints\ImageUniqueCode">
|
||||
<option name="message">sylius.taxon_image.code.unique</option>
|
||||
</constraint>
|
||||
</property>
|
||||
</class>
|
||||
</constraint-mapping>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Sylius\Bundle\CoreBundle\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* @author Anna Walasek <anna.walasek@lakion.com>
|
||||
*/
|
||||
class ImageUniqueCode extends Constraint
|
||||
{
|
||||
public $message = 'This code should be unique.';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validatedBy()
|
||||
{
|
||||
return get_class($this).'Validator';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Sylius\Bundle\CoreBundle\Validator\Constraints;
|
||||
|
||||
use Sylius\Component\Core\Model\ImageInterface;
|
||||
use Sylius\Component\Resource\Repository\RepositoryInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
||||
/**
|
||||
* @author Anna Walasek <anna.walasek@lakion.com>
|
||||
*/
|
||||
class ImageUniqueCodeValidator extends ConstraintValidator
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($images, Constraint $constraint)
|
||||
{
|
||||
/** @var ImageInterface[] $images */
|
||||
foreach ($images as $key => $image) {
|
||||
$filteredImages = $images->filter(function(ImageInterface $imageFromObject) use ($image) {
|
||||
return $imageFromObject->getOwner() === $image->getOwner()
|
||||
&& $imageFromObject->getCode() === $image->getCode()
|
||||
&& $imageFromObject !== $image
|
||||
;
|
||||
});
|
||||
|
||||
if(0 !== count($filteredImages)) {
|
||||
$this->context->addViolationAt(sprintf('[%d].code', $key), $constraint->message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace spec\Sylius\Bundle\CoreBundle\Validator\Constraint;
|
||||
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Prophecy\Argument;
|
||||
use Sylius\Bundle\CoreBundle\Validator\Constraints\ImageUniqueCode;
|
||||
use Sylius\Bundle\CoreBundle\Validator\Constraints\ImageUniqueCodeValidator;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* @mixin ImageUniqueCode
|
||||
*
|
||||
* @author Anna Walasek <anna.walasek@lakion.com>
|
||||
*/
|
||||
class ImageUniqueCodeSpec extends ObjectBehavior
|
||||
{
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType(ImageUniqueCode::class);
|
||||
}
|
||||
|
||||
function it_extends_symfony_constraint_class()
|
||||
{
|
||||
$this->shouldHaveType(Constraint::class);
|
||||
}
|
||||
|
||||
function it_is_validate_by_unique_field_during_creation_validator()
|
||||
{
|
||||
$this->validatedBy()->shouldReturn(ImageUniqueCodeValidator::class);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace spec\Sylius\Bundle\CoreBundle\Validator\Constraint;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Prophecy\Argument;
|
||||
use Sylius\Bundle\CoreBundle\Validator\Constraints\ImageUniqueCode;
|
||||
use Sylius\Bundle\CoreBundle\Validator\Constraints\ImageUniqueCodeValidator;
|
||||
use Sylius\Component\Core\Model\ImageAwareInterface;
|
||||
use Sylius\Component\Core\Model\ImageInterface;
|
||||
use Sylius\Component\Resource\Repository\RepositoryInterface;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
||||
/**
|
||||
* @mixin ImageUniqueCodeValidator
|
||||
*
|
||||
* @author Anna Walasek <anna.walasek@lakion.com>
|
||||
*/
|
||||
final class ImageUniqueCodeValidatorSpec extends ObjectBehavior
|
||||
{
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType(ImageUniqueCodeValidator::class);
|
||||
}
|
||||
|
||||
function it_extends_constraint_validator()
|
||||
{
|
||||
$this->shouldHaveType(ConstraintValidator::class);
|
||||
}
|
||||
|
||||
function it_adds_violation_if_there_two_images_with_the_same_owner_which_have_same_codes(
|
||||
ImageInterface $firstImage,
|
||||
ImageInterface $secondImage,
|
||||
ImageAwareInterface $owner
|
||||
) {
|
||||
$firstImage->getOwner()->willReturn($owner);
|
||||
$secondImage->getOwner()->willReturn($owner);
|
||||
|
||||
$firstImage->getCode()->willReturn('car');
|
||||
$secondImage->getCode()->willReturn('car');
|
||||
|
||||
$this->bulidViolation('[0].code', Argument::type('string'))->shouldBeCalled();
|
||||
$this->bulidViolation('[1].code', Argument::type('string'))->shouldBeCalled();
|
||||
|
||||
$this->validate(new ArrayCollection($firstImage->getWrappedObject(), $secondImage->getWrappedObject()), new ImageUniqueCode());
|
||||
}
|
||||
|
||||
function it_does_not_add_violation_if_there_is_no_duplication_of_a_code(
|
||||
ImageInterface $firstImage,
|
||||
ImageInterface $secondImage,
|
||||
ImageAwareInterface $owner
|
||||
) {
|
||||
$firstImage->getOwner()->willReturn($owner);
|
||||
$secondImage->getOwner()->willReturn($owner);
|
||||
|
||||
$firstImage->getCode()->willReturn('car');
|
||||
$secondImage->getCode()->willReturn('wipers');
|
||||
|
||||
$this->bulidViolation('[0].code', Argument::type('string'))->shouldNotBeCalled();
|
||||
$this->bulidViolation('[1].code', Argument::type('string'))->shouldNotBeCalled();
|
||||
|
||||
$this->validate(new ArrayCollection($firstImage->getWrappedObject(), $secondImage->getWrappedObject()), new ImageUniqueCode());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue