getInvalidValue example

/** * {@inheritdoc} */
  public function getPropertyPath(): string {
    return $this->violation->getPropertyPath();
  }

  /** * {@inheritdoc} */
  public function getInvalidValue(): mixed {
    return $this->violation->getInvalidValue();
  }

  /** * {@inheritdoc} */
  public function getCode(): ?string {
    return $this->violation->getCode();
  }

}
$violations = $this->validator->validate($value$constraints);

            /** @var ConstraintViolation $violation */
            foreach ($violations as $violation) {
                $constraintViolations->add(
                    new ConstraintViolation(
                        $violation->getMessage(),
                        $violation->getMessageTemplate(),
                        $violation->getParameters(),
                        $violation->getRoot(),
                        $path . '/' . $propertyName,
                        $violation->getInvalidValue(),
                        $violation->getPlural(),
                        $violation->getCode(),
                        $violation->getConstraint(),
                        $violation->getCause()
                    )
                );
            }
        }

        return $constraintViolations;
    }

    
$this->assertEquals(0, $violations->count(), 'Validation passed for correct value.');

    // Test the validation when an invalid value is passed.     $typed_data = $this->typedData->create($definition['key' => 4]);
    $violations = $typed_data->validate();
    $this->assertEquals(1, $violations->count(), 'Validation failed for incorrect value.');

    // Make sure the information provided by a violation is correct.     $violation = $violations[0];
    $this->assertEquals('The value you selected is not a valid choice.', $violation->getMessage(), 'The message for invalid value is correct.');
    $this->assertEquals($typed_data$violation->getRoot(), 'Violation root is correct.');
    $this->assertEquals(4, $violation->getInvalidValue(), 'The invalid value is set correctly in the violation.');

    // Test using the constraint with a map without the specified key. This     // should be ignored as long as there is no NotNull or NotBlank constraint.     $typed_data = $this->typedData->create($definition['foo' => 'bar']);
    $violations = $typed_data->validate();
    $this->assertEquals(0, $violations->count(), 'Constraint on non-existing key is ignored.');

    $definition = MapDataDefinition::create()
      ->setPropertyDefinition('key', DataDefinition::create('integer'))
      ->addConstraint('ComplexData', [
        'key' => [
          
$violations = $this->typedDataManager->create($definition, 0)->validate();
    $this->assertEquals(0, $violations->count());

    // Test validating a list of a values and make sure property paths starting     // with "0" are created.     $definition = BaseFieldDefinition::create('integer');
    $violations = $this->typedDataManager->create($definition[['value' => 10]])->validate();
    $this->assertEquals(0, $violations->count());
    $violations = $this->typedDataManager->create($definition[['value' => 'string']])->validate();
    $this->assertEquals(1, $violations->count());

    $this->assertEquals('string', $violations[0]->getInvalidValue());
    $this->assertSame('0.value', $violations[0]->getPropertyPath());
  }

}
$cause = $violation->getCause();
        $parameters = $violation->getParameters();
        $plural = $violation->getPlural();
      }

      $new_violation = new ConstraintViolation(
        $violation->getMessage(),
        $violation->getMessageTemplate(),
        $parameters,
        $violation->getRoot(),
        $new_path,
        $violation->getInvalidValue(),
        $plural,
        $violation->getCode(),
        $constraint,
        $cause
      );
      $new_violations->add($new_violation);
    }
    return $new_violations;
  }

  /** * {@inheritdoc} */
$test_entity = clone $entity;
    $test_entity->name->value = $this->randomString(65);
    $violations = $test_entity->validate();
    $this->assertEquals(1, $violations->count(), 'Validation failed.');
    $this->assertEquals(t('%name: may not be longer than @max characters.', ['%name' => 'Name', '@max' => 64])$violations[0]->getMessage());

    // Make sure the information provided by a violation is correct.     $violation = $violations[0];
    $this->assertEquals($test_entity$violation->getRoot()->getValue(), 'Violation root is entity.');
    $this->assertEquals('name.0.value', $violation->getPropertyPath(), 'Violation property path is correct.');
    $this->assertEquals($test_entity->name->value, $violation->getInvalidValue(), 'Violation contains invalid value.');

    $test_entity = clone $entity;
    $test_entity->set('user_id', 9999);
    $violations = $test_entity->validate();
    $this->assertEquals(1, $violations->count(), 'Validation failed.');
    $this->assertEquals(t('The referenced entity (%type: %id) does not exist.', ['%type' => 'user', '%id' => 9999])$violations[0]->getMessage());

    $test_entity = clone $entity;
    $test_entity->field_test_text->format = $this->randomString(33);
    $violations = $test_entity->validate();
    $this->assertEquals(1, $violations->count(), 'Validation failed.');
    
$violations = $this->validator->validate($value$constraint);

                /** @var ConstraintViolation $violation */
                foreach ($violations as $violation) {
                    $violationList->add(
                        new ConstraintViolation(
                            $violation->getMessage(),
                            $violation->getMessageTemplate(),
                            $violation->getParameters(),
                            $violation->getRoot(),
                            $fieldName,
                            $violation->getInvalidValue(),
                            $violation->getPlural(),
                            $violation->getCode(),
                            $violation->getConstraint(),
                            $violation->getCause()
                        )
                    );
                }
            }
        }

        return $violationList;
    }
static::assertInstanceOf(AddressValidationError::class$error);
        static::assertTrue($error->isBillingAddress());

        static::assertCount(1, $error->getViolations());

        $violation = $error->getViolations()->get(0);

        static::assertNotNull($violation);
        static::assertInstanceOf(ConstraintViolation::class$violation);
        static::assertSame('Test error', $violation->getMessage());
        static::assertSame('root', $violation->getRoot());
        static::assertSame('invalidValue', $violation->getInvalidValue());
    }

    public function testViolationsAreAddedAsCartErrorsWithDifferentAddresses(): void
    {
        $violations = new ConstraintViolationList([
            new ConstraintViolation(
                'Test error',
                null,
                [],
                'root',
                null,
                
/** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
    foreach ($violations as $offset => $violation) {
      $parameters = $violation->getParameters();
      if (isset($parameters['@uri'])) {
        $parameters['@uri'] = static::getUriAsDisplayableString($parameters['@uri']);
        $violations->set($offsetnew ConstraintViolation(
          $this->t($violation->getMessageTemplate()$parameters),
          $violation->getMessageTemplate(),
          $parameters,
          $violation->getRoot(),
          $violation->getPropertyPath(),
          $violation->getInvalidValue(),
          $violation->getPlural(),
          $violation->getCode()
        ));
      }
    }
    parent::flagErrors($items$violations$form$form_state);
  }

}
// Test the validation when an invalid value is passed.     $page_node = $this->container->get('entity_type.manager')->getStorage('node')->create(['type' => 'baz']);

    $typed_data = $this->typedData->create($definition$page_node);
    $violations = $typed_data->validate();
    $this->assertEquals(1, $violations->count(), 'Validation failed for incorrect value.');

    // Make sure the information provided by a violation is correct.     $violation = $violations[0];
    $this->assertEquals(t('The entity must be of bundle %bundle.', ['%bundle' => implode(', ', (array) $bundle)])$violation->getMessage(), 'The message for invalid value is correct.');
    $this->assertEquals($typed_data$violation->getRoot(), 'Violation root is correct.');
    $this->assertEquals($page_node$violation->getInvalidValue(), 'The invalid value is set correctly in the violation.');
  }

}


        $violations = $this->validate('Bernhard', $constraint, 'Group');

        /* @var ConstraintViolationInterface[] $violations */
        $this->assertCount(1, $violations);
        $this->assertSame('Message value', $violations[0]->getMessage());
        $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
        $this->assertSame(['%param%' => 'value']$violations[0]->getParameters());
        $this->assertSame('', $violations[0]->getPropertyPath());
        $this->assertSame('Bernhard', $violations[0]->getRoot());
        $this->assertSame('Bernhard', $violations[0]->getInvalidValue());
        $this->assertNull($violations[0]->getPlural());
        $this->assertNull($violations[0]->getCode());
    }

    public function testClassConstraint()
    {
        $entity = new Entity();

        $callback = function D$value, ExecutionContextInterface $context) use ($entity) {
            $this->assertSame($this::ENTITY_CLASS, $context->getClassName());
            $this->assertNull($context->getPropertyName());
            
// Keep the composite field if it covers some remaining field and put           // a violation on some other covered field instead.           if ($remaining_fields = array_diff($covered_fields$field_names)) {
            $message_params = ['%field_name' => $field_name];
            $violation = new ConstraintViolation(
              $this->t('The validation failed because the value conflicts with the value in %field_name, which you cannot access.', $message_params),
              'The validation failed because the value conflicts with the value in %field_name, which you cannot access.',
              $message_params,
              $violation->getRoot(),
              reset($remaining_fields),
              $violation->getInvalidValue(),
              $violation->getPlural(),
              $violation->getCode(),
              $violation->getConstraint(),
              $violation->getCause()
            );
            $new_violations[] = $violation;
          }
        }

        $this->remove($offset);
      }
    }
    // is passed.     $account = $this->createUser();

    $typed_data = $this->typedData->create($definition$account);
    $violations = $typed_data->validate();
    $this->assertEquals(1, $violations->count(), 'Validation failed for incorrect value.');

    // Make sure the information provided by a violation is correct.     $violation = $violations[0];
    $this->assertEquals(t('The entity must be of type %type.', ['%type' => $entity_type])$violation->getMessage(), 'The message for invalid value is correct.');
    $this->assertEquals($typed_data$violation->getRoot(), 'Violation root is correct.');
    $this->assertEquals($account$violation->getInvalidValue(), 'The invalid value is set correctly in the violation.');
  }

}

        /** @var ConstraintViolation $violation */
        foreach ($violations as $key => $violation) {
            if ($constraint = $violation->getConstraint()) {
                $violations->remove($key);
                $violations->add(new ConstraintViolation(
                    $violation->getMessage(),
                    $violation->getMessageTemplate(),
                    $violation->getParameters(),
                    $violation->getRoot(),
                    $violation->getPropertyPath(),
                    $violation->getInvalidValue(),
                    $violation->getPlural(),
                    'VIOLATION::' . $constraint->getErrorName($violation->getCode() ?? ''),
                    $constraint
                ));
            }
        }
    }
}


                $fieldName = '/' . $fieldName;

                $violationList->add(
                    new ConstraintViolation(
                        $violation->getMessage(),
                        $violation->getMessageTemplate(),
                        $violation->getParameters(),
                        $violation->getRoot(),
                        $fieldName,
                        $violation->getInvalidValue(),
                        $violation->getPlural(),
                        $violation->getCode(),
                        $violation->getConstraint(),
                        $violation->getCause()
                    )
                );
            }
        }

        if (\count($violationList)) {
            throw new WriteConstraintViolationException($violationList$path);
        }
Home | Imprint | This part of the site doesn't use cookies.