0

I have a form, and a user have to enter a date of beginning and a date of ending.

I'm using the compare attribute inside the rules method because the user can't enter a date of ending which is before the date of beginning.

Here's my rules method inside the form's model :

['dateFinTR', 'date', 'format' => 'php: d/m/Y'],
['dateFinTR', 'compare', 'compareAttribute' => 'dateDebutTR', 'operator' => '>=', 'skipOnEmpty' => true,]

The problem is if a user enter a date of beginning as 2016-05-18 and a date of ending of 2016-04-17, an error message is shown and the user can't submit the form. But if the user enter a date of beginning of 2016-05-18 and a date of ending of 2016-04-23 there is no error and the user can't submit the form. In fact only the day is compared with this rule.

EDIT According to this link: Yii2 Date compare validation, I've created a custom validation method but I'm using european format so I think the strtotime method is not wirking good. Here's my validateDates method :

public function validateDates()
{
    if (strtotime($this->dateFinTR) <= strtotime($this->dateDebutTR))
    {
        $this->addError('dateFinTR', 'Veuillez saisir une date de fin se trouvant avant la date de début');
    }
}

And here's the call of this method :

['dateFinTR', 'validateDates', 'skipOnEmpty' => true],
Community
  • 1
  • 1
Samaël Villette
  • 318
  • 3
  • 25

2 Answers2

4

It is a common date formatting/converting problem, not really specific to Yii.

You could simply try to use DateTime::createFromFormat(), e.g. :

public function validateDates()
{
    $date1 = \DateTime::createFromFormat('d/m/Y', $this->dateDebutTR);
    $date2 = \DateTime::createFromFormat('d/m/Y', $this->dateFinTR);
    if ($date2<=$date1) {
        $this->addError('dateFinTR', 'error message');
    }
}

But it should be better, in most cases, to store dates in your database with the right format (DATE with MySQL for example), and only convert them for display.

soju
  • 25,111
  • 3
  • 68
  • 70
0

Consider to fix your dates check to the following:

if (strtotime($this->dateFinTR) > strtotime($this->dateDebutTR))

Event end date should be bigger then start date.

Taras
  • 572
  • 3
  • 15
  • Yes but if the condition is ok an error is shown so in the condition we want to check if the event start date is bigger than the event end date ^^ – Samaël Villette May 18 '16 at 09:37
  • Maybe you should add if ($this->hasErrors()) { return; } before $this->addError call? – Taras May 18 '16 at 10:40