2008年12月29日星期一

Re: [fw-mvc] Adding * to label

-- John Antony Riga <ja.riga@yahoo.com> wrote
(on Monday, 29 December 2008, 05:28 AM -0800):
> Is there a way we could integrate this to the framework ? I'm pretty sure
> everyone would benefit from a nice little patch that would add * to all
> required fields.

There are other ways to denote required fields, and the approaches
differ from developer to developer and site to site. Right now, the
label is marked with a class of "required" when the element is required,
allowing CSS styling. You can change that behavior, as well as prefix or
suffix either optional or required elements; it's all configurable.

You can configure the label decorators at any time by pulling them from
the individual elements, or simply configure them at element
instantiation (which, unfortunately, requires that you configure all
decorators).

Also, as noted, if you have a default behavior you want for *your*
forms, you can extend the default label decorator to specify your
desired behavior:

class My_Decorator_Label extends Zend_Form_Decorator_Label
{
public function __construct($options = null)
{
$this->setOption('escape', true)
->setOption('requiredSuffix', ' <span>*</span>');
parent::__construct($options);
}
}

Then, simply ensure your form specifies an appropriate element prefix
path:

$form->setElementPrefixPath('My_Decorator', 'My/Decorator', 'decorator');

(This must be done prior to creating any elements.)

> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
> From: Luiz Vitor <prowler666@gmail.com>
> To: fw-mvc@lists.zend.com
> Sent: Monday, December 29, 2008 1:25:40 PM
> Subject: Re: [fw-mvc] Adding * to label
>
> Hi there
>
> I managed to get this done subclassing Zend_Form and overriding the render()
> method:
>
> public function render(Zend_View_Interface $view = null)
> {
> foreach ($this->getElements() as $element) {
> $label = $element->getDecorator('Label');
> if ($label instanceof Zend_Form_Decorator_Label) {
> $label->setOption('requiredSuffix', ' <span>*</span>')
> ->setOption('escape', false);
> }
> }
>
> return parent::render($view);
> }
>
> On Mon, Dec 29, 2008 at 9:11 AM, Zladivliba Voskuy <nospampam@hotmail.fr>
> wrote:
>
> Hey, did you finally managed to find an answer to this problem ? It's an
> interesting question and I haven't found any answer to it..
>
> --
> My zend framework experience... the good, the bad
> http://myzendframeworkexperience.blogspot.com/
>
>
>
> > Hi,
> >
> > Currently I am using
> >
> > $element->getDecorator('label')->setOption('requiredSuffix', ' * ');
> >
> > to add * to required fields.
> >
> > I want to manage the markup from a centrally accessible place. Perhaps,
> > a custom label decorator. If I wish to change the markup to
> > <em>*</em> or to
> > <div class="redAsterisk">*</div>
> > it would be convenient to change it in one place.
> >
> > I just want to add this functionality to the label decorator and leave
> > all else as is.
> >
> > What is the best method to accomplish this? I have seen some people
> > adding * using CSS. I prefer to print the text character * and style it
> > using CSS.
> >
> > It would be much more convenient if the custom decorator can check
> > whether the element isRequired() and then add * to the label. But I
> > can't figure out how to do it.
> >
> > Code examples would be highly appreciated.
> >
> > --
> >
> > With warm regards,
> > Sudheer. S
> > Business: http://binaryvibes.co.in, Tech stuff: http://techchorus.net,
> Personal: http://sudheer.net
> >
> >
>
> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
> Organisez une f te en un tour de main : invitation, photos, blog
>
>
>

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: