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

7

19

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 have used 'description' but that HAS to be attached to a form element.

Is there any way to simply display some text as part of a form? Zend considers everything as a form element so I cannot just print it out.

Eg:

The following will test your ability on so and so. . . .

etc...

Any thoughts?

Gavette answered 4/3, 2010 at 17:4 Comment(1)
I think best answer is Aine'sSaberio
H
6

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;
    }

}
Hent answered 18/3, 2010 at 17:1 Comment(0)
D
37

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);
Deferent answered 12/10, 2010 at 9:35 Comment(3)
Add this function in the class: public function isValid($value){ return true; } In that way the element will not disappear in the validation process.Rubellite
I think this should be the correct answer according to the reputations and the simplicity of this solution :)Constitutional
I tried this and a bunch of related approaches and couldn't get them to work. https://mcmap.net/q/628260/-how-does-one-add-a-39-plain-text-node-39-to-a-zend-form worked for me, however.Pancreatin
W
10

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/

Wiltz answered 12/7, 2012 at 9:21 Comment(2)
This is why I love Zend. No, not really.Pension
This is for me the best answer as the formNote (helper) element disappears when the form is POST-ed.Ly
H
6

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;
    }

}
Hent answered 18/3, 2010 at 17:1 Comment(0)
H
6

A little late but thought I'd throw it in anyway for the benefit of the community.

Aine has hit the nail on the head. FormNote is what you need if you want to use text in Zend_Form. However, you can use it without needing to extend Zend_Form_Element_Xhtml. See example below:

$text = new Zend_Form_Element_Text('myformnote');
$text->setValue("Text goes here")
     ->helper = 'formNote';

Note that you can use both text and html with the formNote helper.

Hogen answered 9/4, 2013 at 17:43 Comment(2)
Thanks. This was the only approach that worked for me.Pancreatin
This works but the element will disappear when you validate the form, which may or may not be what you want. See axiom82 answer above for the solution.Aventine
S
5

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>');
Stellarator answered 15/11, 2013 at 2:2 Comment(2)
This is the correct answer. No need to uselessly extend it and override $helper with the same value as is done in the most upvoted answer.Verruca
Yes, much more elegant and simple than the other answers. Works perfectly and avoids the disappearance on validation.Aventine
B
3

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.

Beehive answered 4/3, 2010 at 17:7 Comment(0)
Q
0

This one-liner works for me:

$objectForm->addElement(new Zend_Form_Element_Note('note', array('value' => 'Hello World')));
Quixote answered 12/12, 2018 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.