public function enable()
 {
     $this->validateScripts = array();
     $this->toggleScript = '';
     $this->central = TRUE;
     foreach ($this->form->getControls() as $control) {
         $script = $this->getValidateScript($control->getRules());
         if ($script) {
             $this->validateScripts[$control->getHtmlName()] = $script;
         }
         $this->toggleScript .= $this->getToggleScript($control->getRules());
         if ($control instanceof ISubmitterControl && $control->getValidationScope() !== TRUE) {
             $this->central = FALSE;
         }
     }
     if ($this->validateScripts || $this->toggleScript) {
         if ($this->central) {
             $this->form->getElementPrototype()->onsubmit("return nette.validateForm(this)", TRUE);
         } else {
             foreach ($this->form->getComponents(TRUE, 'Nette\\Forms\\ISubmitterControl') as $control) {
                 if ($control->getValidationScope()) {
                     $control->getControlPrototype()->onclick("return nette.validateForm(this)", TRUE);
                 }
             }
         }
     }
 }
 /**
  * Render the templates.
  * @param Form    $form
  * @param string  $mode
  * @param array   $args
  */
 public function render(Form $form, $mode = NULL, $args = NULL)
 {
     if ($this->template === NULL) {
         if ($presenter = $form->lookup('Nette\\Application\\UI\\Presenter', FALSE)) {
             /** @var \Nette\Application\UI\Presenter $presenter */
             $this->template = clone $presenter->getTemplate();
         } else {
             $this->template = new FileTemplate();
             $this->template->registerFilter(new \Nette\Latte\Engine());
         }
     }
     if ($this->form !== $form) {
         $this->form = $form;
         // translators
         if ($translator = $this->form->getTranslator()) {
             $this->template->setTranslator($translator);
         }
         // controls placeholdersĀ & classes
         foreach ($this->form->getControls() as $control) {
             $this->prepareControl($control);
         }
         $formEl = $form->getElementPrototype();
         if (!($classes = self::getClasses($formEl)) || stripos($classes, 'form-') === FALSE) {
             //$formEl->addClass('form-horizontal');
         }
     } elseif ($mode === 'begin') {
         foreach ($this->form->getControls() as $control) {
             /** @var \Nette\Forms\Controls\BaseControl $control */
             $control->setOption('rendered', FALSE);
         }
     }
     $this->template->setFile(__DIR__ . '/@form.latte');
     $this->template->setParameters(array_fill_keys(array('control', '_control', 'presenter', '_presenter'), NULL) + array('_form' => $this->form, 'form' => $this->form, 'renderer' => $this));
     if ($mode === NULL) {
         if ($args) {
             $this->form->getElementPrototype()->addAttributes($args);
         }
         $this->template->render();
     } elseif ($mode === 'begin') {
         FormMacros::renderFormBegin($this->form, (array) $args);
     } elseif ($mode === 'end') {
         FormMacros::renderFormEnd($this->form);
     } else {
         $attrs = array('input' => array(), 'label' => array());
         foreach ((array) $args as $key => $val) {
             if (stripos($key, 'input-') === 0) {
                 $attrs['input'][substr($key, 6)] = $val;
             } elseif (stripos($key, 'label-') === 0) {
                 $attrs['label'][substr($key, 6)] = $val;
             }
         }
         $this->template->setFile(__DIR__ . '/@parts.latte');
         $this->template->mode = $mode;
         $this->template->attrs = (array) $attrs;
         $this->template->render();
     }
 }
 private function decorateFormControls()
 {
     $this->form->getElementPrototype()->class('form-horizontal');
     $this->usedPrimary = FALSE;
     foreach ($this->form->getControls() as $control) {
         if ($control instanceof NetteBaseControl) {
             $this->decorateFormControl($control);
         }
     }
 }
Example #4
0
 /**
  * @return array
  */
 public function findErrors()
 {
     $formErrors = method_exists($this->form, 'getAllErrors') ? $this->form->getErrors() : $this->form->getErrors();
     if (!$formErrors) {
         return array();
     }
     $form = $this->form;
     $translate = function ($errors) use($form) {
         if ($translator = $form->getTranslator()) {
             // If we have translator, translate!
             foreach ($errors as $key => $val) {
                 $errors[$key] = $translator->translate($val);
             }
         }
         return $errors;
     };
     if (!$this->errorsAtInputs) {
         return $translate($formErrors);
     }
     if (method_exists($this->form, 'getAllErrors')) {
         return $translate($this->form->getErrors());
     }
     foreach ($this->form->getControls() as $control) {
         /** @var \Nette\Forms\Controls\BaseControl $control */
         if (!$control->hasErrors()) {
             continue;
         }
         $formErrors = array_diff($formErrors, $control->getErrors());
     }
     return $translate($formErrors);
 }
 /**
  * @return array
  */
 public function findFormControls()
 {
     $controls = iterator_to_array($this->form->getControls());
     return array_filter($controls, function (Controls\BaseControl $control) {
         return !$control->getOption('rendered');
     });
 }
 public function renderBody()
 {
     $this->assertInForm();
     $groups = $this->renderGroups();
     $groupless = $this->renderPairs($this->form->getControls());
     return $this->areGroupsRenderedFirst() ? $groups . "\n" . $groupless : $groupless . "\n" . $groups;
 }
 /**
  * Renders form end.
  * @return string
  */
 public function renderEnd()
 {
     $s = '';
     foreach ($this->form->getControls() as $control) {
         if ($control instanceof HiddenField && !$control->getOption('rendered')) {
             $s .= (string) $control->getControl();
         }
     }
     if ($s) {
         $s = $this->getWrapper('hidden container')->setHtml($s) . "\n";
     }
     return $s . $this->form->getElementPrototype()->endTag() . "\n";
 }
Example #8
0
 /**
  * @param Form $form
  */
 public static function ApplyBootstrapToControls(Form $form)
 {
     $usedPrimary = FALSE;
     foreach ($form->getControls() as $control) {
         if ($control instanceof Controls\Button) {
             $control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-default');
             $usedPrimary = TRUE;
         } elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
             $control->getControlPrototype()->addClass('form-control');
         } elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
             $control->getSeparatorPrototype()->setName('div')->addClass($control->getControlPrototype()->type);
         }
     }
 }
Example #9
0
 /**
  * Find specific controls within the current form
  *
  * @param string $fieldtype
  * @param bool   $skipRendered false if you want to get even the rendered ones
  * @return array
  *
  * @internal
  */
 public function findFields($fieldtype, $skipRendered = TRUE)
 {
     $stack = array();
     $fieldtype = $fieldtype;
     foreach ($this->form->getControls() as $control) {
         if ($skipRendered && $control->getOption('rendered', FALSE)) {
             continue;
         }
         if ($control->getOption('blockname') === $fieldtype) {
             $stack[] = $control;
         }
     }
     return $stack;
 }
Example #10
0
 /**
  * Renders form begin.
  * @return string
  */
 public static function renderFormBegin(Form $form, array $attrs, $withTags = TRUE)
 {
     foreach ($form->getControls() as $control) {
         $control->setOption('rendered', FALSE);
     }
     $el = $form->getElementPrototype();
     $el->action = (string) $el->action;
     $el = clone $el;
     if (strcasecmp($form->getMethod(), 'get') === 0) {
         $el->action = preg_replace('~\\?[^#]*~', '', $el->action, 1);
     }
     $el->addAttributes($attrs);
     return $withTags ? $el->startTag() : $el->attributes();
 }
Example #11
0
 /**
  * @param \Nette\Forms\Form $form
  * @param string|null $mode
  * @return string
  */
 public function render(Nette\Forms\Form $form, $mode = null)
 {
     $form->getElementPrototype()->class[] = 'form-horizontal';
     foreach ($form->getControls() as $control) {
         if ($control instanceof Controls\Button) {
             $control->setAttribute('class', empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-default');
             $usedPrimary = true;
         } elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
             $control->setAttribute('class', 'form-control');
         } elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
             $control->getSeparatorPrototype()->setName('div')->class($control->getControlPrototype()->type);
         }
     }
     return parent::render($form, $mode);
 }
Example #12
0
 public function render(Forms\Form $form, $mode = NULL)
 {
     $form->getElementPrototype()->class('form-horizontal')->novalidate(TRUE);
     foreach ($form->getControls() as $control) {
         if ($control instanceof Controls\Button) {
             $control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-default');
             $usedPrimary = TRUE;
         } elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
             $control->getControlPrototype()->addClass('form-control');
         } elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
             $control->getSeparatorPrototype()->setName('div')->addClass($control->getControlPrototype()->type);
         }
     }
     return parent::render($form, $mode);
 }
Example #13
0
 /**
  * Renders form end.
  * @return string
  */
 public function renderEnd()
 {
     $s = '';
     foreach ($this->form->getControls() as $control) {
         if ($control instanceof Nette\Forms\Controls\HiddenField && !$control->getOption('rendered')) {
             $s .= (string) $control->getControl();
         }
     }
     if (iterator_count($this->form->getComponents(TRUE, 'Nette\\Forms\\Controls\\TextInput')) < 2) {
         $s .= '<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->';
     }
     if ($s) {
         $s = $this->getWrapper('hidden container')->setHtml($s) . "\n";
     }
     return $s . $this->form->getElementPrototype()->endTag() . "\n";
 }
 /**
  * Provides complete form rendering.
  * @param  Nette\Forms\Form
  * @param  string 'begin', 'errors', 'ownerrors', 'body', 'end' or empty to render all
  * @return string
  */
 public function render(Nette\Forms\Form $form, $mode = null)
 {
     $form->getElementPrototype()->addClass('form-inline');
     foreach ($form->getControls() as $control) {
         if ($control instanceof Controls\Button) {
             if (strpos($control->getControlPrototype()->getClass(), 'btn') === FALSE) {
                 $control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-default');
                 $usedPrimary = true;
             }
         } elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
             $control->getControlPrototype()->addClass('form-control');
         } elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
             $control->getSeparatorPrototype()->setName('div')->addClass($control->getControlPrototype()->type);
         }
     }
     return parent::render($form, $mode);
 }
Example #15
0
 public function render(\Nette\Forms\Form $form, $mode = null)
 {
     $this->wrappers['controls']['container'] = 'div class=col-sm-12';
     $this->wrappers['pair']['container'] = 'div class=form-group';
     $this->wrappers['control']['container'] = 'div class=col-lg-8';
     $this->wrappers['label']['container'] = 'label class=col-lg-4';
     // make form and controls compatible with Twitter Bootstrap
     $form->getElementPrototype()->class('form-horizontal');
     foreach ($form->getControls() as $control) {
         if ($control instanceof Controls\Button) {
             $control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-default');
             $usedPrimary = true;
         } elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
             $control->getControlPrototype()->addClass('form-control');
         } elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
             $control->getSeparatorPrototype()->setName('div')->addClass($control->getControlPrototype()->type);
         }
     }
     return parent::render($form, $mode);
 }
Example #16
0
 /**
  * Renders form end.
  * @return string
  */
 public static function renderFormEnd(Form $form, $withTags = TRUE)
 {
     $s = '';
     if (strcasecmp($form->getMethod(), 'get') === 0) {
         foreach (preg_split('#[;&]#', parse_url($form->getElementPrototype()->action, PHP_URL_QUERY), NULL, PREG_SPLIT_NO_EMPTY) as $param) {
             $parts = explode('=', $param, 2);
             $name = urldecode($parts[0]);
             if (!isset($form[$name])) {
                 $s .= Html::el('input', ['type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])]);
             }
         }
     }
     foreach ($form->getControls() as $control) {
         if ($control->getOption('type') === 'hidden' && !$control->getOption('rendered')) {
             $s .= $control->getControl();
         }
     }
     if (iterator_count($form->getComponents(TRUE, Nette\Forms\Controls\TextInput::class)) < 2) {
         $s .= "<!--[if IE]><input type=IEbug disabled style=\"display:none\"><![endif]-->\n";
     }
     return $s . ($withTags ? $form->getElementPrototype()->endTag() . "\n" : '');
 }
 /**
  * @return array
  */
 public function findErrors()
 {
     if (!($formErrors = $this->form->getErrors())) {
         return array();
     }
     if (!$this->errorsAtInputs) {
         return $formErrors;
     }
     foreach ($this->form->getControls() as $control) {
         /** @var \Nette\Forms\Controls\BaseControl $control */
         if (!$control->hasErrors()) {
             continue;
         }
         $formErrors = array_diff($formErrors, $control->getErrors());
     }
     // If we have translator, translate!
     if ($translator = $this->form->getTranslator()) {
         foreach ($formErrors as $key => $val) {
             $formErrors[$key] = $translator->translate($val);
         }
     }
     return $formErrors;
 }
Example #18
0
 public static function bootstrapForm(Form $form)
 {
     // setup form rendering
     $renderer = $form->getRenderer();
     $renderer->wrappers['controls']['container'] = NULL;
     $renderer->wrappers['pair']['container'] = 'div class="form-group"';
     $renderer->wrappers['pair']['.error'] = 'has-error';
     $renderer->wrappers['control']['container'] = 'div class="col-sm-9"';
     $renderer->wrappers['label']['container'] = 'div class="col-sm-3 control-label"';
     $renderer->wrappers['control']['description'] = 'span class=help-block';
     $renderer->wrappers['control']['errorcontainer'] = 'span class=help-block';
     // make form and controls compatible with Twitter Bootstrap
     $form->getElementPrototype()->class('form-horizontal');
     foreach ($form->getControls() as $control) {
         if ($control instanceof Controls\Button) {
             $control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-shadow' : 'btn btn-default');
             $usedPrimary = TRUE;
         } elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
             $control->getControlPrototype()->addClass('form-control form-control-shadow');
         } elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
             $control->getSeparatorPrototype()->setName('div')->addClass($control->getControlPrototype()->type);
         }
     }
 }
 /**
  * Sets entity values by filled form values.
  * @param Form $form
  */
 public function setFilledValues(Form $form)
 {
     foreach ($form->getControls() as $name => $control) {
         $property = $this->getPropertyByFormControlName($name);
         if ($property === null) {
             continue;
         }
         $property->setAccessible(true);
         $property->setValue($this, $control->getValue());
     }
 }
Example #20
0
    echo '<h2>Form was submitted and successfully validated</h2>';
    Dumper::dump($form->getValues());
    exit;
}
// setup form rendering
$renderer = $form->getRenderer();
$renderer->wrappers['controls']['container'] = NULL;
$renderer->wrappers['pair']['container'] = 'div class=control-group';
$renderer->wrappers['pair']['.error'] = 'error';
$renderer->wrappers['control']['container'] = 'div class=controls';
$renderer->wrappers['label']['container'] = 'div class=control-label';
$renderer->wrappers['control']['description'] = 'span class=help-inline';
$renderer->wrappers['control']['errorcontainer'] = 'span class=help-inline';
// make form and controls compatible with Twitter Bootstrap
$form->getElementPrototype()->class('form-horizontal');
foreach ($form->getControls() as $control) {
    if ($control instanceof Controls\Button) {
        $control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn');
        $usedPrimary = TRUE;
    } elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
        $control->getLabelPrototype()->addClass($control->getControlPrototype()->type);
        $control->getSeparatorPrototype()->setName(NULL);
    }
}
?>
<!DOCTYPE html>
<meta charset="utf-8">
<title>Nette Forms & Bootstrap 2 rendering example</title>

<link rel="stylesheet" media="screen" href="http://netdna.bootstrapcdn.com/bootstrap/2.3.2/css/bootstrap.min.css" />
 /**
  * Render the templates
  *
  * @param \Nette\Forms\Form $form
  * @param string $mode
  * @param array $args
  * @return void
  */
 public function render(Nette\Forms\Form $form, $mode = NULL, $args = NULL)
 {
     if ($this->template === NULL) {
         if ($presenter = $form->lookup('Nette\\Application\\UI\\Presenter', FALSE)) {
             /** @var \Nette\Application\UI\Presenter $presenter */
             $this->template = clone $presenter->getTemplate();
         } else {
             $this->template = new Nette\Bridges\ApplicationLatte\Template(new Nette\Latte\Engine());
         }
     }
     if ($this->form !== $form) {
         $this->form = $form;
         // translators
         if ($translator = $this->form->getTranslator()) {
             $this->template->setTranslator($translator);
         }
         // controls placeholdersĀ & classes
         foreach ($this->form->getControls() as $control) {
             $this->prepareControl($control);
         }
         $formEl = $form->getElementPrototype();
         if (!($classes = self::getClasses($formEl)) || stripos($classes, 'form-') === FALSE) {
             $this->horizontalMode = $this->mode === self::MODE_HORIZONTAL;
             if ($this->mode !== self::MODE_NO_CLASS) {
                 $formEl->addClass($this->mode);
             }
         }
     } elseif ($mode === 'begin') {
         foreach ($this->form->getControls() as $control) {
             /** @var \Nette\Forms\Controls\BaseControl $control */
             $control->setOption('rendered', FALSE);
         }
     }
     $this->template->setFile(__DIR__ . '/@form.latte');
     $this->template->setParameters(array_fill_keys(array('control', '_control', 'presenter', '_presenter'), NULL) + array('_form' => $this->form, 'form' => $this->form, 'renderer' => $this));
     if ($this->horizontalMode) {
         $this->template->labelCols = $this->labelColumns;
         $this->template->inputCols = $this->inputColumns;
         $this->template->labelClass = $this->columnClassPrefix . $this->labelColumns;
         $this->template->inputClass = $this->columnClassPrefix . $this->inputColumns;
         $this->template->skipClass = $this->columnClassPrefix . 'offset-' . $this->labelColumns;
     }
     if ($mode === NULL) {
         if ($args) {
             $this->form->getElementPrototype()->addAttributes($args);
         }
         $this->template->render();
     } elseif ($mode === 'begin') {
         FormMacros::renderFormBegin($this->form, (array) $args);
     } elseif ($mode === 'end') {
         FormMacros::renderFormEnd($this->form);
     } else {
         $attrs = array('input' => array(), 'label' => array(), 'pair' => array(), 'pair-class' => '');
         foreach ((array) $args as $key => $val) {
             if (stripos($key, 'input-') === 0) {
                 $attrs['input'][substr($key, 6)] = $val;
             } elseif (stripos($key, 'label-') === 0) {
                 $attrs['label'][substr($key, 6)] = $val;
             } elseif ($key === 'class') {
                 $attrs['pair-class'] = $val;
             } else {
                 $attrs['pair'][$key] = $val;
             }
         }
         if ($this->horizontalMode) {
             if (isset($attrs['label']['class'])) {
                 $attrs['label']['class'] .= ' ' . $this->columnClassPrefix . $this->labelColumns;
             } else {
                 $attrs['label']['class'] = $this->columnClassPrefix . $this->labelColumns;
             }
         }
         $this->template->setFile(__DIR__ . '/@parts.latte');
         $this->template->mode = $mode;
         $this->template->attrs = (array) $attrs;
         $this->template->render();
     }
 }