Re: DataObject_FormBuilder conditions

From: Date: Mon, 07 Feb 2005 20:41:53 +0000
Subject: Re: DataObject_FormBuilder conditions
References: 1 2 3  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-17434@lists.php.net to get a copy of this message
On Mon, 7 Feb 2005 14:21:43 -0500, James Stewart <lists@jystewart.net> wrote: > On Feb 4, 2005, at 1:20 PM, Justin Patrin wrote: > > FormBuilder doesn't use joins for any of this (that just wouldn't > > work) and a whereAdd like that is never going to work for this. Good > > try, though. :-) > > > > The way to limit values in a select box is to use the > > prepareLinkedDataObject callback in your DataObject. See the docs: > > > > http://opensource.21st.de/tiki-index.php?page=Callback+methods#id427020 > > Aha. Thanks once again. > > One more question about FormBuilder, if I may. I've seen some > references to handling of unique keys when googling and looking through > the archives, but can't see if there's a standard solution. > > In particular, I have a table with a username which must be unique. > I've defined it as a unique key, but when I use processForm to handle a > form with a duplicate username > > $form->process(array(&$fg,'processForm'),false); > > returns 1 despite failing to do the update. > > Do I need to write my own handler in preProcessForm() or is there some > standardised way of handling this? > This pertains to one of our feature requests to return false if the insert / update fails. We haven't gotten around to putting it in. However, for this instance I would suggest you add a (callback) rule to the form (in postGenerateForm) which checks to make sure that the posted username is unique. True, this is prone to race conditions (someone could register with another username after you validate but before you insert) but this isn't likely. In addition, by using a form rule you get nice output right there in the form. Here's a quick and dirty example of one that I created. function &getDB() { if (!isset($GLOBALS['__db_instance'])) { require_once('DB.php'); $GLOBALS['__db_instance'] =& DB::connect('mysql://user:pass@host/db'); } return $GLOBALS['__db_instance']; } function uniqueEmail($email) { $db =& getDB(); if ($db->getOne('SELECT id FROM user WHERE email = '.$db->quoteSmart($email))) { return false; } return true; } //create form here $form->addRule('email', 'This e-mail address has been used to sign up already.', 'callback', 'uniqueEmail'); -- Justin Patrin

« previous php.pear.general (#17434) next »