本文整理汇总了PHP中Symfony\Component\Form\FormConfigInterface::getData方法的典型用法代码示例。如果您正苦于以下问题:PHP FormConfigInterface::getData方法的具体用法?PHP FormConfigInterface::getData怎么用?PHP FormConfigInterface::getData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Form\FormConfigInterface
的用法示例。
在下文中一共展示了FormConfigInterface::getData方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setData
/**
* Updates the form with default data.
*
* @param array $modelData The data formatted as expected for the underlying object
*
* @return Form The current form
*/
public function setData($modelData)
{
if ($this->bound) {
throw new AlreadyBoundException('You cannot change the data of a bound form');
}
// Don't allow modifications of the configured data if the data is locked
if ($this->config->getDataLocked() && $modelData !== $this->config->getData()) {
return $this;
}
if (is_object($modelData) && !$this->config->getByReference()) {
$modelData = clone $modelData;
}
// Hook to change content of the data
$event = new FormEvent($this, $modelData);
$this->config->getEventDispatcher()->dispatch(FormEvents::PRE_SET_DATA, $event);
// BC until 2.3
$this->config->getEventDispatcher()->dispatch(FormEvents::SET_DATA, $event);
$modelData = $event->getData();
// Treat data as strings unless a value transformer exists
if (!$this->config->getViewTransformers() && !$this->config->getModelTransformers() && is_scalar($modelData)) {
$modelData = (string) $modelData;
}
// Synchronize representations - must not change the content!
$normData = $this->modelToNorm($modelData);
$viewData = $this->normToView($normData);
// Validate if view data matches data class (unless empty)
if (!FormUtil::isEmpty($viewData)) {
$dataClass = $this->config->getDataClass();
$actualType = is_object($viewData) ? 'an instance of class ' . get_class($viewData) : ' a(n) ' . gettype($viewData);
if (null === $dataClass && is_object($viewData) && !$viewData instanceof \ArrayAccess) {
$expectedType = 'scalar, array or an instance of \\ArrayAccess';
throw new FormException('The form\'s view data is expected to be of type ' . $expectedType . ', ' . 'but is ' . $actualType . '. You ' . 'can avoid this error by setting the "data_class" option to ' . '"' . get_class($viewData) . '" or by adding a view transformer ' . 'that transforms ' . $actualType . ' to ' . $expectedType . '.');
}
if (null !== $dataClass && !$viewData instanceof $dataClass) {
throw new FormException('The form\'s view data is expected to be an instance of class ' . $dataClass . ', but is ' . $actualType . '. You can avoid this error ' . 'by setting the "data_class" option to null or by adding a view ' . 'transformer that transforms ' . $actualType . ' to an instance of ' . $dataClass . '.');
}
}
$this->modelData = $modelData;
$this->normData = $normData;
$this->viewData = $viewData;
$this->synchronized = true;
if ($this->config->getCompound()) {
// Update child forms from the data
$this->config->getDataMapper()->mapDataToForms($viewData, $this->children);
}
$event = new FormEvent($this, $modelData);
$this->config->getEventDispatcher()->dispatch(FormEvents::POST_SET_DATA, $event);
return $this;
}
示例2: submit
/**
* {@inheritdoc}
*/
public function submit($submittedData, $clearMissing = true)
{
if ($this->submitted) {
throw new AlreadySubmittedException('A form can only be submitted once');
}
// Initialize errors in the very beginning so that we don't lose any
// errors added during listeners
$this->errors = array();
// Obviously, a disabled form should not change its data upon submission.
if ($this->isDisabled()) {
$this->submitted = true;
return $this;
}
// The data must be initialized if it was not initialized yet.
// This is necessary to guarantee that the *_SET_DATA listeners
// are always invoked before submit() takes place.
if (!$this->defaultDataSet) {
$this->setData($this->config->getData());
}
// Treat false as NULL to support binding false to checkboxes.
// Don't convert NULL to a string here in order to determine later
// whether an empty value has been submitted or whether no value has
// been submitted at all. This is important for processing checkboxes
// and radio buttons with empty values.
if (false === $submittedData) {
$submittedData = null;
} elseif (is_scalar($submittedData)) {
$submittedData = (string) $submittedData;
}
$dispatcher = $this->config->getEventDispatcher();
$modelData = null;
$normData = null;
$viewData = null;
try {
// Hook to change content of the data submitted by the browser
if ($dispatcher->hasListeners(FormEvents::PRE_SUBMIT)) {
$event = new FormEvent($this, $submittedData);
$dispatcher->dispatch(FormEvents::PRE_SUBMIT, $event);
$submittedData = $event->getData();
}
// Check whether the form is compound.
// This check is preferable over checking the number of children,
// since forms without children may also be compound.
// (think of empty collection forms)
if ($this->config->getCompound()) {
if (null === $submittedData) {
$submittedData = array();
}
if (!is_array($submittedData)) {
throw new TransformationFailedException('Compound forms expect an array or NULL on submission.');
}
foreach ($this->children as $name => $child) {
$isSubmitted = array_key_exists($name, $submittedData);
if ($isSubmitted || $clearMissing) {
$child->submit($isSubmitted ? $submittedData[$name] : null, $clearMissing);
unset($submittedData[$name]);
if (null !== $this->clickedButton) {
continue;
}
if ($child instanceof ClickableInterface && $child->isClicked()) {
$this->clickedButton = $child;
continue;
}
if (method_exists($child, 'getClickedButton') && null !== $child->getClickedButton()) {
$this->clickedButton = $child->getClickedButton();
}
}
}
$this->extraData = $submittedData;
}
// Forms that inherit their parents' data also are not processed,
// because then it would be too difficult to merge the changes in
// the child and the parent form. Instead, the parent form also takes
// changes in the grandchildren (i.e. children of the form that inherits
// its parent's data) into account.
// (see InheritDataAwareIterator below)
if (!$this->config->getInheritData()) {
// If the form is compound, the default data in view format
// is reused. The data of the children is merged into this
// default data using the data mapper.
// If the form is not compound, the submitted data is also the data in view format.
$viewData = $this->config->getCompound() ? $this->viewData : $submittedData;
if (FormUtil::isEmpty($viewData)) {
$emptyData = $this->config->getEmptyData();
if ($emptyData instanceof \Closure) {
/* @var \Closure $emptyData */
$emptyData = $emptyData($this, $viewData);
}
$viewData = $emptyData;
}
// Merge form data from children into existing view data
// It is not necessary to invoke this method if the form has no children,
// even if it is compound.
if (count($this->children) > 0) {
// Use InheritDataAwareIterator to process children of
// descendants that inherit this form's data.
// These descendants will not be submitted normally (see the check
//.........这里部分代码省略.........
示例3: bind
/**
* {@inheritdoc}
*/
public function bind($submittedData)
{
if ($this->bound) {
throw new AlreadyBoundException('A form can only be bound once');
}
if ($this->isDisabled()) {
$this->bound = true;
return $this;
}
// The data must be initialized if it was not initialized yet.
// This is necessary to guarantee that the *_SET_DATA listeners
// are always invoked before bind() takes place.
if (!$this->initialized) {
$this->setData($this->config->getData());
}
// Don't convert NULL to a string here in order to determine later
// whether an empty value has been submitted or whether no value has
// been submitted at all. This is important for processing checkboxes
// and radio buttons with empty values.
if (is_scalar($submittedData)) {
$submittedData = (string) $submittedData;
}
// Initialize errors in the very beginning so that we don't lose any
// errors added during listeners
$this->errors = array();
$dispatcher = $this->config->getEventDispatcher();
$modelData = null;
$normData = null;
$viewData = null;
try {
// Hook to change content of the data bound by the browser
if ($dispatcher->hasListeners(FormEvents::PRE_BIND) || $dispatcher->hasListeners(FormEvents::BIND_CLIENT_DATA)) {
$event = new FormEvent($this, $submittedData);
$dispatcher->dispatch(FormEvents::PRE_BIND, $event);
// BC until 2.3
if ($dispatcher->hasListeners(FormEvents::BIND_CLIENT_DATA)) {
trigger_error('The FormEvents::BIND_CLIENT_DATA event is deprecated since 2.1 and will be removed in 2.3. Use the FormEvents::PRE_BIND event instead.', E_USER_DEPRECATED);
}
$dispatcher->dispatch(FormEvents::BIND_CLIENT_DATA, $event);
$submittedData = $event->getData();
}
// Check whether the form is compound.
// This check is preferable over checking the number of children,
// since forms without children may also be compound.
// (think of empty collection forms)
if ($this->config->getCompound()) {
if (null === $submittedData) {
$submittedData = array();
}
if (!is_array($submittedData)) {
throw new TransformationFailedException('Compound forms expect an array or NULL on submission.');
}
for (reset($this->children); false !== current($this->children); next($this->children)) {
$child = current($this->children);
$name = key($this->children);
$child->bind(isset($submittedData[$name]) ? $submittedData[$name] : null);
unset($submittedData[$name]);
}
$this->extraData = $submittedData;
// If the form is compound, the default data in view format
// is reused. The data of the children is merged into this
// default data using the data mapper.
$viewData = $this->viewData;
} else {
// If the form is not compound, the submitted data is also the data in view format.
$viewData = $submittedData;
}
if (FormUtil::isEmpty($viewData)) {
$emptyData = $this->config->getEmptyData();
if ($emptyData instanceof \Closure) {
/* @var \Closure $emptyData */
$emptyData = $emptyData($this, $viewData);
}
$viewData = $emptyData;
}
// Merge form data from children into existing view data
// It is not necessary to invoke this method if the form has no children,
// even if it is compound.
if (count($this->children) > 0) {
$this->config->getDataMapper()->mapFormsToData($this->children, $viewData);
}
// Normalize data to unified representation
$normData = $this->viewToNorm($viewData);
// Hook to change content of the data into the normalized
// representation
if ($dispatcher->hasListeners(FormEvents::BIND) || $dispatcher->hasListeners(FormEvents::BIND_NORM_DATA)) {
$event = new FormEvent($this, $normData);
$dispatcher->dispatch(FormEvents::BIND, $event);
// BC until 2.3
if ($dispatcher->hasListeners(FormEvents::BIND_NORM_DATA)) {
trigger_error('The FormEvents::BIND_NORM_DATA event is deprecated since 2.1 and will be removed in 2.3. Use the FormEvents::BIND event instead.', E_USER_DEPRECATED);
}
$dispatcher->dispatch(FormEvents::BIND_NORM_DATA, $event);
$normData = $event->getData();
}
// Synchronize representations - must not change the content!
$modelData = $this->normToModel($normData);
//.........这里部分代码省略.........
示例4: __construct
/**
* Creates an unmodifiable copy of a given configuration.
*
* @param FormConfigInterface $config The configuration to copy.
*/
public function __construct(FormConfigInterface $config)
{
$dispatcher = $config->getEventDispatcher();
if (!$dispatcher instanceof UnmodifiableEventDispatcher) {
$dispatcher = new UnmodifiableEventDispatcher($dispatcher);
}
$this->dispatcher = $dispatcher;
$this->name = $config->getName();
$this->propertyPath = $config->getPropertyPath();
$this->mapped = $config->getMapped();
$this->byReference = $config->getByReference();
$this->virtual = $config->getVirtual();
$this->compound = $config->getCompound();
$this->types = $config->getTypes();
$this->viewTransformers = $config->getViewTransformers();
$this->modelTransformers = $config->getModelTransformers();
$this->dataMapper = $config->getDataMapper();
$this->validators = $config->getValidators();
$this->required = $config->getRequired();
$this->disabled = $config->getDisabled();
$this->errorBubbling = $config->getErrorBubbling();
$this->emptyData = $config->getEmptyData();
$this->attributes = $config->getAttributes();
$this->data = $config->getData();
$this->dataClass = $config->getDataClass();
$this->options = $config->getOptions();
}
示例5: bind
/**
* {@inheritdoc}
*/
public function bind($submittedData)
{
if ($this->bound) {
throw new AlreadyBoundException('A form can only be bound once');
}
if ($this->isDisabled()) {
$this->bound = true;
return $this;
}
// The data must be initialized if it was not initialized yet.
// This is necessary to guarantee that the *_SET_DATA listeners
// are always invoked before bind() takes place.
if (!$this->initialized) {
$this->setData($this->config->getData());
}
// Don't convert NULL to a string here in order to determine later
// whether an empty value has been submitted or whether no value has
// been submitted at all. This is important for processing checkboxes
// and radio buttons with empty values.
if (is_scalar($submittedData)) {
$submittedData = (string) $submittedData;
}
// Initialize errors in the very beginning so that we don't lose any
// errors added during listeners
$this->errors = array();
$dispatcher = $this->config->getEventDispatcher();
// Hook to change content of the data bound by the browser
if ($dispatcher->hasListeners(FormEvents::PRE_BIND) || $dispatcher->hasListeners(FormEvents::BIND_CLIENT_DATA)) {
$event = new FormEvent($this, $submittedData);
$dispatcher->dispatch(FormEvents::PRE_BIND, $event);
// BC until 2.3
$dispatcher->dispatch(FormEvents::BIND_CLIENT_DATA, $event);
$submittedData = $event->getData();
}
// Check whether the form is compound.
// This check is preferrable over checking the number of children,
// since forms without children may also be compound.
// (think of empty collection forms)
if ($this->config->getCompound()) {
if (!is_array($submittedData)) {
$submittedData = array();
}
foreach ($this->children as $name => $child) {
$child->bind(isset($submittedData[$name]) ? $submittedData[$name] : null);
unset($submittedData[$name]);
}
$this->extraData = $submittedData;
// If the form is compound, the default data in view format
// is reused. The data of the children is merged into this
// default data using the data mapper.
$viewData = $this->viewData;
} else {
// If the form is not compound, the submitted data is also the data in view format.
$viewData = $submittedData;
}
if (FormUtil::isEmpty($viewData)) {
$emptyData = $this->config->getEmptyData();
if ($emptyData instanceof \Closure) {
/* @var \Closure $emptyData */
$emptyData = $emptyData($this, $viewData);
}
$viewData = $emptyData;
}
// Merge form data from children into existing view data
// It is not necessary to invoke this method if the form has no children,
// even if it is compound.
if (count($this->children) > 0) {
$this->config->getDataMapper()->mapFormsToData($this->children, $viewData);
}
$modelData = null;
$normData = null;
try {
// Normalize data to unified representation
$normData = $this->viewToNorm($viewData);
// Hook to change content of the data into the normalized
// representation
if ($dispatcher->hasListeners(FormEvents::BIND) || $dispatcher->hasListeners(FormEvents::BIND_NORM_DATA)) {
$event = new FormEvent($this, $normData);
$dispatcher->dispatch(FormEvents::BIND, $event);
// BC until 2.3
$dispatcher->dispatch(FormEvents::BIND_NORM_DATA, $event);
$normData = $event->getData();
}
// Synchronize representations - must not change the content!
$modelData = $this->normToModel($normData);
$viewData = $this->normToView($normData);
} catch (TransformationFailedException $e) {
$this->synchronized = false;
}
$this->bound = true;
$this->modelData = $modelData;
$this->normData = $normData;
$this->viewData = $viewData;
if ($dispatcher->hasListeners(FormEvents::POST_BIND)) {
$event = new FormEvent($this, $viewData);
$dispatcher->dispatch(FormEvents::POST_BIND, $event);
}
//.........这里部分代码省略.........