定制 christianberkman/codeigniter4-date-validation 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

christianberkman/codeigniter4-date-validation

Composer 安装命令:

composer require christianberkman/codeigniter4-date-validation

包简介

Custom validation library for the CodeIgnitor4 framework to validate and compare dates

README 文档

README

Custom validation library for the CodeIgniter4 framework to validate and compare dates.

To Do

  • Add rules:
  • Use CodeIgniter4 coding standard library
  • Create unit tests
  • Use languae files for errors

Installation

Installation via composer:

composer require christianberkman/codeigniter4-date-validation

If auto discovery in your CodeIgniter app is enabled, the library will be discovered automatically.

Manual installation

Copy src/DateValidation.php to app/ThirdParty, adjust namespace and register in app/Config/Validation.php

Usage

The library offers rules for validating the value date to today or another field. All rules also validate the date so the built-in valid_date rule does not need to be called.

// any date before today's date
date_before_today[Y-m-d]    

// any on or after a given field date
date_starting[date_arrival,Y-m-d]

// any date on Monday (1) or Wednesday (3)
date_on_dow[Y-m-d,1,3]

Date Format

When comparing the value date to a field date, both dates are assumed to be in the same format. All format parameters are optional. If the format paremeter is specified, the dates will be creaed using DateTime::createFromFormat. If the format paremeter is omitted, the date is created using strtotime(). It is reccommended to always specify a format.

Exception: The format parameter is required for the date_on_dow rule, 'none' can be specified if you wish to not specify any format: 'today' -> date_on_dow[none,1]

Time information

All time information is discarded, all times will be set to 00:00:00 UTC.

Errors

Errors are dynamically returned by each rule, e.g. Date must be before last_available_date field.

Available Rules

Rule Parameters Discription Example
date_before_today format Validates if the value date is before today date_before_today[Y-m-d]
date_ending_today format Validates if the value date is on or before today date_ending_today[Y-m-d]
date_starting_today format Validates if the value date is on or after today date_starting_today[Y-m-d]
date_after_today format Validates if the value date is after today date_after_todat[Y-m-d]
date_before field, format Validates if the value date is before a field date date_before[departure_date,Y-m-d]
date_ending field, format Validates if the value date is on or before a field date date_ending[departure_date,Y-m-d]
date_starting field, format Validates if the value date is on or after a field date date_starting[arrival_date,Y-m-d]
date_after field, format Validates if the value date is after a field date date_after[date_of_birth,Y-m-d]
date_on_dow format, days of week Validates if the value date is on a given day of week starting Monday (1-7) date_on_dow[Y-m-d,1,2,3,4,5]

Full Example

$data = [
    'arrival_date' => '2024-07-01',
    'excursion_date' => '2024-07-03',
    'depatrue_date' => '2024-07-10',
    'last_available_date' => '2027-08-09',
];

$rules = [
    // arrival needs to be at earliest tomorrow
    'arrival_date' => 'date_after_today', 
    // excursion needs to be between (and not on) departure and arrival
    'excursion_date' => 'date_after[arrival_date,Y-m-d]|date_before[depatrue_date,Y-m-d]', 
    // departure needs to be after (and not on) the arrival date and before (and not on) the last available date
    'departure_date' => 'date_after[arrival_date,Y-m-d]|date_before[last_available_Date]', 
];

$validation = $this->validateData($data, $rules); // returns false
$errors = $this->validator->getErrors();
/**
 * Result:
 * 'departure_date' => 'Date must be before last_available_date field'
 */

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-26