Validation of a field if checkbox 'checked'
Validation of a field if checkbox 'checked'
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
Well, thinking about it I thought I could use .dependent() and jQuery to do something:
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.
In terms of validation, a global validator can be used to check across multiple fields.
Allan
Ok thanks, I'll have a look at it!