public function buildView(FormView $view, FormInterface $form, array $options)
 {
     // Actual form root.
     if ($form->isRoot() && $view->parent === null) {
         if (!$options['jquery_validation']) {
             return;
         }
         $validation_groups = FormHelper::getValidationGroups($form);
         $contextBuilder = new FormRuleContextBuilder();
         $view->vars['rule_builder'] = $contextBuilder;
         $view->vars['rule_builder_children'] = array();
         if ($validation_groups === null) {
             $validation_groups = array(Constraint::DEFAULT_GROUP);
         }
     } else {
         $rootView = FormHelper::getViewRoot($view);
         if (!$this->hasRuleBuilderContext($rootView)) {
             return;
         }
         $contextBuilder = $this->getRuleBuilder($rootView);
         $validation_groups = FormHelper::getValidationGroups($form);
     }
     if ($validation_groups !== null) {
         $contextBuilder->addGroup($view, $validation_groups);
     }
 }
Esempio n. 2
0
    public function validate(FormInterface $form)
    {
        if (!$form->isSynchronized()) {
            $form->addError(new FormError('The value is invalid'));
        }

        if (count($form->getExtraData()) > 0) {
            $form->addError(new FormError('This form should not contain extra fields'));
        }

        if ($form->isRoot() && isset($_SERVER['CONTENT_LENGTH'])) {
            $length = (int) $_SERVER['CONTENT_LENGTH'];
            $max = trim(ini_get('post_max_size'));

            switch (strtolower(substr($max, -1))) {
                // The 'G' modifier is available since PHP 5.1.0
                case 'g':
                    $max *= 1024;
                case 'm':
                    $max *= 1024;
                case 'k':
                    $max *= 1024;
            }

            if ($length > $max) {
                $form->addError(new FormError('The uploaded file was too large. Please try to upload a smaller file'));
            }
        }
    }
Esempio n. 3
0
 /**
  * Validates the form and its domain object.
  *
  * @param FormInterface $form A FormInterface instance
  */
 public function validate(FormInterface $form)
 {
     if ($form->isRoot()) {
         $mapping = array();
         $forms = array();
         $this->buildFormPathMapping($form, $mapping);
         $this->buildDataPathMapping($form, $mapping);
         $this->buildNamePathMapping($form, $forms);
         $this->resolveMappingPlaceholders($mapping, $forms);
         // Validate the form in group "Default"
         // Validation of the data in the custom group is done by validateData(),
         // which is constrained by the Execute constraint
         if ($form->hasAttribute('validation_constraint')) {
             $violations = $this->validator->validateValue($form->getData(), $form->getAttribute('validation_constraint'), self::getFormValidationGroups($form));
         } else {
             $violations = $this->validator->validate($form);
         }
         if ($violations) {
             foreach ($violations as $violation) {
                 $propertyPath = $violation->getPropertyPath();
                 $template = $violation->getMessageTemplate();
                 $parameters = $violation->getMessageParameters();
                 $error = new FormError($template, $parameters);
                 foreach ($mapping as $mappedPath => $child) {
                     if (preg_match($mappedPath, $propertyPath)) {
                         $child->addError($error);
                         continue 2;
                     }
                 }
                 $form->addError($error);
             }
         }
     }
 }
 /**
  * Navigates to the Root form to define if cascading should be done.
  *
  * @param FormInterface $form
  * @return boolean
  */
 public function getRootFormCascadeOption(FormInterface $form)
 {
     if (!$form->isRoot()) {
         return $this->getRootFormCascadeOption($form->getParent());
     }
     return $form->getConfig()->getOption('cascade_filter', false);
 }
 public function interactWith(FormInterface $form, HelperSet $helperSet, InputInterface $input, OutputInterface $output)
 {
     if (!$form->isRoot()) {
         throw new CanNotInteractWithForm('This interactor only works with root forms');
     }
     if ($input->isInteractive()) {
         throw new CanNotInteractWithForm('This interactor only works with non-interactive input');
     }
     /*
      * We need to adjust the input values for repeated types by copying the provided value to both of the repeated
      * fields. We only loop through the top-level fields, since there are no command options for deeper lying fields
      * anyway.
      *
      * The fix was provided by @craigh
      *
      * P.S. If we need to add another fix like this, we should move this out to dedicated "input fixer" classes.
      */
     foreach ($form->all() as $child) {
         $config = $child->getConfig();
         $name = $child->getName();
         if ($config->getType()->getInnerType() instanceof RepeatedType && $input->hasOption($name)) {
             $input->setOption($name, [$config->getOption('first_name') => $input->getOption($name), $config->getOption('second_name') => $input->getOption($name)]);
         }
     }
     // use the original input as the submitted data
     return $input;
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function finishView(FormView $view, FormInterface $form, array $options)
 {
     if ($form->isRoot()) {
         $view->vars['help'] = 'form.required_fields.help';
         $view->children['submit']->vars['label'] = 'form.submit.label';
         $view->children['submit']->vars['render_as_action'] = true;
     }
 }
 /**
  * @param FormInterface $form
  * @return array
  */
 protected function getFormErrorMessages(FormInterface $form)
 {
     $errors = [];
     foreach ($form->getErrors() as $error) {
         if ($form->isRoot()) {
             $errors[] = $this->toErrorArray($error);
         } else {
             $errors[] = $this->toErrorArray($error, $form);
         }
     }
     return $errors;
 }
Esempio n. 8
0
 /**
  * Adds a CSRF field to the root form view.
  *
  * @param FormView      $view The form view
  * @param FormInterface $form The form
  */
 public function buildView(FormView $view, FormInterface $form)
 {
     if ($form->isRoot() && $form->hasChildren() && $form->hasAttribute('csrf_field_name')) {
         $name = $form->getAttribute('csrf_field_name');
         $csrfProvider = $form->getAttribute('csrf_provider');
         $intention = $form->getAttribute('csrf_intention');
         $factory = $form->getAttribute('csrf_factory');
         $data = $csrfProvider->generateCsrfToken($intention);
         $csrfForm = $factory->createNamed('hidden', $name, $data, array('property_path' => false));
         $view->addChild($csrfForm->createView($view));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function buildView(FormView $view, FormInterface $form, array $options)
 {
     if (!$form->isRoot() && isset($options['rules'])) {
         $rules = $options['rules'];
         $innerType = $form->getConfig()->getType()->getInnerType();
         if (($innerType instanceof NumberType || $innerType instanceof IntegerType) && !in_array('numeric', $rules)) {
             $rules[] = 'numeric';
         }
         $ruleParser = new RulesParser($form, $view, $rules);
         $attr = $ruleParser->getAttributes();
         // Set form attributes based on rules
         $view->vars['required'] = in_array('required', $attr);
         $view->vars['attr'] += $attr;
     }
 }
 /**
  * Gets all the errors in the current $field and adds them to $_errors, to
  * get all errors in the form and its children recursively.
  * Also adds display name to error-message.
  *
  * @param  FormInterface $field Field to get Errors from
  */
 protected function _getErrors(FormInterface $field)
 {
     if ($field->getConfig()->getOption('label')) {
         $label = $this->_translator->trans($field->getConfig()->getOption('label'));
     } else {
         $label = ucfirst(trim(strtolower(preg_replace(array('/([A-Z])/', '/[_\\s]+/'), array('_$1', ' '), $field->getName()))));
     }
     $fieldErrors = array();
     foreach ($field->getErrors() as $error) {
         // don't show label if error is on the whole form
         $fieldErrors[] = $field->isRoot() ? $error->getMessage() : $label . ': ' . $error->getMessage();
     }
     $this->_errors[] = $fieldErrors;
     foreach ($field->all() as $child) {
         $this->_getErrors($child);
     }
 }
Esempio n. 11
0
 protected function getErrorMessages(FormInterface $form)
 {
     $errors = array();
     foreach ($form->getErrors() as $key => $error) {
         if ($form->isRoot()) {
             $errors[] = '#: ' . $error->getMessage();
         } else {
             $errors[] = $error->getMessage() . '. ' . implode(', ', $error->getMessageParameters());
         }
     }
     foreach ($form->all() as $child) {
         if (!$child->isValid()) {
             $errorMessages = $this->getErrorMessages($child);
             if (is_array($errorMessages)) {
                 $errors = array_merge($errors, $errorMessages);
             } else {
                 $errors[] = $child->getName() . ': ' . $errorMessages;
             }
         }
     }
     return $errors;
 }
Esempio n. 12
0
 /**
  * Returns whether the data of a form may be walked.
  *
  * @param  FormInterface $form The form to test.
  *
  * @return Boolean Whether the graph walker may walk the data.
  */
 private static function allowDataWalking(FormInterface $form)
 {
     $data = $form->getData();
     // Scalar values cannot have mapped constraints
     if (!is_object($data) && !is_array($data)) {
         return false;
     }
     // Root forms are always validated
     if ($form->isRoot()) {
         return true;
     }
     // Non-root forms are validated if validation cascading
     // is enabled in all ancestor forms
     while (null !== ($form = $form->getParent())) {
         if (!$form->getConfig()->getOption('cascade_validation')) {
             return false;
         }
     }
     return true;
 }
 private function requiresValidConstraint(FormInterface $form)
 {
     $formConfig = $form->getConfig();
     return !$form->isRoot() && $formConfig->getCompound() && $formConfig->getMapped() && $formConfig->getDataClass() !== null;
 }
 /**
  * Gets the form data to which a property path applies
  *
  * @param FormInterface $form
  * @return object|null
  */
 private function resolveDataSource(FormInterface $form)
 {
     if ($form->isRoot()) {
         // Nothing to do if root
         $dataForm = $form->getData();
     } else {
         $dataForm = $form;
         while ($dataForm->getConfig()->getDataClass() === null) {
             $dataForm = $form->getParent();
         }
     }
     $data = $dataForm->getData();
     return array($data, $data === null ? $dataForm->getConfig()->getDataClass() : get_class($data));
 }