Comparison of dates

Asked

Viewed 98 times

0

I want the message to appear when the user enters no date or the initial date is longer than the final date. The code I made was this:

/**
 * @Assert\True(message = "Erro! Verifique as horas. Data inical não pode ser maior que a data final.")
 */
public function isHoraFim() 
{
    if ($this->horainicial > $this->horafim) 
    {
        return false;
    } 
    else 
    {
        return true;
    }

    if ($this->horainicial || $this->horafim) 
    {
        return false;
    }    

    return $this->horainicio < $this->horafim;
}

But the error message always appears, except when the user inserts no date.

  • $this->horafim would be $this->horafinal?

  • it should be the end I’ve already changed, but it’s still not working well

  • What type of properties(variables)? It has how to show their definition, how you are treating the data after the user enters the hours?

2 answers

1


I leave my question here to those who have the same question as me. What happened to me was that I had the same name as my variable.

 /**
 * @Assert\True(message = "Erro! Verifique as horas. Data inical não pode ser maior que a data final.")
 */
public function isHorasCheck() {
        return ($this->horainicio < $this->horafim);
}

Which I added, but which wasn’t in the question, I thought it would be better this way.

 /**
 * @Assert\True(message = "Erro! Verifique as horas. Data inical não pode ser maior que a data final.")
 */
public function isHorasCheck() {

    if ($this->horafim === null){
        return true;
    }
    if ($this->horainicio === null){
        return false;
    }
    else{
        return ($this->horainicio < $this->horafim);
    }

}

Both way they work now is just seeing what’s best for you.

  • If your solution served, be sure to validate your own answer :)

  • I know, but it’s only been two days since the program lets me validate.

0

If you need to return true or false, a simple line solves:

/**
 * @Assert\False(message = "Erro! Verifique as horas. Data inicial não pode ser maior que a data final.")
 */
public function isHoraFim() 
{
    return (!$this->horainicial || !$this->horafim || $this->horainicial > $this->horafim);
}

Or:

/**
 * @Assert\True(message = "Erro! Verifique as horas. Data inicial não pode ser maior que a data final.")
 */
public function isHoraFim() 
{
    return ($this->horainicial && $this->horafim && $this->horainicial < $this->horafim);
}
  • is not doing the same thing as mine.

  • Inverti os asserts. Actually the message will be sent if the method returns false in the Assert\True, or vice versa.

  • Still nothing, what I intended was that when the user presses a save button and has not filled in the data of the time that appears the error message or initial time is greater than the final time and that no message appears when it is right.

  • The other error messages you set with Assert appear normally in form validations?

  • I don’t think so..

  • Assertions define conditions that must be met when the user posts a form and you validate with $form->isValid(). If the form is invalid you display the errors generated for the user.

  • But I have to do this here at Entity Intervencao.php or Intervecaocontroller or Intervencao.new?

  • In the IntervencaoController. It is usually in the controller where resides all the part of user interaction - as data input, validation, response with error messages or success.

  • But Function isHoraFim have to declare in the Intervention.Php only the rest is what I have to do in Intervencaocontroller?

  • That’s right! Validations are in models.

  • And in my method I only have to put $form->isValid();??

  • But it’s not $form->isValid(); it’s not $form->isHoraFim(); I made it $form= $this->isHoraFim();.

  • We will continue the discussion on http://chat.stackexchange.com/rooms/21040/discussion-between-catarina-silvestre-and-rodrigo-rigotti

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.