Beispiel #1
0
// Some basic options
$name = 'username';
$value = '*****@*****.**';
// If there are errors on the input, show it, if not don't even print the elements
$errors[$name] = ["{$name} is required", "{$name} must be a valid email address"];
// If a help text is set, print it with aria-describedby
$help_text = "{$name} is your email address";
// We declare some optional input addons that makes the input wrapped in an input-group if set
$input_group_prepend = new \Illuminate\Support\HtmlString('<span class="input-group-addon"><input type="checkbox" aria-label="Addon checkbox"></span>');
$input_group_append = new \Illuminate\Support\HtmlString('<span class="input-group-btn"><button class="btn btn-default" type="button">Go!</button></span>');
// Build the input's help (aria-describedby) element and keep a reference
$control_help = FluentHtml::create('div')->onlyDisplayedIfHasContent();
// Add any errors relevant to the input as a list in the help element
$control_help->containingElement('ul')->withClass('help-block', 'list-unstyled')->onlyDisplayedIfHasContent()->withContentWrappedIn($errors[$name], 'li', ['class' => 'text-capitalize-first'])->followedByElement('span', $help_text)->withClass('help-block')->onlyDisplayedIfHasContent();
// Build the input element and keep a reference
$input = FluentHtml::create('input')->withAttribute('type', 'text')->withClass('form-control')->withAttribute(['name' => $name, 'value' => $value, 'readonly'])->withAttribute('aria-describedby', function () use($control_help) {
    // Only set the input's aria-describedby attribute if the help element has any content
    if ($control_help->hasContent()) {
        return $control_help->getId();
    }
});
// Build the input-group
$input_group = $input->siblingsWrappedInElement(function ($input_group) {
    // Print the input-group tag only when there's at least one input group addon next to the input
    return $input_group->getContentCount() > 1 ? 'div' : false;
})->withClass('input-group')->withPrependedContent($input_group_prepend)->withAppendedContent($input_group_append);
// Wrap up and print the full result from here
echo $input_group->precededByElement('label', empty($label) ? $name : $label)->withClass('control-label')->withAttribute('for', function () use($input) {
    return $input->getId();
})->siblingsWrappedInElement('div')->withClass('form-group')->withClass(function () use($errors, $name) {
    // Set the validation state class on the form-group
 /**
  * Overridden to make sure the called class is reported correctly even when overriding
  * and to make instance creation publicly accessible.
  * @param string $classname
  * @param array $parameters
  * @return \FewAgency\FluentHtml\FluentHtmlElement
  */
 public function createInstanceOf($classname, $parameters = [])
 {
     return parent::createInstanceOf($classname, $parameters);
 }
 /**
  * @param string|callable|null $html_element_name
  */
 public function __construct($html_element_name = null)
 {
     $child_element = $this->createInstanceOf('FluentHtml', ['p', 'B sub content']);
     parent::__construct($html_element_name, ['B content', $child_element]);
 }
 public function testSplicingContent()
 {
     $e = FluentHtml::create('p', 'A')->followedByElement('hr')->followedByElement('br');
     $this->assertHtmlEquals('<p>A</p><hr><br>', $e);
     $e = FluentHtml::create('p', 'A')->precededByElement('hr')->precededByElement('br');
     $this->assertHtmlEquals('<br><hr><p>A</p>', $e);
 }
 /**
  * Create and return a new basic FluentHtmlElement instance
  *
  * @param string|callable|null $html_element_name
  * @param string|Htmlable|array|Arrayable $tag_contents
  * @param array|Arrayable $tag_attributes
  * @return FluentHtmlElement
  */
 protected static function createFluentHtmlElement($html_element_name = null, $tag_contents = [], $tag_attributes = [])
 {
     return FluentHtml::create($html_element_name, $tag_contents, $tag_attributes);
 }
Beispiel #6
0
 /**
  * @expectedException     Exception
  */
 public function testInsertIntoNonFieldset()
 {
     $legend = new \FewAgency\FluentForm\LegendElement();
     \FewAgency\FluentHtml\FluentHtml::create('div', $legend);
 }