Way back when CakePHP had a wiki there were a series of articles on “Advanced Validation.” One aspect of these articles was using the Model rules in JavaScript to validate on the client side. The code (I believe this is it) was originally for CakePHP version .10 (iirc). I had been using a heavily modified version of this code even in my 1.2 projects, but it was due for a ground up re-write.

I wrote an article for PHPArch not too long ago with a generic PHP approach for JavaScript validation. Using that JavaScript as a base, I created a helper to convert the model rules into JavaScript.

Here are the links:
Download
Instructions
Demo

This is all very beta, so please let me know if you find an issue. The helper should work with the three formats for defining validation in your model (output from bake, one rule per field, multiple rules per field).

If you are worried about security, there is an option to only include certain rules. See the tips section at the end of the instructions. Thanks to Marc for suggesting this.

Popularity: 96% [?]

Bookmark This Post

del.icio.us Digg FURL Yahoo! My Web 2.0 Reddit

I just posted a bug fix for my CakePHP Asset Packer. The previous version didn’t handle $javascript->codeBlock, when the inline option was set to false. The JavaScript would just disappear…ooops. Updated code is available in the Bakery article or you can download here.

Popularity: 80% [?]

Bookmark This Post

del.icio.us Digg FURL Yahoo! My Web 2.0 Reddit

I’ve been using the built in pagination in CakePHP 1.2-beta a lot lately and I must say it’s freaking awesome. My one oh so minor grievance is that it doesn’t “remember” where you were if you navigate off the page. For example many times I’ll be paging through a sorted list of records and want to edit a particular one. After I edit and save I’m dropped back to the index with the default sorting on page 1.

I Googled around a bit and skimmed through the pagination code, but didn’t see a quick way to turn this type of functionality on. So I wrote a quick component that keeps the sort/page in the session and re-applies it. This is all done automatically by just including the component in your controller.

You can download the component here.

I’ll probably submit it to the CakePHP Bakery soon, but was hoping to get some feedback first.

Popularity: 90% [?]

Bookmark This Post

del.icio.us Digg FURL Yahoo! My Web 2.0 Reddit

The Setup

At my job one of the products we offer is a radio service. There are a bunch of channels and each channel has a bunch of playlists. I was tasked with writing something to automatically switch the active playlist for each channel. My initial thought was to make a flag in the database marking the active playlist and then write a script that would be run from cron.

The Solution

After a bit of messing around I came up with something better. It didn’t need the database or cron and it was one line:

$active = round(time() / PLAYLIST_ROTATE_PERIOD) % count($playlists);

Why It Works

Dividing the current unixtime by the amount of time you want each playlist to be active for leads to a grouping of time chunks. Say you want each playlist to run for 3 hours (10800 seconds): round(1179685800 / 10800) = 109230.
Now 15 minutes later: round(1179686700 / 10800) = 109230. Same result.
Now 3 hours later: round(1179696600 / 10800) = 102931. The result increases one, which when modded by the number of playlists gives you the id of the next one in the list.

Popularity: 8% [?]

Bookmark This Post

del.icio.us Digg FURL Yahoo! My Web 2.0 Reddit