Archive for the ‘Code Case’ Category

FormEncode (Pylons): What Is State?

If you’re using Pylons, the Python framework, you’re probably using FormEncode. And if you’re using FormEncode, you’ve probably have noticed, and blithely ignored, the state argument that’s the last argument to a number of the validator class methods.

So what is state and why would you want to use it?

Comparing Big Numbers in PHP

Problem
What if I need to compare really big numbers in PHP? Like comparing 2^66 > 3^53?
Overview
This will work:

print (int) (pow(2,66) > pow(3,53));

PHP will convert the integers to scientific notation. But this script illustrates the limitation of normal operational syntax (i.e.: pow(2,66) > pow(3,53) vs. bccomp(bcpow(2,66), bcpow(3,53),1) > 0):

$max = 1000;
foreach ( range(1,$max) as [...]

Dynamic Validation Error Messages in CakePhp

Problem
In CakePhp, I want to invalidate a field submitted in a form and set the error message dynamically. If the error message is set by a validation parameter within the model, the new message should override that error message and be displayed by the form in the view.
Overview
Consider the following three cases as a developer:
1. [...]

Php Input Validation

Problem
I need to validate input from a textarea field. I want to allow a few tags, like a, i, b, etc. But everything else needs to be filtered out. And the input should be checked to see that its is nested properly.
Overview
User input sanitization and validation is one of those things that just [...]

CakePhp: Custom Pagination Methods

I wanted to use the CakePhp pagination helper to paginate some complex records. The model that the paginator was referencing had both belongsTo and hasMany associations. belongsTo associations are not a problem. hasMany, I discovered, are — which makes sense when I think about.
As the ticket notes:
You can only paginate LEFT JOIN [...]