mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-07 17:04:30 +02:00
Validate that a parts name fullfills the regular expression set in a category
This commit is contained in:
parent
58105575d3
commit
22950f2476
5 changed files with 40 additions and 1 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue