Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BcFormHelper->dateTimePicker 一度選択するまで日時の書式が正しくない #1930

Closed
seto1 opened this issue Feb 15, 2023 · 1 comment
Labels
Bug バグ

Comments

@seto1
Copy link
Collaborator

seto1 commented Feb 15, 2023

問題

画面を開いた初期状態では、dateTimePickerのhiddenの日時の書式が「Y/m/d H:i:s」になっている。
そのまま保存ボタンを押下し、リクエストをDBに保存した場合、日時がnullで保存されてしまう。

これを避けるには、以下のブログの例のように変換処理を毎回行う必要がある。
https://github.com/baserproject/ucmitz/blob/dev/plugins/bc-blog/src/Service/BlogPostsService.php#L507

if (!empty($postData['posted'])) $postData['posted'] = new FrozenTime($postData['posted']);
if (!empty($postData['publish_begin'])) $postData['publish_begin'] = new FrozenTime($postData['publish_begin']);
if (!empty($postData['publish_end'])) $postData['publish_end'] = new FrozenTime($postData['publish_end']);

一度選択すると書式が正しくなるのは、以下でJavaScriptによる変換処理を入れているためだと思われる。
https://github.com/baserproject/ucmitz/blob/dev/plugins/baser-core/src/View/Helper/BcFormHelper.php#L315

<script>
$(function(){
    var id = "{$options['id']}";
    var time = $("#" + id + "-time");
    var date = $("#" + id + "-date");
    time.timepicker({ 'timeFormat': 'H:i' });
    date.add(time).change(function(){
        if(date.val() && !time.val()) {
            time.val('00:00');
        }
        var value = date.val().replace(/\//g, '-'); // <--------------
        if(time.val()) {
            value += ' ' + time.val();
        }
        $("#" + id).val(value);
    });
});
</script>

改善案

https://github.com/baserproject/ucmitz/blob/dev/plugins/baser-core/src/View/Helper/BcFormHelper.php#L258
このあたりに、以下のような書式変更処理を入れる

if (getType($value) === 'object' && get_class($value) === 'Cake\I18n\FrozenTime') {
    $value = $value->format('Y-m-d H:i:s');
}
@seto1
Copy link
Collaborator Author

seto1 commented Feb 15, 2023

ブログの場合、FrozenTimeへの変換処理を消すと以下の型チェックでバリデーションエラーになる

https://github.com/baserproject/ucmitz/blob/dev/plugins/baser-core/src/Model/Validation/BcValidation.php#L424

public static function checkDate($value)
{
    if (!$value instanceof FrozenTime) return false;
    return true;
}

ただ、このルールそのものがcake標準のdatetimeルールと重複している可能性はあり

https://github.com/baserproject/ucmitz/blob/dev/plugins/bc-blog/src/Model/Table/BlogPostsTable.php#L199

$validator
    ->dateTime('posted') // <----------------------------------------------
    ->notEmptyString('posted', __d('baser', '投稿日を入力してください。'))
    ->add('posted', [
        'checkDate' => [
            'rule' => ['checkDate'], // <----------------------------------
            'provider' => 'bc',
            'message' => __d('baser', '投稿日の形式が不正です。')
        ]
    ]);

@ryuring ryuring added the Bug バグ label Feb 16, 2023
@ryuring ryuring transferred this issue from baserproject/ucmitz Apr 16, 2023
@ryuring ryuring added the ver5 label Apr 16, 2023
ryuring pushed a commit that referenced this issue Apr 17, 2023
Co-authored-by: Đỗ Văn Hùng <HungDV2022>
seto1 added a commit to seto1/basercms that referenced this issue May 17, 2023
@gondoh gondoh closed this as completed in 2f49612 May 18, 2023
gondoh added a commit that referenced this issue May 18, 2023
fix #1930 FrozenTime::setToStringFormat 書式変更
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug バグ
Projects
None yet
Development

No branches or pull requests

2 participants