Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrconstruction/da0b7b54f8fcde7fe94c10406ee6438a to your computer and use it in GitHub Desktop.
Save andrconstruction/da0b7b54f8fcde7fe94c10406ee6438a to your computer and use it in GitHub Desktop.
Collection Validation
<?php
namespace Application\Form;
use Application\Entity\ProductDetails;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
class ProductDetailsFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct(ObjectManager $objectManager)
{
parent::__construct('productDetails');
$this->setHydrator(new DoctrineHydrator($objectManager))
->setObject(new ProductDetails());
$this->add([
'type' => 'Zend\Form\Element\Hidden',
'name' => 'id',
]);
$this->add([
'type' => 'Zend\Form\Element\Text',
'name' => 'description',
'options' => [
'label' => 'Opis',
],
]);
$this->add([
'type' => 'Zend\Form\Element\Select',
'name' => 'language',
'options' => [
'label' => 'Język',
//'empty_option' => 'Wybierz język',
'value_options' => [
'pl' => 'polski',
'cs' => 'czeski',
'en' => 'angielski',
],
],
]);
}
public function getInputFilterSpecification()
{
return [
'id' => [
'required' => false,
],
'description' => [
'required' => true,
'filters' => [
['name' => 'Zend\Filter\StringTrim'],
['name' => 'Zend\Filter\StripTags'],
],
'validators' => [
['name' => 'Zend\Validator\StringLength',
'options' => [
'max' => 500
],
],
],
],
'language' => [
'required' => true,
],
];
}
}
<?php
namespace Application\Form;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Form;
class ProductEditionForm extends Form
{
public function __construct(ObjectManager $objectManager, $formManager)
{
parent::__construct('product-edition');
$this->setHydrator(new DoctrineHydrator($objectManager));
$productFieldset = $formManager->get('product-fieldset');
$productFieldset->setUseAsBaseFieldset(true);
$this->add($productFieldset);
$this->add([
'type' => 'Zend\Form\Element\Submit',
'name' => 'submit',
'attributes' => [
'value' => 'Wyślij'
],
]);
$this->setValidationGroup([
'product' => [
'name',
'code',
'manufacturer',
'productDetails',
'type',
'prices',
'sizes',
'colors'
],
]);
}
}
<?php
namespace Application\Form;
use Application\Entity\Product;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Element;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Application\Form\TypeFieldset;
class ProductFieldset extends Fieldset implements InputFilterProviderInterface
{
protected $productFieldset;
public function __construct(ObjectManager $objectManager)
{
parent::__construct('product');
$this->setHydrator(new DoctrineHydrator($objectManager))
->setObject(new Product());
$this->setLabel('Produkt');
$this->add([
'type' => 'Zend\Form\Element\Hidden',
'name' => 'id',
]);
$this->add([
'type' => 'Zend\Form\Element\Text',
'name' => 'name',
'options' => [
'label' => 'Nazwa',
],
]);
$this->add([
'type' => 'Zend\Form\Element\Text',
'name' => 'code',
'options' => [
'label' => 'Kod produktu',
],
]);
$this->add([
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'isSale',
'options' => [
'label' => 'Promocja',
],
]);
$this->add([
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'active',
'options' => [
'label' => 'Aktywny',
],
]);
}
public function init()
{
$this->add([
'type' => 'manufacturerSelectFieldset',
'name' => 'manufacturer',
'options' => [
'label' => 'Producent',
],
]);
$this->add([
'type' => 'typeSelectFieldset',
'name' => 'type',
'options' => [
'label' => 'Kategoria',
],
]);
$this->add([
'type' => Element\Collection::class,
'name' => 'prices',
'options' => [
'label' => 'Ceny',
'count' => 1,
'should_create_template' => true,
'allow_add' => true,
'target_element' => [
'type' => 'priceFieldset',
],
],
]);
$this->add([
'type' => Element\Collection::class,
'name' => 'productDetails',
'options' => [
//'label' => 'Opisy',
'count' => 1,
'should_create_template' => true,
'allow_add' => true,
'target_element' => [
'type' => 'productDetailsFieldset',
],
],
]);
$this->add([
'type' => Element\Collection::class,
'name' => 'sizes',
'options' => [
'label' => 'Rozmiary',
'count' => 0,
'should_create_template' => true,
'allow_add' => true,
'target_element' => [
'type' => 'sizeFieldset',
],
],
]);
$this->add([
'type' => Element\Collection::class,
'name' => 'colors',
'options' => [
'label' => 'Kolory',
'count' => 0,
'should_create_template' => true,
'allow_add' => true,
'target_element' => [
'type' => 'colorFieldset',
],
],
]);
}
public function getInputFilterSpecification()
{
return [
'id' => [
'required' => false,
],
'name' => [
'required' => true,
'filters' => [
['name' => 'Zend\Filter\StringTrim'],
['name' => 'Zend\Filter\StripTags'],
],
'validators' => [
['name' => 'Zend\Validator\StringLength',
'options' => [
'max' => 50,
],
],
],
],
'code' => [
'required' => false,
'filters' => [
['name' => 'Zend\Filter\StringTrim'],
['name' => 'Zend\Filter\StripTags'],
],
'validators' => [
['name' => 'Zend\Validator\StringLength',
'options' => [
'max' => 30,
],
],
],
],
'productDetails' => [
'required' => true,
'validators' => [
['name' => 'Callback',
'options' => [
'callback' => [$this, 'checkIfUnique'],
],
],
],
],
];
}
public function checkIfUnique($value, $context)
{
$splitted = [];
foreach ($context['productDetails'] as $pd)
{
$splitted[] = $pd['language'];
}
$counted = array_count_values($splitted);
foreach ($counted as $unit)
{
if ($unit > 1)
{
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment