diff --git a/src/Entity/Parts/Part.php b/src/Entity/Parts/Part.php index 51573138..bcd87e77 100644 --- a/src/Entity/Parts/Part.php +++ b/src/Entity/Parts/Part.php @@ -41,6 +41,7 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Validator\Constraints as Assert; +use Symfony\Component\Validator\Context\ExecutionContextInterface; /** * Part class. @@ -147,4 +148,23 @@ class Part extends AttachmentContainingDBElement } parent::__clone(); } + + /** + * @Assert\Callback + */ + public function validate(ExecutionContextInterface $context, $payload) + { + //Ensure that the part name fullfills the regex of the category + if ($this->category) { + $regex = $this->category->getPartnameRegex(); + if (!empty($regex)) { + if (!preg_match($regex, $this->name)) { + $context->buildViolation('part.name.must_match_category_regex') + ->atPath('name') + ->setParameter('%regex%', $regex) + ->addViolation(); + } + } + } + } } diff --git a/src/Form/AdminPages/CategoryAdminForm.php b/src/Form/AdminPages/CategoryAdminForm.php index 018b97a6..10a56646 100644 --- a/src/Form/AdminPages/CategoryAdminForm.php +++ b/src/Form/AdminPages/CategoryAdminForm.php @@ -76,6 +76,7 @@ class CategoryAdminForm extends BaseEntityAdminForm 'required' => false, 'empty_data' => '', 'label' => 'category.edit.partname_regex', + 'help' => 'category.edit.partname_regex.help', 'attr' => [ 'placeholder' => 'category.edit.partname_regex.placeholder', ], diff --git a/templates/Parts/edit/_main.html.twig b/templates/Parts/edit/_main.html.twig index 236e1fab..f153d878 100644 --- a/templates/Parts/edit/_main.html.twig +++ b/templates/Parts/edit/_main.html.twig @@ -2,7 +2,7 @@ {% if part.category is not null and part.category.partnameHint is not empty %}
{% trans %}part.edit.name.category_hint{% endtrans %}: {{ part.category.partnameHint }}
+{% trans %}part.edit.name.category_hint{% endtrans %}: {{ part.category.partnameHint }}