示例#1
0
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (!$constraint instanceof Expression) {
         throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\Expression');
     }
     $variables = array();
     // Symfony 2.5+
     if ($this->context instanceof ExecutionContextInterface) {
         $variables['value'] = $value;
         $variables['this'] = $this->context->getObject();
     } elseif (null === $this->context->getPropertyName()) {
         $variables['value'] = $value;
         $variables['this'] = $value;
     } else {
         $root = $this->context->getRoot();
         $variables['value'] = $value;
         if (is_object($root)) {
             // Extract the object that the property belongs to from the object
             // graph
             $path = new PropertyPath($this->context->getPropertyPath());
             $parentPath = $path->getParent();
             $variables['this'] = $parentPath ? $this->getPropertyAccessor()->getValue($root, $parentPath) : $root;
         } else {
             $variables['this'] = null;
         }
     }
     if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
         if ($this->context instanceof ExecutionContextInterface) {
             $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->addViolation();
         } else {
             $this->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value))->addViolation();
         }
     }
 }
 /**
  * @param string|PropertyPathInterface $propertyPath  The property path to modify
  *
  * @throws NoSuchIndexException
  */
 public function removeParameter($propertyPath)
 {
     $propertyPathAccessor = PropertyAccess::createPropertyAccessor();
     if (!$propertyPath instanceof PropertyPathInterface) {
         $propertyPath = new PropertyPath($propertyPath);
     }
     if (1 === $propertyPath->getLength()) {
         $buffer =& $this->parameters;
         unset($buffer[$propertyPath->getElement(0)]);
     } else {
         $parentPropertyPath = $propertyPath->getParent();
         $buffer = $propertyPathAccessor->getValue($this->parameters, $parentPropertyPath);
         unset($buffer[$propertyPath->getElement($propertyPath->getLength() - 1)]);
         $propertyPathAccessor->setValue($this->parameters, $parentPropertyPath, $buffer);
     }
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (null === $value || '' === $value) {
         return;
     }
     $variables = array();
     if (null === $this->context->getPropertyName()) {
         $variables['this'] = $value;
     } else {
         // Extract the object that the property belongs to from the object
         // graph
         $path = new PropertyPath($this->context->getPropertyPath());
         $parentPath = $path->getParent();
         $root = $this->context->getRoot();
         $variables['value'] = $value;
         $variables['this'] = $parentPath ? $this->propertyAccessor->getValue($root, $parentPath) : $root;
     }
     if (!$this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
         $this->context->addViolation($constraint->message);
     }
 }
 /**
  * @param Request $request
  *
  * @return Response
  */
 public function setObjectFieldValueAction(Request $request)
 {
     $field = $request->get('field');
     $code = $request->get('code');
     $objectId = $request->get('objectId');
     $value = $request->get('value');
     $context = $request->get('context');
     $admin = $this->pool->getInstance($code);
     $admin->setRequest($request);
     // alter should be done by using a post method
     if (!$request->isXmlHttpRequest()) {
         return new JsonResponse(array('status' => 'KO', 'message' => 'Expected a XmlHttpRequest request header'));
     }
     if ($request->getMethod() != 'POST') {
         return new JsonResponse(array('status' => 'KO', 'message' => 'Expected a POST Request'));
     }
     $rootObject = $object = $admin->getObject($objectId);
     if (!$object) {
         return new JsonResponse(array('status' => 'KO', 'message' => 'Object does not exist'));
     }
     // check user permission
     if (false === $admin->isGranted('EDIT', $object)) {
         return new JsonResponse(array('status' => 'KO', 'message' => 'Invalid permissions'));
     }
     if ($context == 'list') {
         $fieldDescription = $admin->getListFieldDescription($field);
     } else {
         return new JsonResponse(array('status' => 'KO', 'message' => 'Invalid context'));
     }
     if (!$fieldDescription) {
         return new JsonResponse(array('status' => 'KO', 'message' => 'The field does not exist'));
     }
     if (!$fieldDescription->getOption('editable')) {
         return new JsonResponse(array('status' => 'KO', 'message' => 'The field cannot be edit, editable option must be set to true'));
     }
     $propertyPath = new PropertyPath($field);
     // If property path has more than 1 element, take the last object in order to validate it
     if ($propertyPath->getLength() > 1) {
         $object = $this->pool->getPropertyAccessor()->getValue($object, $propertyPath->getParent());
         $elements = $propertyPath->getElements();
         $field = end($elements);
         $propertyPath = new PropertyPath($field);
     }
     // Handle date type has setter expect a DateTime object
     if ('' !== $value && $fieldDescription->getType() == 'date') {
         $value = new \DateTime($value);
     }
     $this->pool->getPropertyAccessor()->setValue($object, $propertyPath, '' !== $value ? $value : null);
     $violations = $this->validator->validate($object);
     if (count($violations)) {
         $messages = array();
         foreach ($violations as $violation) {
             $messages[] = $violation->getMessage();
         }
         return new JsonResponse(array('status' => 'KO', 'message' => implode("\n", $messages)));
     }
     $admin->update($object);
     // render the widget
     // todo : fix this, the twig environment variable is not set inside the extension ...
     $extension = $this->twig->getExtension('sonata_admin');
     $extension->initRuntime($this->twig);
     $content = $extension->renderListElement($rootObject, $fieldDescription);
     return new JsonResponse(array('status' => 'OK', 'content' => $content));
 }
示例#5
0
 public function testGetParentWhenThereIsNoParent()
 {
     $propertyPath = new PropertyPath('path');
     $this->assertNull($propertyPath->getParent());
 }