Date Field Based on fields in Group not Empty

Tessa

Member
Hello!

I have a question about Date Fields.

I have 3 groups on a list. Each group needs one date field.

The date field should only log the date once all fields within that group is "not empty", meaning those fields have something selected in it.

So my question is, how would I begin or how would I structure the code? I can probably figure it out with a little guidance. :)

The purpose of this is to say, "If all the items exist for the group, Log the date so we can report when the graphic and other parts for the product was complete!" :)

Thanks in advance!
 
say you're elements that need to be filled in are called list___one, list___two, list___three and your date element is list___complete_date:

create a js file in components/com_fabrik/js/form_X.js (replace X with your form's id)

Code:
function fieldsFilledIn() {
  var form = Fabrik.blocks['form_X'];
  var elements = form.formElements;

  var one = elements.get('list___one').get('value');
  var two = elements.get('list___two').get('value');
  var three = elements.get('list___three').get('value');
  var dateEl = elements.get('list___complete_date');
  var date = dateEl.get('value');

 // Check if all three fields have been filled in
  if (one !== '' && two !== '' && three !== '' ) {

    // If the date is already set - don't reset it.
    if (date !== '') {
      return;
    }
    var now = new Date();
    dateEl.update(now);
  }
}

then in each of the elements one, two, three add a js event:

on blur:
Code:
fieldsFilledIn();

I've not tested this, so there may be so errors in it but the general principle is sound.
 
Hi Rob,


Thank you for your time on this. Unfortunately I can't seem to get it to work and have tried altering different parts, and the logic makes complete sense, but I guess I don't have an eye for JavaScript code.

I have rechecked everything over and over again and I still can't seem to spot why the date field isn't logging anything. And each field, colorplot, esm, and printdwg has a javascript onblur event "graphicExist();" and I see the form_25.js file being loaded on the form as well.

Any ideas for troubleshooting? =)

Code:
function graphicExist() {
  var form = Fabrik.blocks['form_25'];
  var elements = form.formElements;

  var colorplot = elements.get('tblskufiles___graphiccolorplot_file').get('value');
  var esm = elements.get('tblskufiles___graphicesm_file').get('value');
  var printdwg = elements.get('tblskufiles___graphicprintdwg_file').get('value');
  var dateEl = elements.get('tblskufilesgraphic_upload_date');
  var date = dateEl.get('value');
 // Check if all three fields have been filled in
  if (colorplot !== '' && esm !== '' && printdwg !== '' ) {
    // If the date is already set - don't reset it.
    if (date !== '') {
      return;
    }
    var now = new Date();
    dateEl.update(now);
  }
}
 
As usual with JS, we'd need to see the page and debug it in Firebug.

If you use Firebug, and put a breakpoint in the first line of your function, does the code actually run (and break into the debugger)? If so, step through each line, and see where you start getting unexpected results.

One thing I see is that you don't seem to have the full element name (including the list___) for your date element's ID.

-- hugh
 
As usual with JS, we'd need to see the page and debug it in Firebug.

If you use Firebug, and put a breakpoint in the first line of your function, does the code actually run (and break into the debugger)? If so, step through each line, and see where you start getting unexpected results.

One thing I see is that you don't seem to have the full element name (including the list___) for your date element's ID.

-- hugh


Hi Hugh,

I can't believe I missed those underscores. I swear I looked at that for hours. I always think it's something more complex. I'm sure you've been through that a zillion times.

It works perfectly now. Thank you Hugh and Rob.
 
Yup. Amazing how often someone can take a look at code I've been staring at for hours, and point to something blindingly simple and say "shouldn't that be ....".

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Staff online

Members online

Back
Top