Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Fix the Widget::getAttributesFromDca() for "time" rgxp. #7721

Closed
wants to merge 1 commit into from
Closed

Fix the Widget::getAttributesFromDca() for "time" rgxp. #7721

wants to merge 1 commit into from

Conversation

discordier
Copy link
Contributor

We have discovered a problem, where a widget with an rgxp of "time" refuses to be created.
The problem is, that the \Date object is created without format string and therefore uses date as fallback.
The result is, that the parsed value is totally wrong (I encountered "00:00:00" for most values) and can even result in a OutOfBoundsException.

\Contao\Config::set('timeFormat', 'H:i:s');
\Contao\Config::set('dateFormat', 'Y-m-d');
\Contao\Config::set('datimFormat', 'Y-m-d H:i:s');

$prepared = Widget::getAttributesFromDca(
array('inputType' => 'text', 'eval' => array('rgxp' => 'time')),
'test',
'12:21'
);
var_dump($prepared['value']);
// Before: string(8) "00:00:00"
// After:  string(8) "12:21:00"

$prepared = Widget::getAttributesFromDca(
array('inputType' => 'text', 'eval' => array('rgxp' => 'time')),
'test',
'12:21:33'
);
var_dump($prepared['value']);
// Before: OutOfBoundsException: Invalid date "11:22:33"
// After:  string(8) "12:21:00"

$prepared = Widget::getAttributesFromDca(
array('inputType' => 'text', 'eval' => array('rgxp' => 'date')),
'test',
'12:21:33'
);
var_dump($prepared['value']);
// Before: OutOfBoundsException: Invalid date "11:22:33"
// After:  OutOfBoundsException: Invalid date "11:22:33"

$prepared = Widget::getAttributesFromDca(
array('inputType' => 'text', 'eval' => array('rgxp' => 'datim')),
'test',
'12:21:11'
);
var_dump($prepared['value']);
// Before: OutOfBoundsException: Invalid date "11:22:33"
// After:  OutOfBoundsException: Invalid date "12:21:11"

The behaviour for invalid values is retained but now correct for valid values.

@leofeyer
Copy link
Member

Why did you use call_user_func()?

// Yours
new \Date($varValue, call_user_func(array('Date', 'getNumeric' . ucfirst($arrData['eval']['rgxp']) . 'Format')));

// Alternative
$method = 'getNumeric' . ucfirst($arrData['eval']['rgxp']) . 'Format';
new \Date($varValue, \Date::$method());

@leofeyer leofeyer added this to the 3.2.20 milestone Mar 26, 2015
@leofeyer leofeyer self-assigned this Mar 26, 2015
@leofeyer
Copy link
Member

Is the following fix ok for you, too?

        // Convert timestamps
        if ($varValue != '')
        {
            switch ($arrData['eval']['rgxp'])
            {
                case 'date':
                    $objDate = new \Date($varValue, \Date::getNumericDateFormat());
                    $arrAttributes['value'] = $objDate->date;
                    break;

                case 'time':
                    $objDate = new \Date($varValue, \Date::getNumericTimeFormat());
                    $arrAttributes['value'] = $objDate->time;
                    break;

                case 'datim':
                    $objDate = new \Date($varValue, \Date::getNumericDatimFormat());
                    $arrAttributes['value'] = $objDate->datim;
                    break;
            }
        }

It is a little more explicit.

@discordier
Copy link
Contributor Author

The last one will raise the complexity but anything is ok that works. :D

Shall I update my PR?

@leofeyer
Copy link
Member

No. I have found at least 5 more occurrences of the same problem, so I have to work on this anyway :(

@leofeyer
Copy link
Member

Fixed in d30fb79.

@leofeyer leofeyer closed this Mar 26, 2015
@discordier discordier deleted the fix/validate-time-correctly branch March 26, 2015 15:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants