Rectangle 27 0

$form->getMessages()

Will give you the validation messages.

You can dump the contents or loop the messages in a foreach loop. For example:

foreach($form->getMessages() as $msgId => $msg) {
    echo "Validation error: $msgId => $msg"
}

php - Zend Framework 2 - Wrapped form elements cause validation to fai...

php forms annotations zend-framework2
Rectangle 27 0

can you try to add line to your code, as I can see in zend's Form.php, element names aren't wrapped with 'entity' till the moment you call prepare();

$myForm->setName('entity');
$myForm->setWrapElements(true);
$myForm->prepare(); // << add this

But I don't believe that it will help, becuase behaviour you describe looks little different. Cau you show us more source code of Entity and var_dumps of things that Aydin and Sina wanted.

it was an error within my form. the getMessages solved the problem for me!

php - Zend Framework 2 - Wrapped form elements cause validation to fai...

php forms annotations zend-framework2
Rectangle 27 0

There must be a validator defined for this particular element that you are trying to remove.

In your controller where you are adding new elements to form, there must be addValidator calling like:

That is actually causing validation to be failed. So you have removed the element from form but you still have validation defined on that element to be checked.

If you are not able to find this validation adding function in controller, try to see if it has been defined through config file.

I'm adding the validators via Annotations... any ideas how to remove them in ZF2?

php - Zend Framework 2 - Removed form element causes validation to fai...

php validation zend-framework2 zend-form zend-form-element
Rectangle 27 0

you can't perform validation on non visible elements because they're not even submitted with the form.

Maybe you use hidden inputs instead and perform some filling when the form submit event occurs

php - validation of hidden elements zend form - Stack Overflow

php jquery zend-framework zend-form
Rectangle 27 0

In ZF2 data is not binded if the form is not valid. The reason because you see an empty string in the return of isValid is that the return type is a boolean, use var_dump instead.

php - Zend Framework 2 - Wrapped form elements cause validation to fai...

php forms annotations zend-framework2
Rectangle 27 0

The first thing I thought about was to remove the validator from your myElement's ValidatorChain. You could get it within the controller with:

$form->getInputFilter()->get( 'myElement' )->getValidatorChain()

It seems like you can't remove from the ValidatorChain, just add. Check this post. Matthew Weier O'Phinney, from Zend, explains why it can't be done and a possible solution for your scenario.

The way I solve this problem, is checking the 'remove condition' when I create the validator in the FormFilter class. If you use annotations I think it doesn't works for you, so Matthew suggestions is the one you should use.

Or you could try the one in this post from @Stoyan Dimov: define two forms, a kind of BasicForm and ExtendedForm. The first one have all the common form elements, the second one is an extended one of the other with the rest of fields. Depending on your condition you could use one or another.

thanks for this very summed-up answer! unfortunately I'm using AnnotationForms and I'd end up with nearly complete redundancy when creating a second form...

php - Zend Framework 2 - Removed form element causes validation to fai...

php validation zend-framework2 zend-form zend-form-element
Rectangle 27 0

you can't perform validation on non visible elements because they're not even submitted with the form.

Maybe you use hidden inputs instead and perform some filling when the form submit event occurs

php - validation of hidden elements zend form - Stack Overflow

php jquery zend-framework zend-form
Rectangle 27 0

Javascript can't (not in a sensible way) switch Zend_Form configuration. What you can do is changing the 'required' param for certain form fields on validation. For example; If you want to allow fieldTwo to be empty if fieldOne has 'desiredValue' as value you can achieve this using the following function in your form:

public function isValid($data) {
    if('desiredValue' == $data['fieldOne'])) {
        $this->getElement('fieldTwo')->setRequired(false);
    }

    return parent::isValid($data);
}

ok, the problem is that I get some post data from page A and then based on this post data, I construct page B. On page B, I display a form, this form is based on post data got from page A. Now page C validates the form created on page B and if it is invalidated, user should get back to page B with highlighted invalid fields.

To keep it simple use action B as renderer of form and validation of it. You can post values of the POST of page A to the form of page B and use them in the form to add/remove fields, change validators etc.

Rather than remove the validator in question, you could conditionally add it in $form->isValid() before calling parent::isValid(). See this post from Jeremy Kendall for one approach to dynamically changing the form behavior (adding fields, in his case) based on actions that take place on the client-side. He uses a preValidate() method, but you could probably just override isValid() as described above, which keeps the controller-flow pretty standard.

php - remove validation zend form element javascript/jquery - Stack Ov...

php javascript jquery zend-framework
Rectangle 27 0

You could do that by creating new ValidatorChain, and then loop through the validators attached to your element and dettach the Zend\Validator\NotEmpty validator. Just like this :

$newValidatorChain = new \Zend\Validator\ValidatorChain;

foreach ($form->getInputFilter()->get('test')->getValidatorChain()->getValidators() 
          as $validator) 
{
//Attach all validators except the \Zend\Validator\NotEmpty one

if (!($validator['instance'] instanceof \Zend\Validator\NotEmpty)) {
    $newValidatorChain->addValidator($validator['instance'],
                                     $validator['breakChainOnFailure']);
}
}
$form->getInputFilter()->get('test')->setValidatorChain($newValidatorChain);

thans for reply. But such big thing for that small thing? Why ZF2 is complex like this?

php - ZF2 remove isEmpty validation for form element - Stack Overflow

php validation zend-framework zend-framework2 zend-form
Rectangle 27 0

I managed to solve this problem. Hope it helps someone in the future.

The problem was with the sequence in which I was adding the validators to the file elemenet.

Changing the sequence to below solved the problem.

$file = $this->createElement('file', 'templateFile');
        $file->setAttrib('id', 'templateFile')
            ->setAttrib('class', 'form-control')
            ->setAttrib('placeholder', 'File containing template headers')
            ->setLabel('Template File <span class="glyphicons glyphicons-sheriffs_star requiredFlag"></span>')
            ->setDestination($destination)
            ->setRequired(true)
            ->addValidator('Count', false, 1)
            ->addValidator('NotEmpty', false)
            ->addValidator('Size', false, '15MB')
            ->addValidator("Extension", false, array('txt' ,'csv'))
            ->addFilter('StringTrim');

php - Zend Framework 1.12 - Zend Form File element triggering wrong va...

php zend-framework zend-form
Rectangle 27 0

It turns out the validation was breaking for static form element. To solve this, I have overridden the isValid() method in my Plano_Form_Element_Note class:

public function isValid($value)
{
    return true;
}

php - Custom Zend_Form element disappearing after validation - Stack O...

php forms zend-framework
Rectangle 27 0

Ok, finally I found a solution! You can define a ValidationGroup which allows you to set the attributes you'd like to validate. The others are not validated:

$form->setValidationGroup('name', 'email', 'subject', 'message');
$form->setData($data);
if ($form->isValid()) {
    ...

Great! That's THE solution. I'll use it from now on!

Congrats ron. I think this is the most acceptable way to go since there is no detach() method on ValidatorChain.

php - Zend Framework 2 - Removed form element causes validation to fai...

php validation zend-framework2 zend-form zend-form-element
Rectangle 27 0

class ValidatorChain implements Countable, ValidatorInterface
public function remove($name){
    foreach ($this->validators as $key => $element) {
        $validator = $element['instance'];
        if($validator instanceof $name){
            unset($this->validators[$key]);
            break;
        }
    }   
}
$form->getInputFilter()->get("xxxxx")->getValidatorChain()->remove('xxxxxx');

ValidatorChain is one of the core classes of ZF2. What about how to add a new method to validationChain instance?

php - Zend Framework 2 - Removed form element causes validation to fai...

php validation zend-framework2 zend-form zend-form-element