Validation of a field if checkbox 'checked'

Validation of a field if checkbox 'checked'

sdroulerssdroulers Posts: 43Questions: 14Answers: 0
edited September 2017 in DataTables

Hi,

I'm dealing with building an employee schedule form where I need for to tell on which day (on a week basis) an employee works and from when to when.

So I have a field of checkboxes with options : {"Monday":1, "Tuesday":2, "Wednesday":3, "Thursday":4, "Friday":5, "Saturday":6, "Sunday":0} and 14 (!!) datetime fields with the start_time en end_time for each day (7*2).

So far I can check one day and not fill the corresponding start and end dates and this is an issue for me. I would need to forbid such things.

For example if "Monday" is checked, there must be a start and an end time in the related datetime fields.

Here is an extract of my JS:

  {
  "label": "Working days:",
  "name": "specific_daily_schedule",
  "type": "checkbox",
  "options": {"Monday":1, "Tuesday":2, "Wednesday":3, "Thursday":4, "Friday":5, "Saturday":6, "Sunday":0},
  "separator": ','
  },
  //Datetime fields, 2 for each day, start and end times
  {
  "label": "Monday",
  "name": "start_D_1",
  "type": "datetime",
  "format": 'HH:mm'
  },
  {
  "label": "Monday",
  "name": "end_D_1",
  "type": "datetime",
  "format": 'HH:mm'
  },

And my php:

    Field::inst( 'specific_daily_schedule'),
Field::inst( 'start_D_1' )
    ->getFormatter( 'Format::datetime', array(
        'from' => 'H:i:s',
        'to' =>   'H:i'
    ) )
    ->setFormatter( 'Format::datetime', array(
        'from' => 'H:i',
        'to' =>   'H:i:s'
    ) )
    ->validator( 'Validate::dateFormat', array(
        'format' => 'H:i'
    ) ),
 Field::inst( 'end_D_1' )
    ->getFormatter( 'Format::datetime', array(
        'from' => 'H:i:s',
        'to' =>   'H:i'
    ) )
    ->setFormatter( 'Format::datetime', array(
        'from' => 'H:i',
        'to' =>   'H:i:s'
    ) )
    ->validator( 'Validate::dateFormat', array(
        'format' => 'H:i'
    ) ), ....

So far the form is saving data perfectly but I have this validation issue I'm facing.

Many thanks and all advices welcomed if you would have done that (not creating so many fields...) differently !

Sébastien

Answers

  • sdroulerssdroulers Posts: 43Questions: 14Answers: 0

    Well, thinking about it I thought I could use .dependent() and jQuery to do something:

    editor.dependent( 'specific_daily_schedule', function ( val ) {
    
        var days_checked = val.split(",")
        var arrayLength = days_checked.length;
    
        for (var i = 0; i < arrayLength; i++) {
    
            if($("#DTE_Field_start_D_"+days_checked[i]).val()==""){
                $("#DTE_Field_start_D_"+days_checked[i]).val(" ")
            }
            if($("#DTE_Field_end_D_"+days_checked[i]).val()==""){
                $("#DTE_Field_end_D_"+days_checked[i]).val(" ")
            }
        }
    })
    

    Replacing the empty value by a space, it throws an error a validation which is ok. But still looking for a more elegant way to do it.

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin

    In terms of validation, a global validator can be used to check across multiple fields.

    Allan

  • sdroulerssdroulers Posts: 43Questions: 14Answers: 0

    Ok thanks, I'll have a look at it!

This discussion has been closed.