Skip to content

Commit b1367de

Browse files
committed
Added error css class to input when has error
1 parent ac31b6b commit b1367de

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

src/Fields/BoundField.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,14 @@ protected function getValue()
121121

122122
private function buildWidgetAttrs(array $attrs = array())
123123
{
124-
$css_classes = implode(" ", $this->form->getCssClasses());
124+
$css_classes = $this->form->getCssClasses();
125+
126+
if ($this->has_errors) {
127+
$css_classes[] = $this->form->getErrorCssClass();
128+
}
125129

126130
if (!empty($css_classes)) {
127-
$attrs['class'] = $css_classes;
131+
$attrs['class'] = implode(" ", $css_classes);
128132
}
129133

130134
if ($this->field->isRequired()) {

src/Forms/Form.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ abstract class Form implements ArrayAccess, Iterator, Countable
4949
*/
5050
protected $css_classes = array();
5151

52+
/**
53+
* Cass classes to be added to all widgets.
54+
* @var array
55+
*/
56+
protected $error_css_class = 'is-invalid';
57+
5258
/**
5359
* Fields declared to this form.
5460
* @var array
@@ -104,6 +110,15 @@ public function getCssClasses()
104110
return $this->css_classes;
105111
}
106112

113+
/**
114+
* Return error css class to be added on case of field error.
115+
* @return array
116+
*/
117+
public function getErrorCssClass()
118+
{
119+
return $this->error_css_class;
120+
}
121+
107122
/**
108123
* Special method to make errors accessible as a attribute.
109124
* @param string $name Attribute name.

src/Widgets/Widget.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,6 @@ public function valueFromData($data, $files, string $name)
9696
return array_key_exists($name, $data) ? $data[$name] : null;
9797
}
9898

99-
/**
100-
* Return css classes to be added to each widget.
101-
* @return string
102-
*/
103-
private function buildCssClasses()
104-
{
105-
return implode(" ", $this->css_classes);
106-
}
107-
10899
/**
109100
* Return defined subwidget.
110101
*

tests/unit/Fields/BoundFieldTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,23 @@ public function testToStringWithCssClasses()
106106
$this->assertXmlStringEqualsXmlString($expected, (string) $bound);
107107
}
108108

109+
public function testToStringWithErrors()
110+
{
111+
$form = $this->getMockBuilder(Form::class)
112+
->setMethods(array('hasErrors'))
113+
->getMockForAbstractClass();
114+
115+
$form->method('hasErrors')
116+
->will($this->returnValue(true));
117+
118+
$field = new CharField(["required" => true]);
119+
$bound = new BoundField($form, $field, "name");
120+
121+
$expected = '<input type="text" id="id_name" name="name" required="required" class="is-invalid"/>';
122+
123+
$this->assertXmlStringEqualsXmlString($expected, (string) $bound);
124+
}
125+
109126
public function testLabelTag()
110127
{
111128
$field = new CharField(array("label" => "Label"));

tests/unit/Fields/FieldTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testValidateWithRequiredValueEmpty()
4545
{
4646
$args = array("required" => true);
4747
$stub = $this->getMockForAbstractClass(Field::class, array($args));
48-
$this->assertNull($stub->validate(""));
48+
$stub->validate(null);
4949
}
5050

5151
public function testRunValidatorsWithEmptyValue()

0 commit comments

Comments
 (0)