Tag Archives: measurement

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

Speed Up the jQuery Code: Selectors’ Cache

Short Experiment

Here’s a short optimization of a small chunk of jQuery code. The experiment was to increment the number of DOM element access. In this case this was a change of the innerHTML property – using the $.html() method.

I’ve measured the result with the console.time() method and thus I expect correct results for both cases. In the first case I directly call the jQuery selectors’ html() method:

for (var i = 0; i < 10000; i++) {
    $('#container').html(i);
}

While in the second case I “cache” it before the loop:

var t = $('#container');
for (var i = 0; i < 10000; i++) {
    t.html(i);
}

and here’s the full code in the first case:

<html>
<head>
    <title>jQuery Cache</title>
</head>
<body>
 
<div id="container"></div>
 
<script src="/scripts/library.js"></script>
<script>
 
$(document).ready(function() {
 
    console.time('foo');
    for (var i = 0; i < 10000; i++) {
        $('#container').html(i);
    }
    console.timeEnd('foo');
});
</script>
</body>
</html>

The Results

As expected the second “cached” approach gave better results. With the increment of the iterations the second method began to be slightly better than the first one. That’s not so much, but imagine you have more than one selector in the loop’s body?

Numbers:


Note that in the time is in ms, and, yes, this is not so much, but when you deal with large data and you’ve more than one selector in the loop’s body, this can be critical!

Graphics: