/** * Validates the object and all objects related to this table. * * @see getValidationFailures() * @param ValidatorInterface|null $validator A Validator class instance * @return boolean Whether all objects pass validation. */ public function validate(ValidatorInterface $validator = null) { if (null === $validator) { $validator = new RecursiveValidator(new ExecutionContextFactory(new IdentityTranslator()), new LazyLoadingMetadataFactory(new StaticMethodLoader()), new ConstraintValidatorFactory()); } $failureMap = new ConstraintViolationList(); if (!$this->alreadyInValidation) { $this->alreadyInValidation = true; $retval = null; // We call the validate method on the following object(s) if they // were passed to this object by their corresponding set // method. This object relates to these object(s) by a // foreign key reference. // If validate() method exists, the validate-behavior is configured for related object if (method_exists($this->aUser, 'validate')) { if (!$this->aUser->validate($validator)) { $failureMap->addAll($this->aUser->getValidationFailures()); } } // If validate() method exists, the validate-behavior is configured for related object if (method_exists($this->aPack, 'validate')) { if (!$this->aPack->validate($validator)) { $failureMap->addAll($this->aPack->getValidationFailures()); } } $retval = $validator->validate($this); if (count($retval) > 0) { $failureMap->addAll($retval); } $this->alreadyInValidation = false; } $this->validationFailures = $failureMap; return (bool) (!(count($this->validationFailures) > 0)); }
protected function validateOne() { setContentType("json"); $pack = new Pack(); $given = array_keys($_POST); $response["error"] = null; if (count($given) == 1) { if ($given[0] == "name") { $p = PackQuery::create()->filterByOwner($this->data["loggedUser"])->filterByName($_POST["name"])->findOne(); if ($p) { $response["error"] = array("name" => "name", "message" => "You can not have two packs with the same name."); $this->viewString(json_encode($response)); return; } $pack->setName($_POST["name"]); } else { if ($given[0] == "description") { $pack->setDescription($_POST["description"]); } else { setHTTPStatusCode("400"); return; } } if (!$pack->validate()) { foreach ($pack->getValidationFailures() as $failure) { if ($given[0] == $failure->getPropertyPath()) { $response["error"] = array("name" => $failure->getPropertyPath(), "message" => $failure->getMessage()); } } } $this->viewString(json_encode($response)); } else { setHTTPStatusCode("400"); } }