Skip to content
This repository has been archived by the owner on Jul 10, 2020. It is now read-only.

Commit

Permalink
Merge pull request #195 from Saeven/master
Browse files Browse the repository at this point in the history
Adding support for has-feedback.
  • Loading branch information
neilime committed Mar 17, 2017
2 parents 61b484b + 612098b commit 8799d13
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
17 changes: 14 additions & 3 deletions src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function render(ElementInterface $oElement, $sLabelPosition = null)
return $sElementContent . "\n";
default:
// Render element into form group
return $this->renderElementFormGroup($sElementContent, $this->getRowClassFromElement($oElement));
return $this->renderElementFormGroup($sElementContent, $this->getRowClassFromElement($oElement), $oElement->getOption('feedback'));
}
}

Expand All @@ -125,11 +125,14 @@ public function getRowClassFromElement(\Zend\Form\ElementInterface $oElement)
if ($oElement->getMessages()) {
$sRowClass .= ' has-error';
}
if( $oElement->getOption('feedback')) {
$sRowClass .= ' has-feedback';
}

// Column size
if (($sColumSize = $oElement->getOption('column-size')) && $oElement->getOption('twb-layout') !== TwbBundleForm::LAYOUT_HORIZONTAL
) {
$sColumSize = (is_array($sColumSize)) ? $sColumSize : array($sColumSize);
$sColumSize = (is_array($sColumSize)) ? $sColumSize : array($sColumSize);
$sRowClass .= implode('', array_map(function($item) { return ' col-' . $item; }, $sColumSize));
}

Expand All @@ -143,17 +146,25 @@ public function getRowClassFromElement(\Zend\Form\ElementInterface $oElement)
/**
* @param string $sElementContent
* @param string $sRowClass
* @param string $sFeedbackElement A feedback element that should be rendered within the element, optional
*
* @return string
* @throws \InvalidArgumentException
*/
public function renderElementFormGroup($sElementContent, $sRowClass)
public function renderElementFormGroup($sElementContent, $sRowClass, $sFeedbackElement = '' )
{
if (!is_string($sElementContent)) {
throw new \InvalidArgumentException('Argument "$sElementContent" expects a string, "' . (is_object($sElementContent) ? get_class($sElementContent) : gettype($sElementContent)) . '" given');
}
if (!is_string($sRowClass)) {
throw new \InvalidArgumentException('Argument "$sRowClass" expects a string, "' . (is_object($sRowClass) ? get_class($sRowClass) : gettype($sRowClass)) . '" given');
}
if ($sFeedbackElement && !is_string($sFeedbackElement)) {
throw new \InvalidArgumentException('Argument "$sFeedbackElement" expects a string, "' . (is_object($sFeedbackElement) ? get_class($sFeedbackElement) : gettype($sFeedbackElement)) . '" given');
}
if( $sFeedbackElement ){
$sElementContent .= '<i class="' . $sFeedbackElement . ' form-control-feedback"></i>';
}
return sprintf(static::$formGroupFormat, $sRowClass, $sElementContent) . "\n";
}

Expand Down
20 changes: 17 additions & 3 deletions tests/TwbBundleTest/Form/View/Helper/TwbBundleFormRowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function testRenderInputWithHelpTextAndError()
// Test content
$this->assertStringEqualsFile($this->expectedPath . 'input-with-help-text-and-error.phtml', $this->formRowHelper->__invoke($oElement));
}

public function testRenderWithBothInlineAndNoInlineRadios() {
$oForm = new \Zend\Form\Form();
$oForm->add(array(
Expand All @@ -205,11 +205,25 @@ public function testRenderWithBothInlineAndNoInlineRadios() {
'value_options' => array('label1','label2','label3'),
'inline' => false,
),
));
));

$this->assertStringEqualsFile($this->expectedPath . 'both-inline-and-no-inline-radios.phtml', $this->formRowHelper->__invoke($oForm->get('optInput1')).$this->formRowHelper->__invoke($oForm->get('optInput2')));
}

public function testAllowsFeedbackInTextField(){
$oForm = new \Zend\Form\Form();
$oForm->add(array(
'name' => 'username',
'type' => 'text',
'options' => array(
'label' => 'Your Username',
'feedback' => 'glyphicon glyphicon-user',
),
));

$this->assertStringEqualsFile($this->expectedPath . 'has-feedback-in-textfield.phtml', $this->formRowHelper->__invoke($oForm->get('username')));
}

/**
* @param string $sExpectedFile
* @param string $sActualString
Expand Down
1 change: 1 addition & 0 deletions tests/_files/expected-rows/has-feedback-in-textfield.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="form-group has-feedback"><label>Your Username</label><input type="text" name="username" class="form-control" value=""><i class="glyphicon glyphicon-user form-control-feedback"></i></div>

0 comments on commit 8799d13

Please sign in to comment.