How does one add a 'plain text node' to a zend form?

后端 未结 7 551
夕颜
夕颜 2021-02-04 02:41

I\'m trying to add a plain text node in a zend form - the purpose is to only dispay some static text.

The problem is - im not aware of any such way to do it.

I

相关标签:
7条回答
  • 2021-02-04 03:09

    There might be a better way, but I created a paragraph by using a custom form element and view helper. Seems like alot of code for something so simple. Please let me know if you've found a more simplistic way to do it.

    //From your form, add the MyParagraph element
    $this->addElement(new Zend_Form_Element_MyParagraph('myParagraph'));
    
    class Zend_Form_Element_MyParagraph extends Zend_Form_Element
    {
        public $helper = 'myParagraph';
        public function init()
        {
            $view = $this->getView();
        }
    }
    
    class Zend_View_Helper_MyParagraph extends Zend_View_Helper_FormElement {
    
        public function init() {
        }
    
        public function myParagraph() {
            $html = '<p>hello world</p>';
            return $html;
        }
    
    }
    
    0 讨论(0)
  • 2021-02-04 03:10

    This one-liner works for me:

    $objectForm->addElement(new Zend_Form_Element_Note('note', array('value' => 'Hello World')));
    
    0 讨论(0)
  • 2021-02-04 03:11

    This functionality is built into Zend via Zend_Form_Element_Note.

    $note = new Zend_Form_Element_Note('forgot_password');
    $note->setValue('<a href="' . $this->getView()->serverUrl($this->getView()->url(array('action' => 'forgot-password'))) . '">Forgot Password?</a>');
    
    0 讨论(0)
  • 2021-02-04 03:14

    Zend has a form note view helper (Zend_View_Helper_FormNote), which you can use to add text.

    Just create a new form element (/application/forms/Element/Note.php):

    class Application_Form_Element_Note extends Zend_Form_Element_Xhtml  
    {  
        public $helper = 'formNote';  
    }
    

    In your form:

    $note = new Application_Form_Element_Note(
        'test',
        array('value' => 'This is a <b>test</b>')
    );
    $this->addElement($note);
    
    0 讨论(0)
  • 2021-02-04 03:23

    I faced the same problem and decided is better not to use Zend_Form at all, but to use directly view helpers (like Ruby on Rails does) and validate on the model.

    0 讨论(0)
  • 2021-02-04 03:24

    Adding a hidden element with non-escaped description does the thing.

    $form->addElement('hidden', 'plaintext', array(
        'description' => 'Hello world! <a href="#">Check it out</a>',
        'ignore' => true,
        'decorators' => array(
            array('Description', array('escape'=>false, 'tag'=>'')),
        ),
    ));
    

    Works perfectly. It is still attached to an element, which is, however, not rendered this way.

    Code taken from: http://paveldubinin.com/2011/04/7-quick-tips-on-zend-form/

    0 讨论(0)
提交回复
热议问题