Added possibility to change the main product reference

This commit is contained in:
Julien Chanséaume
2014-07-21 15:01:49 +02:00
committed by Julien Chanseaume
parent f37625e5cf
commit eaad1afdff
6 changed files with 31 additions and 14 deletions

View File

@@ -22,17 +22,15 @@ class ProductCreationForm extends BaseForm
{
protected function buildForm($change_mode = false)
{
$ref_constraints = array(new NotBlank());
if (! $change_mode) {
$ref_constraints[] = new Callback(array(
"methods" => array(array($this, "checkDuplicateRef"))
));
}
$this->formBuilder
->add("ref", "text", array(
"constraints" => $ref_constraints,
"constraints" => array(
new NotBlank(),
new Callback(array(
"methods" => array(array($this, "checkDuplicateRef"))
))
),
"label" => Translator::getInstance()->trans('Product reference *'),
"label_attr" => array("for" => "ref")
))

View File

@@ -12,9 +12,12 @@
namespace Thelia\Form;
use Propel\Runtime\ActiveQuery\Criteria;
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\ExecutionContextInterface;
use Thelia\Core\Translation\Translator;
use Thelia\Model\ProductQuery;
class ProductModificationForm extends ProductCreationForm
{
@@ -49,6 +52,23 @@ class ProductModificationForm extends ProductCreationForm
$this->addStandardDescFields(array('title', 'locale'));
}
public function checkDuplicateRef($value, ExecutionContextInterface $context)
{
$data = $context->getRoot()->getData();
$count = ProductQuery::create()
->filterById($data['id'], Criteria::NOT_EQUAL)
->filterByRef($value)->count();
if ($count > 0) {
$context->addViolation(
Translator::getInstance()->trans(
"A product with reference %ref already exists. Please choose another reference.",
array('%ref' => $value)
));
}
}
public function getName()
{
return "thelia_product_modification";