Tag Archives: zend framework

How to Setup Different Error Messages for Each Zend Form Element Validator

Anyone who has worked with that has come across this problem. I’d like to show different error message on each validator attached to a Zend_Form_Element. Let’s say we validate an text input field. We want it to contain only digits, but also we’d like to display different messages when the field is empty and when the user has entered something that is different from digits.

It can be done by attaching to the form element two validators: Zend_Validate_Digits and Zend_Validate_NotEmpty, but first let’s see how to change the default “Value is required and can’t be empty” error message of a form field.

$element = $form->createElement('text', 'phone');
$element->setLabel('Please, enter your phone number:')
	->setRequired(true)
	->addValidator('Digits');
$form->addElement($element);

Here we validate the field with Zend_Validate_Digits and we have set it to be required. Thus everything containing characters, i.e. “my123name” or “007bond”, will be false, while “1234” will be true.

Zend Framework
To show different error messages you've to attach them per validator and not per form element!

Continue reading How to Setup Different Error Messages for Each Zend Form Element Validator

POST with Zend_Http_Client

CURL and Zend_Http

It’s a well know fact that you can preform HTTP requests with CURL. Zend Framework does the same job with Zend_Http. Especially Zend_Http_Client can be used to “replace” the usual client – the browser, and to perform some basic requests.

HTTP requests can be performed with Zend_Http_Client
Zend_Http_Client is mostly used to perform GET requests, but it can be also very helpful for POST HTTP requests.

I’ve seen mostly GET requests, although Zend_Http_Client can perform various requests such as POST as well.

// new HTTP request to some HTTP address
$httpClient = new Zend_Http_Client('http://www.example.com/');
// GET the response
$response = $httpClient->request(Zend_Http_Client::GET);

Here’s a little snippet showing how to POST some data to a server.

// new HTTP request to some HTTP address
$client = new Zend_Http_Client('http://www.example.com/');
// set some parameters
$client->setParameterPost('name', 'value');
// POST request
$response = $client->request(Zend_Http_Client::POST);

Note that the request method returns a response. Thus if you are simulating a form submit action you can “redirect” to the desired page just like the form.

// new HTTP request to some HTTP address
$client = new Zend_Http_Client('http://www.example.com/');
// set some parameters
$client->setParameterPost('name', 'value');
// POST request
$response = $client->request(Zend_Http_Client::POST);
echo $response->location;

Use fopen() to Check File Availability?

Zend Framework and Zend_Http_Client

PHP's fopen() can be used to check remote file existence
PHP's fopen() can be used to check remote file existence

I’ve posted about Zend_Http_Client. Simply there you can ‘make’ your own http client and you can request a remote file. Just to check what’s going on with this file.

// new HTTP request to a file
$httpClient = new Zend_Http_Client('http://www.example.com/myfile.mp4');
 
// get the HEAD of the response and match agains the
// Content-Length. That's because using the Content-Type is slower
$response = $httpClient->request(Zend_Http_Client::HEAD);
 
// if the Content-Length is 0 the file doesn't exists
if (0 === (int)$response->getHeader('Content-Length')) {
	echo 'the file doesn\'t exits';
}

However is there any other way to answer the same question?

fopen()

Yes and no? Perhaps yes, but you should be careful. I’m still not sure it can be used in any case. However here’s the snippet.

if (FALSE === @fopen('http://www.example.com/myfile.mp4', 'r')) {
	echo 'the file doesn\'t exists';
}

fopen() will return FALSE whenever the file doesn’t exists.

In both cases I request a remote file – an MPEG-4 file. Note that fopen()’s first parameter can be a HTTP resource.

Download Images with PHP

As it seems one possible solution while trying to download images with PHP is to write a “client” to do so. Will it be with cURL, Zend Framework or some other tool – it doesn’t matter.

However one of the most used approaches is simply with file_get_contents and file_put_contents. I’m not sure whether I wrote already about this or not, but this solution simply looks something like this.

 
file_put_contents('/path/to/file', 
                  file_get_contents('http://www.example.com/source.image');

In fact a client will give you more control over the process, to handle errors, etc. So maybe this is a better solution.

A Memcached Zend_Cache

Zend_Cache

Usually Zend_Cache is used to store cache files on the file system, which can be really fast and useful in most of the cases. However there’s a faster cache mechanism and hopefully it’s supported by Zend_Cache as well. This is the Memcached backend.

A Faster Cache

Memcached is a really powerful tool to cache directly into the RAM. First, this tool has nothing to do primary with Zend Framework. It’s a server, usually started on some port, that can be called to store and get things from the memory. This of course is very fast, way faster than the cache in the hard drives.

Zend_Cache and Memcached

Zend_Cache has an interface to work with Memcached which is great as usual. The PHP example of Memcache (note that there are two things Memcache and Memcached, which are slight different) can be found here and as it says:

$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
 
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";
 
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
 
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
 
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
 
var_dump($get_result);

However this can be coded into a Zend Framework style like that:

$frontend = array('caching' => true, 'lifetime' => 1800, 'automatic_serialization' => true);
 
$backend = array(
    'servers' =>array(
        array('host' => '127.0.0.1', 'port' => 11211)
    ),
    'compression' => false
);
 
$cache = Zend_Cache::factory('Core', 'Memcached', $frontend, $backend);

Note that you don’t have the typical “cache_dir”, just because everything’s cached into the memory.

Now you can call the cache as it’s called with the “File” backend interface:

$key = 'mykey';
 
if (($result = $cache->load($key)) === false) {
	// call the slow database query here ...
	// save in $result
 
	$cache->save($result, $key);	
}
 
echo $result