I have defined a fieldset for phone numbers. This contains fields "type" (private, Office mobile ...) and "number". The Input filter for number is "required => true":
``
class PhoneFieldset extends BaseFieldset implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('phones');
$this->setHydrator(new DoctrineHydrator($this->getEntityManager(), 'HtsBase\Entity\Phone'))
->setObject(new Phone());
$this->add(array(
'type' => 'DoctrineORMModule\Form\Element\EntitySelect',
'name' => 'type',
'options' => array(
'label' => 'Type',
'empty_option' => '',
'object_manager' => $this->getEntityManager(),
'target_class' => 'HtsBase\Entity\OptionlistPhoneType',
'property' => 'name',
),
'attributes' => array(
#'id' => 'type',
'class' => 'input-medium',
),
));
$this->add(array(
'name' => 'number',
'options' => array(
'label' => 'Number',
),
'attributes' => array(
'type' => 'text',
#'id' => 'number',
'class' => 'input-medium',
'maxlength' => '25',
'autocomplete' => 'off',
),
));
}
public function getInputFilterSpecification()
{
return array(
'type' => array(
'required' => false,
),
'number' => array(
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'max' => 25,
),
),
),
),
);
}
``
Can i attach a validator/filter to the entire fieldset? So that if "type" AND "number" are empty the fieldset is valid, but validate if least one is filled out?
I found an easy to use solution, although I don't use the form any more I now heavily use the
InputFilter
and still needed the same stuff. But found an easy solutionThe
AbstractFilterValidator
, my own implementationOld answer
Although you were using the
InputFilterProviderInterface
, I usedZend\InputFilter\InputFilter
and wanted the same as you. If the fieldset was not filled in, validatetrue
.To do this I replace
isValid
with the following;It simply filters the array from all empty array keys, see docs for info about that. Then a check if the
$values
is empty, and if so returntrue
. Else the validators are called.Well I needed something again but needed a decent solution. Still no luck finding a good one, so I wrote the following code.
github gist
You can now simply call
setRequired(false)
on aInputFilter