Re: HTML_QuickForm2_Node: ID generation

From: Date: Tue, 04 May 2010 09:43:48 +0000
Subject: Re: HTML_QuickForm2_Node: ID generation
References: 1 2  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-30497@lists.php.net to get a copy of this message
Le 4 mai 2010 à 10:16, Alexey Borzov a écrit : > Hi Christian, > > On 03.05.2010 23:04, Christian Weiske wrote: >> Element IDs have to be specified explicitly in the attributes array, >> otherwise they are autogenerated - with an index appended to them: >> Element named "username" gets ID "username-0". While this makes sense >> for large and complicated forms, it is not really convenient for small >> forms with no duplicated element names. >> >> How am I supposed to get "pretty" IDs automatically? Overwriting >> generateID() is not really possible, since I would have to do that for >> every single element class. > > Autogenerating such IDs was Bertrand's idea (CCing him), I was actually in favor of your > approach. There probably were some good reasons, but I can't quite remember them, hopefully > Bertrand will be able to shed some light here. It makes the element id more predictable than having "username" and then "username-1". It also simplify javascript code in case you have to deal with element ids. For example, it is more consistent and easier to write : var id = "username-" + i; than : var id; if (i == 0) { id = "username"; } else { id = "username-" + i; } It also makes regex matching on elements id faster and easier, CSS3 will support basic regex selectors IIRC. And it reduces your chances to break your own code since you know what to expect all the time, be there one or many elements. I think you have to get used to it to start to see the benefit, I admit it doesn't look "pretty" at first. We could also propose a way to override Node::generateId(), it's just a static function, it could be replaced by any php callback, ids are stored in a static variable as well : Node::useIdGenerator('generateMyId') -- Bertrand Mansion Work : http://www.mamasam.com

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