Form Field Id's - Do They Ever Change?

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
tomgsd
Forum Members
Forum Members
Posts: 74
Joined: Tue Feb 12, 2008 10:00 am

Form Field Id's - Do They Ever Change?

Post by tomgsd »

Just a quick question.

I'm creating a module that has a form I need to validate with Javascript.  When I output the fields, the id's are made up of a random string and the name I've given.

I'm planning on referring to the fields by their id's and wanted to know if this random string ever changes?  Will the fields always have exactly the same name?
Green Sheep Design Ltd. - www.greensheep.co.uk
markS
Forum Members
Forum Members
Posts: 58
Joined: Wed Aug 20, 2008 3:04 pm

Re: Form Field Id's - Do They Ever Change?

Post by markS »

Hello,

I've needed to do the same thing with a site in the past.  Because I wasn't entirely sure it would remain the same, and I couldn't find a clear answer to that question, I decided to pass the value via a smarty tag to my javascript.

Something like:

[php script]
//jquery needs the id that gets prefixed to all the inputs to work in a sensible manner...
$this->smarty->assign('buttonPrefix', $id);

[js in template]

{literal}buttonPrefix={/literal}'{$buttonPrefix}';


Then I can use that value within my javascript and not worry about it. This seems to be a pretty robust solution, at least as far as I've found.  Maybe someone will put us out of our misery about the permanence, or not, of the  id values!

Cheers,
          Mark.
tomgsd
Forum Members
Forum Members
Posts: 74
Joined: Tue Feb 12, 2008 10:00 am

Re: Form Field Id's - Do They Ever Change?

Post by tomgsd »

I'm glad it's not just me wondering about this!  I had actually planned on doing exactly the same as you have suggested just to be sure though.

I've also had a quick look through the code of some of the other modules but couldn't find a suitable example.  I suppose the other way around it would be to use a wrapper div and target that instead.

It would be nice to hear the official line on this though...
Green Sheep Design Ltd. - www.greensheep.co.uk
NaN

Re: Form Field Id's - Do They Ever Change?

Post by NaN »

markS wrote: ...
I decided to pass the value via a smarty tag to my javascript.
...
I believe this is the most common way to do this.
I've done this several times and even seen this in other modules.

Module ids are needed since modules can be called several times on a page.
And to define what module call will be replaced by what module output the CMS core needs this module id.

Example: If you have the news module twice showing summaries of two different categories with output limited to 5 articles per page and you select page two of one category it would also switch to page 2 of the other category.
Or all the params of the first module call would also be passed to the second module call if they are not explicitly overriden. So if the first call has a special summary template (and therefore the summarytemplate param is used) and the second one should use the default summary template (therefore you don't use the summarytemplate param) it would show the special template even for the second call.

So the module id is prepended to all params that are passed to a module instance to make sure that only this instance will get those params that actually belongs to it.

To avoid multiple html ids the core also uses this module id to create the html id.
So you always have unique ids in your html output.

If you use a wrapper div with an own id you may call your module just one time since the wrapper will have a static id. So if you would call the module twice your validation script won't work properly since you also will have the same id twice.

Just my 2 cents ;)
katon

Re: Form Field Id's - Do They Ever Change?

Post by katon »

My solution:
When you create a form with function CreateFormStart(), put your validation script call as the last parameter, e.g.:

Code: Select all

	$form_start = $this->CreateFormStart($id, 'default', $returnid, 'post', '', false, '', array(), 'onsubmit="return validate(\''.$id.'\')"');
As you see, I am passing the $id parameter to javascript. It contains the module id string.

My javascript validation function would look like this:

Code: Select all

	function validate(prefix)
	{
		if(document.getElementsByName(prefix + 'myFieldId')[0].value = '')
		{
			alert('You are submitting an empty field');
			return false;
		}
		else
		{
			return true;
		}
	}
Post Reply

Return to “Developers Discussion”