Giter Site home page Giter Site logo

yii2-datetime-compare-validator's Introduction

yii2-datetime-compare-validator

Validator for yii2 allows you to compare dates.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require nepstor/yii2-datetime-compare-validator "^2.0"

or add

"nepstor/yii2-datetime-compare-validator": "^2.0"

to the require section of your composer.json file.

Using

public function rules()
{
    return [
        ['begin', nepstor\validators\DateTimeCompareValidator::className(), 'compareAttribute' => 'end', 'format' => 'Y-m-d', 'operator' => '>=']
    ];
}

Properties

This validator compares the specified input datetime with another one and make sure if their relationship is as specified by the operator property.

  • compareAttribute: the name of the attribute whose value should be compared with.
  • compareValue: a constant value that the input value should be compared with. When both of this property and compareAttribute are specified, this property will take precedence.
  • operator: the comparison operator. Defaults to =. The following operators are supported:
    • =: check if two values are equal. The comparison is done is non-strict mode.
    • !=: check if two values are NOT equal. The comparison is done is non-strict mode.
    • >: check if value being validated is greater than the value being compared with.
    • >=: check if value being validated is greater than or equal to the value being compared with.
    • <: check if value being validated is less than the value being compared with.
    • <=: check if value being validated is less than or equal to the value being compared with.
  • format: Date format to parse values with. Defaults to Y-m-d.
  • jsFormat: Date format to parse values with client side. Defaults to YYYY-MM-DD.

yii2-datetime-compare-validator's People

Contributors

gael-wogenstahl avatar matperez avatar nepstor avatar philippfrenzel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

yii2-datetime-compare-validator's Issues

validateAttribute never returns the passed messages if clientValidation is false

In the validateAttribute there is a switch on the operator which only ever returns the default messages and not the one passed by the validator config specified.

example:

switch ($this->operator) {
case '=':
if ($valueDT != $compareValueDT) {
$message = $this->_messages[$this->operator];
}
break;

This means that if you are not using clientValidation the error message is not the one you set in the config. (function clientValidateAttribuite does return the config message). I think the above (and cases like it for other operators in the same switch) should be:-

switch ($this->operator) {
case '=':
if ($valueDT != $compareValueDT) {
$message = $this->message;
}
break;

Sorry if I've got the wrong end of the stick here

Undefined variable: compareAttribute

exception 'yii\base\ErrorException' with message 'Undefined variable: compareAttribute' in vendor/nepstor/yii2-datetime-compare-validator/DateTimeCompareValidator.php:98

$this->addError($model, isset($compareAttribute) ? $attribute : $compareAttribute, Yii::t('yii', 'Invalid compare value date format: {value}'), ['{value}' => $compareValue]);
/** correctly: */
$this->addError($model, isset($compareAttribute) ? $compareAttribute : $attribute, Yii::t('yii', 'Invalid compare value date format: {value}'), ['{value}' => $compareValue]);
/** correctly without translate value error: */
$this->addError($model, isset($compareAttribute) ? $compareAttribute : $attribute, Yii::t('yii', 'Invalid compare value date format: {value}', ['value' => $compareValue]));

Add new tag

Last month you fixed a few issues; it would be great if these commit(s) could be tagged so we can benefit without having to use dev-master as a version requirement.

Install

Your requirements could not be resolved to an installable set of packages.

Problem 1
- The requested package nepstor/yii2-datetime-compare-validator could not be found in any version, there may be a typo in the package name.

Potential causes:

Read http://getcomposer.org/doc/articles/troubleshooting.md for further common problems.

It work but hot to make it work on the client side ?

I used it like the following

['PostDate', \nepstor\validators\DateTimeCompareValidator::className(), 'compareAttribute' => 'PostEndDate', 'format' => 'Y-m-d', 'operator' => '<','message'=>'something wrong']

the form submit but it don't insert, should it show something on the client side

Release a first version

Your manual says we should require any released version ("*") or this package, but no version has been released yet, so we have to end up using "dev-master", which is not a very good idea. Please tag an initial release.
Doing that would also fix #3, which is basically the same bug as this, but worded differently.

Validation 2021-01-01 >= 2020-12-01 does not work as expected

Hi,

I just stumbled upon your extension and it seems to work,except...
if I have a start date '2020-12-01' and an end date '2021-01-01' then the extension says that the latter has to be bigger...

If I set the end date to '2021-01-02' it works.

Thanks in advance.

Tagged release.

Is this stable? If so please add a v1.0.0 tag so it gets a stable package on composer!

Change dependency on moment.

Currently an explicit version of the bower moment library is required.
As far as I can tell they use semantic versioning, so anything a simpler constraint would be: ^2.14.

Default format doesn't work with DATETIME database format

When using this extension with attribute of DATETIME format in database the conversion to DateTime object during validation failed.
Indeed as stated in https://secure.php.net/manual/en/datetime.createfromformat.php :

Unrecognized characters in the format string will cause the parsing to fail

To be more flexible regarding all kinds of date attribute (either DATE or DATETIME) the default format should not be Y-m-d but Y-m-d+ instead to ignore trailing data as stated :

+ If this format specifier is present, trailing data in the string will not cause an error, but a warning instead

Лишнее условие

Мне кажется вот здесь ошибка:

$this->addError($model, isset($compareAttribute) ? $compareAttribute : $compareAttribute, Yii::t('yii', 'Invalid compare value date format: {value}'), ['{value}' => $compareValue]);

isset($compareAttribute) ? $compareAttribute : $compareAttribute так или иначе подставит $compareAttribute.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.