Andy Holmes
Andy Holmes

Reputation: 8047

Throw an error inside Symfony2 entity if value doesn't match if statement

I have the following code inside my Clutch.php entity.

/**
 * Set incubationTemp
 *
 * @param integer $incubationTemp
 * @return Clutch
 */
public function setIncubationTemp($incubationTemp)
{
    $this->incubationTemp = $incubationTemp;

    if($incubationTemp > 77.5 && $incubationTemp < 82.5){
        $estimatedHatchStart = new \DateTime($this->getLaidDate()->format('Y-m-d') . '+60days');
        $estimatedHatchEnd = new \DateTime($this->getLaidDate()->format('Y-m-d') . '+70days');
    } 
    else if($incubationTemp > 82.5 && $incubationTemp < 88.5) {
        $estimatedHatchStart = new \DateTime($this->getLaidDate()->format('Y-m-d') . '+45days');
        $estimatedHatchEnd = new \DateTime($this->getLaidDate()->format('Y-m-d') . '+55days');
    }
    else if($incubationTemp > 88.5 && $incubationTemp < 92.5) {
        $estimatedHatchStart = new \DateTime($this->getLaidDate()->format('Y-m-d') . '+35days');
        $estimatedHatchEnd = new \DateTime($this->getLaidDate()->format('Y-m-d') . '+45days');
    } else {
        // Throw an error here
    }

    $this->setEstimatedHatchStart($estimatedHatchStart);
    $this->setEstimatedHatchEnd($estimatedHatchEnd);

    return $this;
}

Which works fine if the passed value is between the values you can see in my if statement. What i would like to do is force an error to show if the value of $incubationTemp is outside the values being checked. What is the best way to achieve this?

EDIT:

As per Peter's answer. This has worked perfectly:

use Symfony\Component\Validator\Constraints as Assert;

// ...

/**
 * @var integer
 *
 * @ORM\Column(name="incubation_temp", type="integer")
 * @Assert\Range(
 *      min = 78,
 *      max = 92,
 *      minMessage = "The fahrenheit value you have entered is not a suggested value for incubating leopard geckos. Please make sure the value is at least {{ limit }}°f.",
 *      maxMessage = "The fahrenheit value you have entered is not a suggested value for incubating leopard geckos. Please make sure the value is lower than {{ limit }}°f."
 * )
 */
private $incubationTemp;

Upvotes: 1

Views: 847

Answers (1)

Peter Bailey
Peter Bailey

Reputation: 105868

There is not likely to be a "best" way but exceptions are an option:

class InvalidIncubationTemperatureException extends \DomainException {}

And then

else {
    throw new InvalidIncubationTemperatureException(
        'Whatever message you want'
    )
}

When you say "show" an error that could mean many things, but let's assume you're in a controller context

try {
    $clutch = new Clutch();
    $clutch->setIncubationTemp(0);
}
catch (InvalidIncubationTemperatureException $e)
{
    $this->addFlash('clutch_errors', $e->getMessage())
}

Then in your twig

{% set errors = app.session.flashbag.get('clutch_errors') %}
{% if errors|length %}
  {% for message in errors %}
    {{ message}}
  {% endfor %}
{% endif %}

Or whatever mechanism you'd like.

A better way to handle this, however, might be with validation

Upvotes: 1

Related Questions