/** * Returns a form builder for a property of a class. * * If any of the 'max_length', 'required' and type options can be guessed, * and are not provided in the options argument, the guessed value is used. * * @param string $class The fully qualified class name * @param string $property The name of the property to guess for * @param mixed $data The initial data * @param array $options The options for the builder * @param FormBuilder $parent The parent builder * * @return FormBuilder The form builder named after the property * * @throws FormException if any given option is not applicable to the form type */ public function createBuilderForProperty($class, $property, $data = null, array $options = array(), FormBuilder $parent = null) { if (!$this->guesser) { $this->loadGuesser(); } $typeGuess = $this->guesser->guessType($class, $property); $maxLengthGuess = $this->guesser->guessMaxLength($class, $property); $minLengthGuess = $this->guesser->guessMinLength($class, $property); $requiredGuess = $this->guesser->guessRequired($class, $property); $type = $typeGuess ? $typeGuess->getType() : 'text'; if ($maxLengthGuess) { $options = array_merge(array('max_length' => $maxLengthGuess->getValue()), $options); } if ($minLengthGuess) { if ($maxLengthGuess) { $options = array_merge(array('pattern' => '.{' . $minLengthGuess->getValue() . ',' . $maxLengthGuess->getValue() . '}'), $options); } else { $options = array_merge(array('pattern' => '.{' . $minLengthGuess->getValue() . ',}'), $options); } } if ($requiredGuess) { $options = array_merge(array('required' => $requiredGuess->getValue()), $options); } // user options may override guessed options if ($typeGuess) { $options = array_merge($typeGuess->getOptions(), $options); } return $this->createNamedBuilder($type, $property, $data, $options, $parent); }
/** * Returns a form builder for a property of a class. * * If any of the 'max_length', 'required', 'pattern' and type options can be guessed, * and are not provided in the options argument, the guessed value is used. * * @param string $class The fully qualified class name * @param string $property The name of the property to guess for * @param mixed $data The initial data * @param array $options The options for the builder * @param FormBuilder $parent The parent builder * * @return FormBuilder The form builder named after the property * * @throws FormException if any given option is not applicable to the form type */ public function createBuilderForProperty($class, $property, $data = null, array $options = array(), FormBuilder $parent = null) { if (!$this->guesser) { $this->loadGuesser(); } $typeGuess = $this->guesser->guessType($class, $property); $maxLengthGuess = $this->guesser->guessMaxLength($class, $property); // Keep $minLengthGuess for BC until Symfony 2.3 $minLengthGuess = $this->guesser->guessMinLength($class, $property); $requiredGuess = $this->guesser->guessRequired($class, $property); $patternGuess = $this->guesser->guessPattern($class, $property); $type = $typeGuess ? $typeGuess->getType() : 'text'; $maxLength = $maxLengthGuess ? $maxLengthGuess->getValue() : null; $minLength = $minLengthGuess ? $minLengthGuess->getValue() : null; $pattern = $patternGuess ? $patternGuess->getValue() : null; // overrides $minLength, if set if (null !== $pattern) { $options = array_merge(array('pattern' => $pattern), $options); } if (null !== $maxLength) { $options = array_merge(array('max_length' => $maxLength), $options); } if (null !== $minLength && $minLength > 0) { $options = array_merge(array('pattern' => '.{' . $minLength . ',' . $maxLength . '}'), $options); } if ($requiredGuess) { $options = array_merge(array('required' => $requiredGuess->getValue()), $options); } // user options may override guessed options if ($typeGuess) { $options = array_merge($typeGuess->getOptions(), $options); } return $this->createNamedBuilder($type, $property, $data, $options, $parent); }