我想在一个Zend表格Collection中添加独特的元素。 我发现从这个真棒工作阿隆克尔
我做的形式和字段集像阿隆Kerr's例子,它工作正常。
在我来说,我创建表单,从公司门店插入的集合。
我的表
首先,我有这样的一个StoreFieldset 申请\表格\ CompanyStoreForm:
$this->add(array(
'name' => 'company',
'type' => 'Application\Form\Stores\CompanyStoresFieldset',
));
该字段集
申请\表格\店\ CompanyStoresFieldset有实体店这样的集合:
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'stores',
'options' => array(
'target_element' => array(
'type' => 'Application\Form\Fieldset\StoreEntityFieldset',
),
),
));
申请\表格\字段集\ StoreEntityFieldset
$this->add(array(
'name' => 'storeName',
'attributes' => ...,
'options' => ...,
));
//AddressFieldset
$this->add(array(
'name' => 'address',
'type' => 'Application\Form\Fieldset\AddressFieldset',
));
到阿龙Kerrs CategoryFieldset的差异我再增加一个字段集: 应用程序\表格\字段集\ AddressFieldset
申请\表格\字段集\ AddressFieldset具有文本元素streetName。
该Inputfilters
该CompanyStoresFieldsetInputFilter没有元件进行验证。
该StoreEntityFieldsetInputFilter有STORENAME和应用程序\表格\字段集\ AddressFieldset这样的验证
public function __construct() {
$factory = new InputFactory();
$this->add($factory->createInput([
'name' => 'storeName',
'required' => true,
'filters' => array( ....
),
'validators' => array(...
),
]));
$this->add(new AddressFieldsetInputFilter(), 'address');
}
该AddressFieldset有另一个输入过滤AddressFieldsetInputFilter。 在AddressFieldsetInputFilter我增加了对streetName一个输入过滤器。
FormsFactory
将所有Inputfilters以这样的形式
public function createService(ServiceLocatorInterface $serviceLocator) {
$form = $serviceLocator->get('FormElementManager')->get('Application\Form\CompanyStoreForm');
//Create a Form Inputfilter
$formFilter = new InputFilter();
//Create Inputfilter for CompanyStoresFieldsetInputFilter()
$formFilter->add(new CompanyStoresFieldsetInputFilter(), 'company');
//Create Inputfilter for StoreEntityFieldsetInputFilter()
$storeInputFilter = new CollectionInputFilter();
$storeInputFilter->setInputFilter(new StoreEntityFieldsetInputFilter());
$storeInputFilter->setUniqueFields(array('storeName'));
$storeInputFilter->setMessage('Just insert one entry with this store name.');
$formFilter->get('company')->add($storeInputFilter, 'stores');
$form->setInputFilter($formFilter);
return $form;
}
我用阿隆Kerrs CollectionInputFilter。
该STORENAME应该是整个集合中是唯一的。 一切工作正常,到目前为止!
但现在我的问题!
该streetName应该是整个集合中是唯一的。 但元素在AddressFieldset。 我不能做这样的事情:
$storeInputFilter->setUniqueFields(array('storeName', 'address' => array('streetName')));
我想我应该从CollectionInputFilter延长阿隆Kerrs 的isValid()
阿隆Kerrs原始的isValid()
public function isValid()
{
$valid = parent::isValid();
// Check that any fields set to unique are unique
if($this->uniqueFields)
{
// for each of the unique fields specified spin through the collection rows and grab the values of the elements specified as unique.
foreach($this->uniqueFields as $k => $elementName)
{
$validationValues = array();
foreach($this->collectionValues as $rowKey => $rowValue)
{
// Check if the row has a deleted element and if it is set to 1. If it is don't validate this row.
if(array_key_exists('deleted', $rowValue) && $rowValue['deleted'] == 1) continue;
$validationValues[] = $rowValue[$elementName];
}
// Get only the unique values and then check if the count of unique values differs from the total count
$uniqueValues = array_unique($validationValues);
if(count($uniqueValues) < count($validationValues))
{
// The counts didn't match so now grab the row keys where the duplicate values were and set the element message to the element on that row
$duplicates = array_keys(array_diff_key($validationValues, $uniqueValues));
$valid = false;
$message = ($this->getMessage()) ? $this->getMessage() : $this::UNIQUE_MESSAGE;
foreach($duplicates as $duplicate)
{
$this->invalidInputs[$duplicate][$elementName] = array('unique' => $message);
}
}
}
return $valid;
}
}
首先,我尝试(只是用于测试)在集合的第一个条目添加一个错误消息streetName。
$this->invalidInputs[0]['address']['streetName'] = array('unique' => $message);
但它好好尝试的工作。
添加它来STORENAME它的工作原理
$this->invalidInputs[0]['storeName'] = array('unique' => $message);
我想原因是字段集有一个自己的输入过滤器()?
当我做的var_dump($这- > collectionValues())1接收到的所有的值(也addressFieldset的)的多维阵列。 没关系! 但我不能添加的错误消息,在字段集的元素。
我怎样才能做到这一点? 我不希望插入在StoreEntityFieldset的AddressFieldset的所有元素。 (我用的是AddressFieldset还以其他形式)