Skip to content

Step 7 - Add basic validation

To provide basic validation that ensures both coordinates are provided, add assertions to the src/FieldType/Point2D/Value.php:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
declare(strict_types=1);

namespace App\FieldType\Point2D;

use Ibexa\Contracts\Core\FieldType\Value as ValueInterface;
use Symfony\Component\Validator\Constraints as Assert;

final class Value implements ValueInterface
{
    public function __construct(
        #[Assert\NotBlank]
        private ?float $x = null,
        #[Assert\NotBlank]
        private ?float $y = null
    ) {
    }

    public function getX(): ?float
    {
        return $this->x;
    }

    public function setX(?float $x): void
    {
        $this->x = $x;
    }

    public function getY(): ?float
    {
        return $this->y;
    }

    public function setY(?float $y): void
    {
        $this->y = $y;
    }

    public function __toString(): string
    {
        return "({$this->x}, {$this->y})";
    }
}

As a result, if a user tries to publish the Point 2D with one value, they receive an error message.

Point 2D validation