Skip to content

Step 7 - Add basic validation

Tip

You can find all files used and modified in this step on GitHub.

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
<?php

use Symfony\Component\Validator\Constraints as Assert;

final class Value implements ValueInterface

{  
   /**
     * @var float|null
     *
     * @Assert\NotBlank()
     */
    private $x;

    /**
     * @var float|null
     *
     * @Assert\NotBlank()
     */
    private $y;

 // ...   
}

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

Point 2D validation