Skip to content

Instantly share code, notes, and snippets.

@hrach
Created July 29, 2013 13:44
Show Gist options
  • Save hrach/6104393 to your computer and use it in GitHub Desktop.
Save hrach/6104393 to your computer and use it in GitHub Desktop.
FormMacros customization of Bootstrap3 macro (for Nette framework). See https://github.com/nextras/forms
<?php
/**
* signaly.cz - christian community platform
* MIT license
*
* @link https://www.signaly.cz
*/
namespace Signaly;
use Nette;
use Nette\Latte;
use Nette\Utils\Html;
use Nette\Forms\Controls;
use Nextras\Forms\Bridges\Latte\Macros\BS3InputMacros;
class FormMacros extends BS3InputMacros
{
public static function install(Latte\Compiler $compiler)
{
$me = parent::install($compiler);
$me->addMacro('labelText', 'echo $_label->setName(NULL)->render();');
$me->addMacro('formErrors', 'echo Signaly\FormMacros::formErrors($_form->getErrors());');
$me->addMacro('formAllErrors', 'echo Signaly\FormMacros::formErrors($_form->getAllErrors());');
}
public static function label(Html $label, Controls\BaseControl $control)
{
$label = parent::label($label, $control);
if ($label->getName() === 'label') {
if (!$control->getOption('required')) {
$label->addClass('optional');
if (!empty($label->optional)) {
unset($label->optional);
$label->add('<small>nepovinné</small>');
}
}
}
return $label;
}
public static function input(Html $input, Controls\BaseControl $control)
{
$input = parent::input($input, $control);
if ($input->getName() === 'button') {
$input->addClass('btn-primary');
}
return $input;
}
public static function formErrors($errors)
{
foreach ($errors as $error) {
echo '<p class="message-error">' . Nette\Templating\Helpers::escapeHtml($error, ENT_NOQUOTES) . '</p>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment