Hello,

I know, regarding the subject do you think... please not more of these
questions but I have searched the web for hours and days now and I couldn't
find the answer on my specific question. Let me explain my current
situation:

1. I have a Form-Class called OrderForm that extends Zend\Form\Form:

class OrderForm extends Form
{
        public function __construct()
        {
                parent::__construct('order');

        //-->

                $this->add(array(
                        'name' => 'packages',
                        'type' => 'MultiCheckbox',
                        'options' => array(
                                'label_attributes' => array(
                                        'class' => 'checkbox inline',
                                ),
                                'value_options' => array(
                                        'germany' => 'Germany',
                                        'austria' => 'Austria',
                                        'switzerland' => 'Switzerland'
                                ),
                        ),
                ));

                //-->
        }
}

2. I have a InputFilter-Class called OrderFilter that extends
Zend\InputFilter\InputFilter:

class OrderFilter extends InputFilter
{
        public function __construct()
        {
                //-->

                $this->add(array(
                        'name'=> 'packages',
                        'required'       => true,
                        'validators' => array(
                                array(
                                        'name'    => 'NotEmpty',
                                        'options' => array(
                                                'type'    => 
\Zend\Validator\NotEmpty::ALL,
                                                'message' => 'isRequired'
                                        ),
                                        'break_chain_on_failure' => true,
                                ),
                        ),
                ));

                //-->
        }
}

3. I have a FormFactory-Class called OrderFormFactory:

class OrderFormFactory implements FactoryInterface
{
        public function createService(ServiceLocatorInterface $serviceLocator)
        {       
                $form = new OrderForm();
                
                $form->setAttribute('method', 'post');
                $form->removeAttribute('name');

                $form->setInputFilter(new OrderFilter());

                return $form;
        }
}

So far so good, this works perfect. And now to my question:

The element which I added in step 1 gets a default InArray-Validator and the
required true option. BUT now I want to customize the standard validator
message for the require option, to get this working I add the NotEmpty
Validator to my form field (step 2). In step 3 I connect my form with my
InputFilter-Class and there start my problems. If I check my InputFilter
with $form->getInputFilter() I get the following ValidatorChain for my
"packages" element.

1. InArray-Validator
2. NotEmpty-Validator

The problem is now that my NotEmpty validator is after the InArray validator
and if I validate my form with an empty option now, I get both error
messages. Furthermore it doesn't make sense to have the NotEmpty-Validator
at the end of the ValidatorChain.

I know that the NotEmpty Validator is at the end because custom validators
are added to the validator chain. I also know methods like
prependValidator() but I couldn't use this method in my InputFilter-Class.
After hours of searching I found  this
<https://github.com/zendframework/zf2/pull/3218#issuecomment-11535066>   and
it already merged to ZF2. Unfortunately I don't understand the difference
between setPreferFormInputFilter(true) and useInputFilterDefaults(false). In
my eyes it does exactly the same but I know there is a difference between
for sure.

But both of this methods don't do what I want. These methods clear the
default validators from my element but I don't want to clear the InArray
Validator, I only want the NotEmpy validator with a custom error message
before the InArray Validator.

It would very nice, if someone could help me.

Stefan




--
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/Custom-Validator-Messages-tp4660769.html
Sent from the Zend Framework mailing list archive at Nabble.com.

-- 
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to