Validate that a parts name fullfills the regular expression set in a category

This commit is contained in:
Jan Böhmer 2023-01-28 21:36:19 +01:00
parent 58105575d3
commit 22950f2476
5 changed files with 40 additions and 1 deletions

View file

@ -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();
}
}
}
}
}

View file

@ -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',
],

View file

@ -2,7 +2,7 @@
{% if part.category is not null and part.category.partnameHint is not empty %}
<div class="row">
<div class="col-sm-9 offset-sm-3">
<p class="text-muted"><b>{% trans %}part.edit.name.category_hint{% endtrans %}:</b> {{ part.category.partnameHint }}</p>
<p class="form-text help-text"><b>{% trans %}part.edit.name.category_hint{% endtrans %}:</b> {{ part.category.partnameHint }}</p>
</div>
</div>
{% endif %}

View file

@ -10401,5 +10401,17 @@ Element 3</target>
<target>Hint from category</target>
</segment>
</unit>
<unit id="_vbPEFP" name="category.edit.partname_regex.placeholder">
<segment>
<source>category.edit.partname_regex.placeholder</source>
<target>e.g "/Capacitor \d+ nF/i"</target>
</segment>
</unit>
<unit id="QFgP__5" name="category.edit.partname_regex.help">
<segment>
<source>category.edit.partname_regex.help</source>
<target>A PCRE-compatible regular expression, which a part name have to match.</target>
</segment>
</unit>
</file>
</xliff>

View file

@ -293,5 +293,11 @@
<target>You have selected less quantity to withdraw than needed for the build! Add additional quantity.</target>
</segment>
</unit>
<unit id="G9ZKt.4" name="part.name.must_match_category_regex">
<segment>
<source>part.name.must_match_category_regex</source>
<target>The part name does not match the regular expression stated by the category: %regex%</target>
</segment>
</unit>
</file>
</xliff>