Zend_Filter_Null
- Posted by thomasw at 11:16:41 // //
- Filter
Today I added a new component to Zend Framework which will be available as with release 1.10.
Zend_Filter_Null is a filter which converts special input to null.
Now you may ask, what should this filter do ? I could also unset() the variable.
Well… one reason is database awareness… a second one may be user input.
Let’s see an example, so you understand what I mean:
$filter = new Zend_Filter_Null();
$filter->filter('2'); // returns 2
$filter->filter('0'); // returns null
Now what’s the intention…
Let’s say you have a selectbox with different values.
When someone does not select anything you will get a ‘0′ returned because the selectbox always returns anything. In your database you don’t want to store integer ‘0′ but have a NULL instead to mark that there has not been selected anything.
Before, you had to code an if clause and then store the content different so you had no integer ‘0′ in your database.
Now you can simply attach this filter and get a NULL in return.
Another case could be a textbox… when the user does not enter a text you will get an empty string returned. Attaching Zend_Filter_Null to your textbox you will get NULL instead of an empty string.
Zend_Filter_Null works per default like php’s empty(). This means that when the input is a integer 0, an empty string, an string ‘0′, an empty array or a boolean false, then you will get a NULL from this filter.
Any other input will be returned as is, without changes.
But you can configure the convertion algorithm to return defined types as is.
$filter = new Zend_Filter_Null(Zend_Filter_Null::INTEGER);
$filter->filter('0'); // returns '0' because a string 0 was given
$filter->filter(0); // returns NULLi
This will only detect integer ‘0′ values and return NULL in the case they are given. All other cases (boolean false, empty string and so on), are returned as is, without filtering them.
For more details look into the manual.
Of course it is more convinient to attach the filter directly to the form element.
$element = new Zend_Form_Element_Text('MyText');
$element->addFilter('Null');
But these details you should already know ;-)
When you have a question dont be shy to ask.
More to come from me soon…
Have a good work with Zend Framework.
Greetings
Thomas Weidner
I18N Team Leader, Zend Framework
Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework
Recieving files with Zend_Form_Element_File
- Posted by thomasw at 01:44:02 // //
- Filter
How to receive a file from Zend_Form_Element_File is often a difficult question.
When you have problems you should read below to get an idea of the handling and to prevent possible problems:
Let’s expect our form has a file element like this one:
$element = new Zend_Form_Element_File('file');
$element->setDestination('/MyDir');
In past, before ZF 1.8, when you wanted to get the uploaded file you had to call receive() afterwards like this…
if ($form->isValid($this->getPost())) {
if (!$element->file->receive()) {
print_r($element->file->getMessages());
}
}
Now many people said, and I think they were right, that it would be a great idea to get the file when calling getValues(). So we added this new feature for ZF 1.8.
This means that that with ZF 1.8 this call is enough:
$values = $form->getValues();
Your file is already transferred… no need to call received afterwards.
But sometime it is wished to call received manually. You could, for example, wish to add a own filter based on a value which you get from your form. So calling getValues() now would mean that you can not add a filter afterwards… as the file is already transferred.
But also for this usecase a solution has been added. You can set a flag to the file element which prevents that the file itself will be received when you call getValues().
Take a look at the following file element:
$element = new Zend_Form_Element_File('file');
$element->setDestination('/MyDir')
->setValueDisabled(true);
Note the added call of setValueDiabled(). It tells the file element that it should not receive the file when you call getValues().
Now you can call getValues() and the file will not be received in the background. But this means, of course, that you have to call receive() manually. Otherwise your file will not be available after the script has been finished.
$values = $form->getValues();
if ($form->isValid($this->getPost())) {
$element->file->addFilter('Rename', false, 'Filename_'.$values['id']);
if (!$element->file->receive()) {
print_r($element->file->getMessages());
}
}
Remember, to have this working you need to call setValueDisabled() otherwise the file is transferred when calling getValues(). When you omit the call then your file will be transferred, due to getValues(), but it will not be renamed, because the rename filter was not available at the time, the file has been received.
Note, that using a filename based on a user input is always a security issue. Use a autoincremented number from your database for example, or be sure that you validate the user input before using it as new filename.
You should also note that I did not use another instance of Zend_File_Transfer…
The file element itself has already a instance available. When you really need to have the instance itself, then use getTransferAdapter() instead of creating a new instance. This is faster, uses less ressources and prevents possible problems with files already being transferred by the other instance.
Greetings
Thomas Weidner
I18N Team Leader, Zend Framework
Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework
File uploads and upload progress
- Posted by thomasw at 23:03:22 // //
- Filter
Sometimes it is necessary to show the progress for files which a user actually uploads to a site.
With the next release 1.8 the Zend Framework supports a very easy and simple way to provide the user with all wished data.
There are some prerequisits:
You must have installed and activated eighter the APC or UploadProgress extension.
You should use Zend_Form_File_Element, otherwise the needed hidden fields will not be attached.
So let’s expect you have already created the form and the controller to handle the upload itself.
To show your user the progress of his file upload you need to have an iframe or another different process beside the upload itself which shows it’s result to the user. The server part looks like this:
$adapter = new Zend_ProgressBar_Adapter_Console();
$upload = Zend_File_Transfer_Adapter_Http::getProgress($adapter);
while (!$upload['done']) {
$upload = Zend_File_Transfer_Adapter_Http:getProgress($upload);
}
There is only one thing you have to do:
* Create the wished ProgressBar adapter and give it to Zend_File_Transfer_Adapter_Http::getProgress()
All other things are done in background by this static method. You don’t need to create the form, or a file adapter. Simply call this static method.
In return your ProgressBar adapter will be updated and will show you the new progress for this upload.
However you realize your view, there are different methods to do so, you have to give the output of Zend_File_Transfer_Adapter_Http::getProgress() as input to the next call of this method.
It was never simpler to get the upload progress and show it to a user.
Greetings
Thomas Weidner
I18N Team Leader, Zend Framework
Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework
