x

Web Computing, php php

Mixing Object and Form Helpers in Symfony

August 21st, 2008
post #125 

Being new to Symfony I’m still getting my head around some of its paradigms. Two months with the manual before embarking on any development taught me that it was worth pursuing, but there’re always areas you need to see working.

The Object form helpers looked particularly useful, so for my first production development I aim to use them wherever possible. These helpers allow you edit objects directly, simplifying the update process back in your actions.

sample form in editformSuccess.html?php echo form_tag('editform/updatetitle'); ?>
<?php echo object_input_hidden_tag($newsletter, 'getID') ;?>
Title: <?php echo object_input_tag($newsletter, 'getTitle') ;?>
<?php echo submit_tag('update'); ?>

and in actions.class.html?php
public function executeEditform($request)
{
$this->newsletter = »
NewsletterPeer::retrieveByPk($request->getParameter(’id’));
$this->forward404Unless($this->newsletter);
}

public function executeUpdatetitle($request)
{
$nltoupdate = NewsletterPeer::retrieveByPk($request->getParameter(’id’));
$this->forward404Unless($nltoupdate );
$nltoupdate->fromArray(»
$this->getRequest()->getParameterHolder()->getAll(),BasePeer::TYPE_FIELDNAME);
$nltoupdate->save();
return $this->redirect(»
‘newsletters/view?id=’.$request->getParameter(’id’));
}

The update code uses the fromArray method to update any properties of your object it holds data for, received from your form. Adding fields to your form doesn’t require changes to the action.

I got into difficulties when it came to setting a boolean field for my object, called “IsPublished”. Wishing to stick with the Object form helpers I investigated the Object_select_tag, however this has a different use altogether - allowing you to grab properties of other objects to match with your current object. The classic example being choosing an author from the authors object to match up to a post object.

I still wanted to save a property for my object, but I didn’t want to have it select from the object, or grab values from other objects. Further searches revealed I wasn’t alone in wondering how to set default selected values and generally get to grips with object_select_tag.

Mixing Object and Form helpers seemed the best solution, using select_tag for my boolean field. However this meant I wasn’t using the Object helper exclusively, so would it still work with fromArray?

The form code generated looked promisingly simple, so I tried a mix of object_input_tag and select_tag in my editformSuccess.html?php
$defaultOption = »
$newsletter->getIsPublished() == 1?1:0; # get current, set default
echo select_tag(’in_published‘, options_for_select(array(»
0, 1),
$defaultOption));
?>

Note the highlighted property in select_tag. My Object Property is called “IsPublished”. To object-generate a plain text input field I’d have used:

<?php
Published: <?php echo object_input_tag($newsletter, 'getIsPublished') ;
?>

So my current field value would be retrieved, and the Object helper generates form fields using BasePeer::TYPE_FIELDNAME which in turn is used in my fromArray method to update the Db.

Normally one would use select_tag with internal field names (BasePeer:: TYPE:PHP_NAME) and write more action code to marry them up before a save(), however here I have to add my database field name into my view code.

Tethering your templates to your database schema is a route no-one should go down. So the internal name should be converted to the Db name on-the-fly in your template. The Peer classes have a function for this, and its public. The following call converts your usual internal field name for you, ready to drop into your select_tag:


<?php
$IsPublished_translated = BaseNewsletterPeer::translateFieldName(»‘IsPublished’,BasePeer::TYPE_PHPNAME, BasePeer::TYPE_FIELDNAME )
?>

You could assign this in your action, but it works just as well in the template:

<?php
echo select_tag(»
BaseNewsletterPeer::translateFieldName(»
‘IsPublished’,BasePeer::TYPE_PHPNAME, BasePeer::TYPE_FIELDNAME
‘,»
options_for_select(array(»
0, 1),
$defaultOption));
?>

A final step would be to migrate this into a function higher up that so as to make templates a little prettier when editing.

First spam attempt on Drag n’ Drop captcha

July 14th, 2008
post #118 

As its still in development, I’ve set the drag n’ drop captcha mechanism to report failures in full as well as forwarding legitimate responses.
I’ve had the first (update: two now) such failure notice from a live installation today - subject line: ‘yGAQJUnxHNOw’ and just a few web addresses I won’t repeat here.

Positive so far.

Drag n’ Drop captcha updated

July 7th, 2008
post #110 

I finally have this released into production environments which will no doubt help me to improve it. There’ve been some updates to the mechanism to dissociate image filenames from the puzzle fields.

Next I need to make the names more variable, and perhaps cache them and generate random permutations. Accessibility remains a concern.

dragndrop-captcha

Post Index for Wordpress

December 20th, 2006
post #37 

no.229My first Wordpress Plugin.

I couldn’t find anything that would get me the index of the current displayed post, so this does the job:

userpostindex_v1

Once in The Loop, it counts the current author’s other, older posts to arrive at the index for the current post, ignoring private posts, attachments and the like.

My own implementation is only in text for now, the idea ccoming from the more stylish treatment of the edition number flash on AListApart’s homepage.

MVC nesting

November 13th, 2006
post #31 

Coursework for the MSc has been going at varying speeds for a week now. I lost sleep earlier on when trying fine tune the MVC setup (no Cake here). Then things sped up nicely over the weekend when I began happily juggling tabled views around.

Then I thought it might be nice to display the user’s basket on all relevant pages, as a courtesy. This was easier than I’d thought. I simply add a new MVC and merge it’s results.

I have a master array of values matching each area of my template, I array_merge it with that which I receive from View to populate the page. If a user is logged in, a new step merges the user info MVC set with the main View prior to overwriting the defaults. This handles and returns all their account/basket details neatly into a sidebar, all without breaking the rules or setting up problems for later.

Drag n’ Drop CAPTCHA puzzle

September 8th, 2006
post #19 

Drag n Drop CAPTCHA mechanism screenie

Simply re-arrange the images to make a visible key before clicking submit.

Fiddling with Scriptaculous earlier today I had an idea for a CAPTCHA mechanism. Cycling through the $_POST array in PHP gives you the form data, in order of submission.Scriptaculous’ sortable feature allows you to change order of items in a List, so adding an image with corresponding hidden input field gives you a basic puzzle to satisfy the CAPTCHA requirement.

Simple Drag n’ Drop CAPTCHA (written 8th September, 2006, code available on request).

Still comes with all the same accessibility problems most mechanisms have (with the added JS requirement), but saves the need for image generation.This version *can* use any naming convention for the images and input field names, they’re generated and checked by an array so don’t need to be numerically ordered - which could be a giveaway.

Phorum module updated

August 28th, 2006
post #15 

No way to spend a BH Monday.

Started the day reading a mail from someone about my mod_change_username thingy for Phorum. Decided it was high time I address the rather tight restriction on username formats (tighter than Phorum’s own).

Tested it with Phorum 5.1.15 while I was at it - and barring some anomalies in the upgrade it seems to work as expected. V1.2 now available in the downloads section.