I have a Dns
entity who have a content
field with a NotBlank
constraint
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=true)
* @Assert\Regex("/^(([[:alnum:]-_]+(\.[[:alnum:]-_]+)*)|(\*))$/u")
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="content", type="string", length=64000, nullable=true)
* @Assert\NotBlank
* @Assert\Length(max="64000")
*/
private $content;
I have made a working PATCH
action with FosRestBundle
/**
* @ParamConverter("updatedRecord", converter="fos_rest.request_body")
*/
public function patchAction(PowerDNSDomain $domain, PowerDNSRecord $record, PowerDNSRecord $updatedRecord, ConstraintViolationListInterface $validationErrors)
{
if ($validationErrors->count() > 0) {
return $this->handleBodyValidationErrorsView($validationErrors);
}
$record->setName($updatedRecord->getName())
->setContent($updatedRecord->getContent())
->setTtl($updatedRecord->getTtl())
->setPrio($updatedRecord->getPrio());
$this->get('manager.dns')->saveRecord($record);
return $this->view($record);
}
When I'm trying to update a Dns
entry without changing the content
field i get the following error because of my NotBlank
constraint.
{ "error": "validation_failed", "error_description": "Data validation failed. Please check errors below.", "validation_errors": { "content": [ "This value cannot be null." ] } }
This is doing the same with NotNull
constraint.
I'm trying to PATCH a Dns
entry without changing the content
field.
Is this possible to keep my constraint inside the entity or I must use another way ?
You can deal with validation groups to keep your constraint on INSERT
and skip it on UPDATE
.
Example:
/**
* @var string
*
* @ORM\Column(name="content", type="string", length=64000, nullable=true)
* @Assert\NotBlank(groups={"new"})
* @Assert\Length(max="64000")
*/
private $content;
Then, if your method is creating a new entry, set the new
validation group in the @ParamConverter
annotation:
/**
* @ParamConverter("updatedRecord", converter="fos_rest.request_body", options={"validator"={"groups"={"new"}}})
*/
See the Request body listener for more.