Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

assert symfony

<?php

require_once '../vendor/autoload.php';

use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentValidatorValidation;

$validator = Validation::createValidator();

$input = [
    //'name' => [],
];

$notNestedConstraint = new AssertCollection(
    [
        'first_name' => new AssertRequired(),
        'last_name' => new AssertRequired(),
    ],
);
$nestedConstraint = new AssertCollection(
    [
        'name' => $notNestedConstraint,
    ]
);

$items = [];
$constraint = (array_key_exists('name', $input)) ? $nestedConstraint: $notNestedConstraint

$violations = $validator->validate($input, $constraint);
foreach ($violations as $violation) {
    $items[] = [
        'path' => $violation->getPropertyPath(),
        'message' => $violation->getMessage(),
    ];
}

var_dump($items);
 
PREVIOUS NEXT
Tagged: #assert #symfony
ADD COMMENT
Topic
Name
3+3 =