Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3803720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:24:25+00:00 2026-05-19T14:24:25+00:00

I have an element. I want to add a custom validator and custom filter

  • 0

I have an element. I want to add a custom validator and custom filter to it. The validator makes sure the input is one of several permitted values, then the filter adds some custom values to the input. This means I have to validate the original input first before running the filter. I do it in this order

$element = new Zend_Form_Element_Text('element');
$element->addValidator('PermittedValue', false);
$element->addFilter('TotalHyphen', false);
$this->addElement($element);

but this order isn’t being respected. The filter runs first and changes the data, then the validator runs on the filtered data which means it always fails even for valid input. It seems from documentation that this is intentional

Note: Validation Operates On Filtered
Values Zend_Form_Element::isValid()
filters values through the provided
filter chain prior to validation. See
the Filters section for more
information.

How can I specify the order in which validators and filters run?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:24:26+00:00Added an answer on May 19, 2026 at 2:24 pm

    Sure seems like creating a custom element that supports post-validation filtering would be the way to go. How about this:

    /**
     * An element that supports post-validation filtering
     */
    class My_Form_Element_PostValidateFilterable extends Zend_Form_Element_Text
    {
        protected $_postValidateFilters = array();
    
        public function setPostValidateFilters(array $filters)
        {
            $this->_postValidateFilters = $filters;
            return $this;
        }
    
        public function getPostValidateFilters()
        {
            return $this->_postValidateFilters;
        }
    
        public function isValid($value, $context = null)
        {
            $isValid = parent::isValid($value, $context);
            if ($isValid){
                foreach ($this->getPostValidateFilters() as $filter){
                    $value = $filter->filter($value);
                }
                $this->setValue($value);
            }
            return $isValid;
        }
    }
    

    Usage would be something like this:

    $elt = $form->addElement('PostValidateFilterable', 'myElement', array(
         'label' => 'MyLabel',
         'filters' => array(
             'StringTrim',
             // etc
         ),
         'validators' => array(
             'NotEmpty',
             // etc 
         ),
         // here comes the good stuff
         'postValidateFilters' => array(
             new My_Filter_RunAfterValidateOne(),
             new My_Filter_RunAfterValidateTwo(),
         ),
    ));
    

    This keeps the validation and filtering in the form – keeping the controller thin.

    Not tested, just a stab in the dark. And surely you could fatten/modify the API to add/remove filters by key, etc.

    Whaddya think?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an input element, and i want bind both change and keypress event
I have a list of structs and I want to change one element. For
I have a <tr> element in my view, and I want to add classes
I have a form element that I want to address via javascript, but it
I have an XML datatype and want to convert an element into a sql
I have a custom authentication HttpModule that is pretty strait forward. But I want
I want to add a custom column to the Inbox which is a Yes/No
i'm building a Videoplayer custom control (Project called WpfCustomControlLibrary1) and want to add a
I have an XSL style sheet for which I need to add some custom
I have a VGroup in my application that contains several custom components of type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.