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 8970783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:47:29+00:00 2026-06-15T17:47:29+00:00

How can I in ZF2 create custom form element with custom validator? I want

  • 0

How can I in ZF2 create custom form element with custom validator? I want to create custom category picker that uses jQuery and content of this element should be rendered from phtml script. In ZF1 it was quite easy but in ZF2 I don’t know from where to start.

  • 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-06-15T17:47:30+00:00Added an answer on June 15, 2026 at 5:47 pm

    A form element must implement a Zend\Form\ElementInterface. A default class is the Zend\Form\Element which you can use as a base form:

    <?php
    namespace MyModule\Form\Element;
    
    use Zend\Form\Element;
    
    class Foo extends Element
    {
    }
    

    CUSTOM VALIDATOR

    You can let the element directly assign a custom validator. Then you must implement the Zend\InputFilter\InputProviderInterface:

    <?php
    namespace MyModule\Form\Element;
    
    use Zend\Form\Element;
    use Zend\InputFilter\InputProviderInterface;
    use MyModule\InputFilter\Bar as BarValidator;
    
    class Foo extends Element implements InputProviderInterface
    {
        protected $validator;
    
        public function getValidator()
        {
            if (null === $this->validator) {
                $this->validator = new BarValidator;
            }
            return $this->validator;
        }
    
        public function getInputSpecification()
        {
            return array(
                'name'       => $this->getName(),
                'required'   => true,
                'validators' => array(
                    $this->getValidator(),
                ),
            );
        }
    }
    

    CUSTOM RENDERING

    At this moment it is a bit complex how Zend Framework handles the rendering of custom form element types. Usually, it just returns plain <input type="text"> elements.

    There is one option, then you have to override the Zend\Form\View\Helper\FormElement helper. It is registered as formelement and you must override this view helper in your custom module:

    namespace MyModule;
    
    class Module
    {
        public function getViewHelperConfig()
        {
            return array(
                'invokables' => array(
                    'formelement' => 'MyModule\Form\View\Helper\FormElement',
                    'formfoo'     => 'MyModule\Form\View\Helper\FormFoo',
                ),
            );
        }
    }
    

    Furthermore, every form element in Zend Framework 2 is rendered by a view helper. So you create a view helper for your own element, which will render the element’s content.

    Then you have to create your own form element helper (MyModule\Form\View\Helper\FormElement):

    namespace MyModule\Form\View\Helper;
    
    use MyModule\Form\Element;
    use Zend\Form\View\Helper\FormElement as BaseFormElement;
    use Zend\Form\ElementInterface;
    
    class FormElement extends BaseFormElement
    {
        public function render(ElementInterface $element)
        {
            $renderer = $this->getView();
            if (!method_exists($renderer, 'plugin')) {
                // Bail early if renderer is not pluggable
                return '';
            }
    
            if ($element instanceof Element\Foo) {
                $helper = $renderer->plugin('form_foo');
                return $helper($element);
            }
    
            return parent::render($element);
        }
    }
    

    As a last step, create your view helper to render this specific form element:

    namespace MyModule\Form\View\Helper;
    
    use Zend\Form\ElementInterface;
    use Zend\Form\View\Helper\AbstractHelper;
    
    class Foo extends AbstractHelper
    {
        public function __invoke(ElementInterface $element)
        {
            // Render your element here
        }
    }
    

    If you want to render a .phtml file for example for this form element, load it inside this helper:

    namespace MyModule\Form\View\Helper;
    
    use Zend\Form\ElementInterface;
    use Zend\Form\View\Helper\AbstractHelper;
    
    class Foo extends AbstractHelper
    {
        protected $script = 'my-module/form-element/foo';
    
        public function render(ElementInterface $element)
        {
            return $this->getView()->render($this->script, array(
                'element' => $element
            ));
        }
    }
    

    It will render a my-module/form-element/foo.phtml and in this script you will have a variable $element which contains your specific form element.

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

Sidebar

Related Questions

I'm trying to create a SOAP server in my ZF2 application that I can
This is the 1st time to touch Zend Framework, I'm user zf2 now, want
can someone show me the regex for this preg_match. I want to make sure
Does anybody try zf2? I can not understand new mechanism of using sessions in
Can anyone enlighten me to a way I can Highlight the content of an
In my zf2 controller I want to retrieve the application base URL (for example
can someone explain what means this line, or provide with source where it is
Can someone please explain how this code is working for me? I don't understand
Can I access my SkyDrive using a permanent access token? Basically, I want to
in zf1, we can get controller and action name using $controller = $this->getRequest()->getControllerName(); $action

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.