|
Choosing A Webhost:
A web hosting service is a type of Internet hosting service that allows individuals and organizations to provide their own website accessible via the World Wide Web. Web hosts are companies that provide space on a server they own for use by their clients as well as providing Internet connectivity, typically in a data center. Web hosts can also provide data center space and connectivity to the Internet for servers they do not own to be located in their data center, called colocation. more...
|
Re: Zend_Filter: Simple use cases?: msg#00026
|
Subject: |
Re: Zend_Filter: Simple use cases? |
I think dealing with parameters that are arrays may need to be
capability of the individual filters. They could all be array capable
(or selectable via a constructor param) or there could be a second set
for arrays. I know this has been discussed.
Alternatively we could implement an array iterator and then you could do
something like this:
$filter->addFilter('ages', new Zend_Filter_Iterator($intFilter));
That would assume that $request->ages is an array and it would run the
Int filter on the entire array.
Regarding:
> $filter->addFilter('*', $intFilter);
It would probably be better to add a method like this, that would run on
all values:
$filter->addGlobalFilter($intFilter);
Bryce Lohr wrote:
I've really liked this idea, ever since I saw it in the original
Zend_FilterChain proposal. Unfortunately for me, my forms heavily make
use of nested arrays in the POST data. This was brought up in the
comments on that proposal, but I don't know if any solution was ever
suggested. It's a really elegant concept; if the nested array thing were
sorted out, I'd start using it immediately.
Thinking about it, I suppose I could just do this (reusing your example):
// Assume 'id', 'name', etc are under $_POST['level1']
$filter = new Zend_Filter_Container();
$intFilter = new Zend_Filter_Int();
$filter->addFilter('id', $intFilter);
$filter->addFilter('name', new Zend_Filter_Alpha());
$filter->addFilter('email', new Zend_Filter_Email());
$filter->addFilter('age', $intFilter);
$filter->run($this->_request->level1);
$id = $this->_request->level1->id;
The first argument to addFilter() above seems to be the field name to
apply the filter to. I assume you could specify the same field name
repeatedly to add more filters to the same field. What if I had a whole
array of check boxes that I wanted to run the same filter or validator
over? It would be really useful if I had way of specifying some kind of
wildcard or pattern to match against. That way I could hit the entire
checkbox array with something like:
$filter->addFilter('*', $intFilter);
$filter->run($this->_request->checkboxes);
Actually, I imagine implementing this would open up a whole can of
worms. But it would be really handy...
Regards,
Bryce Lohr
Christopher Thompson wrote:
This is one of the not-so-good things about the new Filter and
Validate classes -- they work on values not a container (e.g. request)
so you need one chain per value. And as you note since there are
usually only one or two filters/rules for each parameter it is more
trouble than it is worth.
I should also note that you don't need the filter chain at all in your
example, you can just do:
$intFilter new Zend_Filter_Int();
$id = $intFilter ->filter($this->_request->getParam('id'));
(which can be shortened further fluently) But you are still in the
procedural style of Filter_Input so what's the point...
Since the thing is mainly used for the request it should work
something like this:
$filter = new Zend_Filter_Container();
$intFilter = new Zend_Filter_Int();
$filter->addFilter('id', $intFilter);
$filter->addFilter('name', new Zend_Filter_Alpha());
$filter->addFilter('email', new Zend_Filter_Email());
$filter->addFilter('age', $intFilter);
$filter->run($this->_request);
$id = $this->_request->id;
This style is much more Form Controller friendly as well.
|
| |