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?
Form Field Id's - Do They Ever Change?
Form Field Id's - Do They Ever Change?
Green Sheep Design Ltd. - www.greensheep.co.uk
Re: Form Field Id's - Do They Ever Change?
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.
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.
Re: Form Field Id's - Do They Ever Change?
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...
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
Re: Form Field Id's - Do They Ever Change?
I believe this is the most common way to do this.markS wrote: ...
I decided to pass the value via a smarty tag to my javascript.
...
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

Re: Form Field Id's - Do They Ever Change?
My solution:
When you create a form with function CreateFormStart(), put your validation script call as the last parameter, e.g.:
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:
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.'\')"');
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;
}
}