2009年2月24日星期二

Re: [fw-mvc] Zend Forms and Filters

-- PHPScriptor <contact@phpscriptor.com> wrote
(on Tuesday, 24 February 2009, 07:57 AM -0800):
> I noticed the $form->getValues(), but placed it before the $form->isValid().
> Didn't work there, but after isValid seems to be working fine!

Actually, pulling the values always filters them... assuming the values
have been populated. isValid() populates the values in the form as a
side effect -- but you can also populate the form using setDefaults()
and populate(). Once you have, calling getValues() will also give you
the filtered values (but there are no assumptions about whether or not
those values are valid).


> PHPScriptor wrote:
> >
> > Hey,
> >
> > I've a little question how I could deal with Forms and Filters.
> >
> > Lets say, I put a filter on an element that will change my input value.
> > Only, I find it always strange that, from the moment I do getPost I get
> > the unfiltered values.
> >
> > What I do know, after the getPost I do:
> > $formData['day'] = Zend_Filter_MyFilter::filter($formData['day']);
> >
> > In my opinion, I have to do 2 times the filter just to get the correct
> > value. In this example I only have to do it once, but what if I have 7
> > Filters in a form, it becomes a lot of work and code.
> > Isn't there a way to set the filter on the formelements, en get the
> > filtered values in the controller?
> >
> > // form
> > $formElements[]= self::createElement('text','day')
> > ->addValidator('int')
> > ->addFilter(new Zend_Filter_MyFilter())
> > ->addValidator('Between',false, array(0,24));
> >
> > // controller
> > if ($this->_request->isPost()) {
> > $formData = $this->_request->getPost();
> > if ($form->isValid($formData)) {
> > ... // submit value to db
> > }
> > }
> >
>
>
> -----
> visit my website at http://www.phpscriptor.com/ http://www.phpscriptor.com/
> --
> View this message in context: http://www.nabble.com/Zend-Forms-and-Filters-tp22179829p22181851.html
> Sent from the Zend MVC mailing list archive at Nabble.com.
>

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

没有评论: