Limit the number of responses in databasejoin - checkbox

Status
Not open for further replies.

adriannunez

Member
Hi
I'm using this code as in the Wiki:

Code:
var el = Fabrik.blocks.form_10.formElements.get('avisos___aviso_otras_actividades');
if (el.numChecked() > 3 ) {
alert('Message');
event.stop();
}

but it simply doesn't work: I can check any number of boxes.
Somebody know if there's a bug?
 
The code is in the javascript section of the databsejoin element that is rendered as checkbox, and the action is "click".
Adrian
 
I have found this example code in the Element wiki (which I have linked for convenience).

When I tried this myself, I looked in the page HTML to find the line which sets the form variables and it was as follows:
JavaScript:
var form_19_1 = Fabrik.form('form_19_1', 19, ...
So it looks like the format of the form-id has changed (probably to handle cases where you have embedded the same form twice in a module or article on your web page).

Can you try:
JavaScript:
var el = form_10_1.formElements.get('avisos___aviso_otras_actividades');
if (el.numChecked() > 3 ) {
    alert('Message');
    event.stop();
}

Thx.

S
 
Is this field in a repeat group? If so, then you may have a little difficulty since the element name is suffixed with _n where n identifies the repeat group row.

However when I added a _0 to mine, the following code worked and a message popped up:
JavaScript:
var el = Fabrik.blocks.form_19_1.formElements.get('zsupport_main_49_repeat___rpt_checkbox_0');
if (el.numChecked() > 2 ) {
  alert('Message');
  event.stop();
}

S
 
P.S. Looking in element.php, I note that you can use the word "this" with js events and it puts in the correct placeholder. So if you set the code to:
JavaScript:
if (this.numChecked() > 2 ) {
  alert('Message');
  event.stop();
}
then it works on each instance in a repeat group too.

S
 
Many thanks, Sophist! But it didn't work.
Yes, I think it's a repeat group because a checkbox in a databasejoin element autogenerates a table with "repeat" word in it's name (because it's a many-to-many relationship). But I guess I've tried everything you kindly suggested and it didn't do the trick. Not even using "this" nor "_0" at the end. I might be using a wrong name for calling the element (the form seems to be simply form_10 without the ending _1). Look, this is my test page (as you can see, now you can select unlimited boxes): http://web-peru.info/webs/yofestejo/index.php?option=com_content&view=article&id=9&Itemid=146
Adrian
 
No - the use of repeat in a multi-select dbjoin is just confusing - it is not a repeat group.

(In Chrome at least) if you load the page, and go into developer tools (in Chrome you press F12) and bring up the console, and at the console prompt type:
Code:
form_10.formElements
then it dumps the object and if you open it, you can see that it has the following members:
Code:
avisos___aviso_actividad_principal: Object
avisos___aviso_fotos: Object
avisos___aviso_miembro: Object
avisos___aviso_otras_actividades: Object
avisos___aviso_precio_maximo: Object
avisos___aviso_precio_minimo: Object
avisos___aviso_precio_por: Object
avisos___aviso_puntaje: Object
avisos___aviso_texto: Object
avisos___aviso_usuario_bloqueado: Object
avisos___date_time: Object
avisos___id: Object

But your JS still says:
JavaScript:
var el = Fabrik.blocks.form_10.formElements.get('avisos_repeat_aviso_otras_actividades___aviso_otras_actividades_0');
if (el.numChecked() > 2 ) {
    alert('Message');
    event.stop();
}

So "avisos_repeat_aviso_otras_actividades___aviso_otras_actividades_0" should probably be just "avisos___aviso_otras_actividade".

Or even simpler use the simplified code I provided (and which I took the time to test on a checkbox element):
JavaScript:
if (this.numChecked() > 2 ) {
  alert('Message');
  event.stop();
}

S
 
Thanks again! The thing is that "avisos___aviso_otras_actividades" was the first option I tried (as I said in my first post) and when I used "this" also fails. I just rewrote the JS code to "avisos___aviso_otras_actividades". Still not working :(
 
Ok - the main problem is that numChecked is only currently available in checkbox element, not in databasejoin when used as a checkbox.

Try this (which takes the numChecked code from checkbox):
JavaScript:
var numChecked = this._getSubElements().filter(function (c) {return c.checked}).length;
if (numChecked > 2) {
  alert('Message');
  event.stop();
}

S

P.S. Not tested this myself.
 
Just tried this - looks like it is returning double the correct number because dbjoin has hidden fields to hold the raw value for each visible checkbox.

Here is updated code to fix this:
JavaScript:
var numChecked = this._getSubElements().filter(function (c) {return c.value !== "0" ? c.checked : false}).length;
if (numChecked > 2) {
  alert('Message');
  event.stop();
}

S
 
Wow, this is working! Thanks!
One aditional question: If I use "numChecked > X" is it possible to get the value of X from the database or a placeholder? (the value of X should depend on the group of the user who is editing the form or any other varible associated with its user account)
 
Yes - you need to put the value X into a hidden HTML field so you can pick it up in the browser.

Then use form_10.formElements.get('field name').value to get X.

S
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top