Asked  7 Months ago    Answers:  5   Viewed   41 times

I use a certain form in several places. In one of them I need to ignore a form element which I set programmatically after the validation.

Because it's just an exception I don't want to create a new form. So I thought, I just remove this element in the controller like:

$myForm->remove('myElement');

The problem is that the form now won't validate. I don't get any errors but the $myForm->isValid() just returns an empty value.

Any ideas what I might be doing wrong?

Thanks!

 Answers

52

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()) {
    ...
Wednesday, March 31, 2021
 
tadman
answered 7 Months ago
25

You will have to overwrite the default messages using the messages key from the options.

Try this (i guess you'll have to trim this into one line though for the annotations to work ;) That's up to you, hehe.

@FormValidator({
    "name":"regex", 
    "options":{
        "pattern":"/(-?d{1,3}Dd+)[^d-]+(-?d{1,3}Dd+)/",
        "messages":{
            "regexInvalid":"Regex is invalid, Booo!",
            "regexNotMatch": "Input doesn't match, bleeeeh!",
            "regexErrorous": "Internal error, i'm like wtf!"
        }
    }
})

Each Validator has it's own messages. You're best advised to see the Source-Code to find out what ErrorMessages are present within each Element. To give you an example, please follow this link (click) to see how to find out about the messages-keys.

When using the array-style-syntax for creating forms outside of Annotations, it may be a little bit safer to go the statis approach for the keys like

'messages' => array(
    ZendValidatorRegex::INVALID => "Regex is invalid, Booo!",
    //etc...
)

Since strings - at least in theory - could always change, the constants won't.

Wednesday, March 31, 2021
 
Deyson
answered 7 Months ago
51

Using the following:

$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"
}
Wednesday, March 31, 2021
 
Sufi
answered 7 Months ago
29

You can find a similar example in the documentation Forms and Actions

In Zend Framework 2 this is done using an input filter, which can either be standalone or defined within any class that implements the InputFilterAwareInterface interface.

So, in this example model entity implements the InputFilterAwareInterface and there are methods setInputFilter and getInputFilter. So later you can call isValid().


I personally put all my filters into src/ModuleName/Filter/ (UserFilter.php). Where implements InputFilterAwareInterface and define getInputFilter() method with all dirty things.

Than in controller, or any other class, just call:

use ModuleNameFilterUserFilter;

...

$filter = new UserFilter();
$data   = $this->params()->fromPost();

if(!$filter->getInputFilter()->setData($data)->isValid()){
    $data   = $filter->getInputFilter()->getValues();
    $errors = $filter->getInputFilter()->getMessages();
    // Throw an exception
}
Saturday, May 29, 2021
 
hjalpmig
answered 5 Months ago
24

You can simply use the disable_html_escape label's option. It works for me.

$this->add(array(
        'type' => 'Button',
        'name' => 'submit',
        'options' => array(
            'label' => '<i class="icon icon-foo"></i> Submit',
            'label_options' => array(
                'disable_html_escape' => true,
            )
        ),
        'attributes' => array(
            'type'  => 'submit',
            'class' => 'btn btn-success'
         )
    ));
Thursday, August 19, 2021
 
Omar
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :