-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[project-base] fixed product edit in development environment (#3544)
- Loading branch information
1 parent
e38a8be
commit 83c7dc6
Showing
9 changed files
with
145 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
assets/js/admin/validation/form/validationUniqueProductCatnum.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
(function ($, window) { | ||
window.ShopsysFrameworkBundleFormConstraintsUniqueProductCatnum = function () { | ||
this.message = null; | ||
|
||
this.validate = (value, element) => { | ||
const $catnumInput = $('#' + element.id); | ||
|
||
const url = $catnumInput.data('unique-catnum-url'); | ||
const currentProductCatnum = $catnumInput.data('current-product-catnum'); | ||
|
||
if (url === undefined) { | ||
return []; | ||
} | ||
|
||
FpJsFormValidator.ajax.sendRequest( | ||
url, | ||
{ | ||
catnum: value, | ||
currentProductCatnum | ||
}, | ||
(response) => { | ||
const catnumExists = JSON.parse(response); | ||
|
||
if (catnumExists) { | ||
$catnumInput.jsFormValidator('showErrors', { | ||
errors: [this.message], | ||
sourceId: 'form-error-' + String(element.id).replace(/_/g, '-') | ||
}); | ||
} | ||
} | ||
); | ||
|
||
return []; | ||
}; | ||
}; | ||
|
||
})(jQuery, window); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrameworkBundle\Form\Constraints; | ||
|
||
use Shopsys\FrameworkBundle\Model\Product\Product; | ||
use Symfony\Component\Validator\Constraint; | ||
|
||
class UniqueProductCatnum extends Constraint | ||
{ | ||
public string $message = 'Product with entered catalog number already exists'; | ||
|
||
public ?Product $product = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrameworkBundle\Form\Constraints; | ||
|
||
use Shopsys\FrameworkBundle\Model\Product\ProductFacade; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
class UniqueProductCatnumValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* @param \Shopsys\FrameworkBundle\Model\Product\ProductFacade $productFacade | ||
*/ | ||
public function __construct( | ||
protected readonly ProductFacade $productFacade, | ||
) { | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @param \Symfony\Component\Validator\Constraint $constraint | ||
*/ | ||
public function validate($value, Constraint $constraint): void | ||
{ | ||
if (!$constraint instanceof UniqueProductCatnum) { | ||
throw new UnexpectedTypeException($constraint, UniqueProductCatnum::class); | ||
} | ||
|
||
if ($value === null || $value === '') { | ||
return; | ||
} | ||
|
||
if ($constraint->product !== null && $value === $constraint->product->getCatnum()) { | ||
return; | ||
} | ||
|
||
$productByCatnum = $this->productFacade->findByCatnum($value); | ||
|
||
if ($productByCatnum === null) { | ||
return; | ||
} | ||
|
||
$this->context->addViolation($constraint->message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters