/** * @param \Symfony\Component\Form\FormView $view * @param \Symfony\Component\Form\FormInterface $form */ public function buildView(FormView $view, FormInterface $form) { $sonataAdmin = $form->getAttribute('sonata_admin'); // avoid to add extra information not required by non admin field if ($form->getAttribute('sonata_admin_enabled', true)) { $sonataAdmin['value'] = $form->getData(); // add a new block types, so the Admin Form element can be tweaked based on the admin code $types = $view->get('types'); $baseName = str_replace('.', '_', $sonataAdmin['field_description']->getAdmin()->getCode()); $baseType = $types[count($types) - 1]; $types[] = sprintf('%s_%s', $baseName, $baseType); $types[] = sprintf('%s_%s_%s', $baseName, $sonataAdmin['field_description']->getName(), $baseType); if ($sonataAdmin['block_name']) { $types[] = $sonataAdmin['block_name']; } $view->set('types', $types); $view->set('sonata_admin_enabled', true); $view->set('sonata_admin', $sonataAdmin); $attr = $view->get('attr', array()); if (!isset($attr['class'])) { $attr['class'] = $sonataAdmin['class']; } $view->set('attr', $attr); } else { $view->set('sonata_admin_enabled', false); } $view->set('sonata_admin', $sonataAdmin); }
/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options) { $choiceList = $form->getAttribute('choice_list'); // We should not load anything right now when loading via ajax $view->set('multiple', $form->getAttribute('multiple'))->set('expanded', $form->getAttribute('expanded'))->set('preferred_choices', array())->set('choices', array())->set('separator', '-------------------')->set('empty_value', $form->getAttribute('empty_value')); if ($view->get('multiple') && !$view->get('expanded')) { // Add "[]" to the name in case a select tag with multiple options is // displayed. Otherwise only one of the selected options is sent in the // POST request. $view->set('full_name', $view->get('full_name') . '[]'); } }
public function buildView(FormView $view, FormInterface $form) { $choices = $form->getAttribute('choice_list')->getChoices(); $preferred = array_flip($form->getAttribute('preferred_choices')); $view->set('multiple', $form->getAttribute('multiple'))->set('expanded', $form->getAttribute('expanded'))->set('preferred_choices', array_intersect_key($choices, $preferred))->set('choices', array_diff_key($choices, $preferred))->set('separator', '-------------------')->set('empty_value', ''); if ($view->get('multiple') && !$view->get('expanded')) { // Add "[]" to the name in case a select tag with multiple options is // displayed. Otherwise only one of the selected options is sent in the // POST request. $view->set('full_name', $view->get('full_name') . '[]'); } }
public function zform(\Symfony\Component\Form\FormView $field) { $attr = $field->get('attr', array()); $attr['class'] = 'zTextForm'; $field->set('attr', $attr); return $this->helper->render(array('field' => $field, 'id' => 1)); }
/** * @param \Symfony\Component\Form\FormView $view * @param \Symfony\Component\Form\FormInterface $form */ public function buildView(FormView $view, FormInterface $form) { $format = $form->getAttribute('format'); $imageSrc = $view->get('image_src'); if ($imageSrc !== null && $format !== null) { $view->set('image_src', $this->imManager->getUrl($format, $imageSrc)); } }
/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form) { $values = $view->get('value'); $selecteds = array_flip($values); $choices_selected = $choices_unselected = array(); //Rebuilds choices foreach ($this->choices as $key => $choice) { if (isset($selecteds[$key])) { $choices_selected[$key] = $choice; } else { $choices_unselected[$key] = $choice; } } $view->set('choices_selected', $choices_selected); $view->set('choices_unselected', $choices_unselected); }
protected function renderSection(FormView $view, $section, array $variables = array()) { $template = null; $blocks = $view->get('types'); if ('widget' === $section || 'row' === $section) { array_unshift($blocks, '_' . $view->get('id')); } foreach ($blocks as &$block) { $block = $block . '_' . $section; $template = $this->lookupTemplate($block); if ($template) { break; } } if (!$template) { throw new FormException(sprintf('Unable to render form as none of the following blocks exist: "%s".', implode('", "', $blocks))); } if ('widget' === $section || 'row' === $section) { $view->setRendered(); } return $this->render($template, array_merge($view->all(), $variables)); }
/** * Get password. * TODO: 無理矢理パスワードを取得してる為、もうちょい考えてみる */ public function getPassword(FormView $view) { $method = 'get' . ucfirst($view->get('name')); return $view->getParent()->get('value')->{$method}(); }
public function render(FormView $form, $variables, FormRenderer $renderer) { return $form->get('multipart') ? 'enctype="multipart/form-data"' : ''; }
/** * Returns the name of the template to use to render the block * * @param FormView $view The form view * @param string $block The name of the block * * @return string|Boolean The template logical name or false when no template is found */ protected function lookupTemplate(FormView $view, $block) { $file = $block . '.html.php'; $id = $view->get('id'); if (!isset($this->templates[$id][$block])) { $template = false; $themes = $view->hasParent() ? array() : $this->resources; if (isset($this->themes[$id])) { $themes = array_merge($themes, $this->themes[$id]); } for ($i = count($themes) - 1; $i >= 0; --$i) { if ($this->engine->exists($templateName = $themes[$i] . ':' . $file)) { $template = $templateName; break; } } if (false === $template && $view->hasParent()) { $template = $this->lookupTemplate($view->getParent(), $block); } $this->templates[$id][$block] = $template; } return $this->templates[$id][$block]; }
/** * Renders a template. * * 1. This function first looks for a block named "_<view id>_<section>", * 2. if such a block is not found the function will look for a block named * "<type name>_<section>", * 3. the type name is recursively replaced by the parent type name until a * corresponding block is found * * @param FormView $view The form view * @param string $section The section to render (i.e. 'row', 'widget', 'label', ...) * @param array $variables Additional variables * * @return string The html markup * * @throws FormException if no template block exists to render the given section of the view */ protected function renderSection(FormView $view, $section, array $variables = array()) { $mainTemplate = in_array($section, array('row', 'widget')); if ($mainTemplate && $view->isRendered()) { return ''; } $template = null; $types = $view->get('types'); $types[] = '_'.$view->get('proto_id', $view->get('id')); for ($i = count($types) - 1; $i >= 0; $i--) { $types[$i] .= '_'.$section; $template = $this->lookupTemplate($types[$i]); if ($template) { $html = $this->render($view, $template, $variables); if ($mainTemplate) { $view->setRendered(); } return $html; } } throw new FormException(sprintf('Unable to render form as none of the following blocks exist: "%s".', implode('", "', $types))); }
protected function render(FormView $view, $section, array $variables = array()) { $mainTemplate = in_array($section, array('widget', 'row')); if ($mainTemplate && $view->isRendered()) { return ''; } $id = '_' . $view->get('proto_id', $view->get('id')); $template = $id . $section; $renderer = $this->getRenderer(); if (isset($this->varStack[$template])) { $typeIndex = $this->varStack[$template]['typeIndex'] - 1; $types = $this->varStack[$template]['types']; $this->varStack[$template]['variables'] = array_replace_recursive($this->varStack[$template]['variables'], $variables); } else { $types = $view->get('types'); $types[] = $id; $typeIndex = count($types) - 1; $this->varStack[$template] = array('variables' => array_replace_recursive($view->all(), $variables), 'types' => $types); } do { $types[$typeIndex] .= '_' . $section; if (isset($renderer[$types[$typeIndex]])) { $this->varStack[$template]['typeIndex'] = $typeIndex; $html = $renderer[$types[$typeIndex]]->render($view, $this->varStack[$template]['variables'], $this); if ($mainTemplate) { $view->setRendered(); } unset($this->varStack[$template]); return $html; } } while (--$typeIndex >= 0); throw new FormException(sprintf('Unable to render the form as none of the following renderer exist: "%s".', implode('", "', array_reverse($types)))); }
/** * Renders a template. * * 1. This function first looks for a block named "_<view id>_<section>", * 2. if such a block is not found the function will look for a block named * "<type name>_<section>", * 3. the type name is recursively replaced by the parent type name until a * corresponding block is found * * @param FormView $view The form view * @param string $section The section to render (i.e. 'row', 'widget', 'label', ...) * @param array $variables Additional variables * * @return string The html markup * * @throws FormException if no template block exists to render the given section of the view */ protected function renderSection(FormView $view, $section, array $variables = array()) { $mainTemplate = in_array($section, array('row', 'widget')); if ($mainTemplate && $view->isRendered()) { return ''; } $template = null; $blocks = $view->get('types'); array_unshift($blocks, '_' . $view->get('id')); foreach ($blocks as &$block) { $block = $block . '_' . $section; $template = $this->lookupTemplate($block); if ($template) { break; } } if (!$template) { throw new FormException(sprintf('Unable to render form as none of the following blocks exist: "%s".', implode('", "', $blocks))); } $html = $this->render($view, $template, $variables); if ($mainTemplate) { $view->setRendered(); } return $html; }
/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form) { $choiceList = $form->getAttribute('choice_list'); $view->set('multiple', $form->getAttribute('multiple'))->set('expanded', $form->getAttribute('expanded'))->set('preferred_choices', $choiceList->getPreferredViews())->set('choices', $choiceList->getRemainingViews())->set('separator', '-------------------')->set('empty_value', $form->getAttribute('empty_value')); if ($view->get('multiple') && !$view->get('expanded')) { // Add "[]" to the name in case a select tag with multiple options is // displayed. Otherwise only one of the selected options is sent in the // POST request. $view->set('full_name', $view->get('full_name') . '[]'); } }
/** */ public function isChoiceSelected(FormView $view, $choice) { return FormUtil::isChoiceSelected($choice, $view->get('value')); }
/** * {@inheritdoc} */ public function buildViewBottomUp(FormView $view, FormInterface $form) { if ($form->hasAttribute('prototype') && $view->get('prototype')->get('multipart')) { $view->set('multipart', true); } }
/** * {@inheritdoc} */ public function buildViewBottomUp(FormView $view, FormInterface $form) { if ($view->get('expanded')) { // Radio buttons should have the same name as the parent $childName = $view->get('full_name'); // Checkboxes should append "[]" to allow multiple selection if ($view->get('multiple')) { $childName .= '[]'; } foreach ($view->getChildren() as $childView) { $childView->set('full_name', $childName); } } }
/** * Renders a template. * * 1. This function first looks for a block named "_<view id>_<section>", * 2. if such a block is not found the function will look for a block named * "<type name>_<section>", * 3. the type name is recursively replaced by the parent type name until a * corresponding block is found * * @param FormView $view The form view * @param string $section The section to render (i.e. 'row', 'widget', 'label', ...) * @param array $variables Additional variables * * @return string The html markup * * @throws FormException if no template block exists to render the given section of the view */ protected function render(FormView $view, $section, array $variables = array()) { $mainTemplate = in_array($section, array('widget', 'row')); if ($mainTemplate && $view->isRendered()) { return ''; } if (null === $this->template) { $this->template = reset($this->resources); if (!$this->template instanceof \Twig_Template) { $this->template = $this->environment->loadTemplate($this->template); } } $custom = '_' . $view->get('id'); $rendering = $custom . $section; $blocks = $this->getBlocks($view); if (isset($this->varStack[$rendering])) { $typeIndex = $this->varStack[$rendering]['typeIndex'] - 1; $types = $this->varStack[$rendering]['types']; $this->varStack[$rendering]['variables'] = array_replace_recursive($this->varStack[$rendering]['variables'], $variables); } else { $types = $view->get('types'); $types[] = $custom; $typeIndex = count($types) - 1; $this->varStack[$rendering] = array('variables' => array_replace_recursive($view->all(), $variables), 'types' => $types); } do { $types[$typeIndex] .= '_' . $section; if (isset($blocks[$types[$typeIndex]])) { $this->varStack[$rendering]['typeIndex'] = $typeIndex; // we do not call renderBlock here to avoid too many nested level calls (XDebug limits the level to 100 by default) ob_start(); $this->template->displayBlock($types[$typeIndex], $this->varStack[$rendering]['variables'], $blocks); $html = ob_get_clean(); if ($mainTemplate) { $view->setRendered(); } unset($this->varStack[$rendering]); return $html; } } while (--$typeIndex >= 0); throw new FormException(sprintf('Unable to render the form as none of the following blocks exist: "%s".', implode('", "', array_reverse($types)))); }
/** * @param FormView $form A form. * @return boolean If the form (taking into account all of its children) has errors. */ public function hasFormDeepErrors(FormView $form) { if (count($form->get('errors')) > 0) { return true; } foreach ($form->getChildren() as $child) { if ($this->hasFormDeepErrors($child)) { return true; } } return false; }