Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Add a way to check if a form or field has an extra css class. #10112

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,25 @@ public function extraClass()
return implode(' ', array_unique($this->extraClasses));
}

/**
* Check if a CSS-class has been added to the form container.
*
* @param string $class A string containing a classname or several class
* names delimited by a single space.
* @return boolean True if all of the classnames passed in have been added.
*/
public function hasExtraClass($class)
{
//split at white space
$classes = preg_split('/\s+/', $class);
foreach ($classes as $class) {
if (!isset($this->extraClasses[$class])) {
return false;
}
}
return true;
}

/**
* Add a CSS-class to the form-container. If needed, multiple classes can
* be added by delimiting a string with spaces.
Expand Down
19 changes: 19 additions & 0 deletions src/Forms/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,25 @@ public function extraClass()
return implode(' ', $classes);
}

/**
* Check if a CSS-class has been added to the form container.
*
* @param string $class A string containing a classname or several class
* names delimited by a single space.
* @return boolean True if all of the classnames passed in have been added.
*/
public function hasExtraClass($class)
{
//split at white space
$classes = preg_split('/\s+/', $class);
foreach ($classes as $class) {
if (!isset($this->extraClasses[$class])) {
return false;
}
}
return true;
}

/**
* Add one or more CSS-classes to the FormField container.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/php/Forms/FormFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ public function testAddExtraClass()
$this->assertStringEndsWith('class1 class2', $field->extraClass());
}

public function testHasExtraClass()
{
$field = new FormField('MyField');
$field->addExtraClass('class1');
$field->addExtraClass('class2');
$this->assertTrue($field->hasExtraClass('class1'));
$this->assertTrue($field->hasExtraClass('class2'));
$this->assertTrue($field->hasExtraClass('class1 class2'));
$this->assertTrue($field->hasExtraClass('class2 class1'));
$this->assertFalse($field->hasExtraClass('class3'));
$this->assertFalse($field->hasExtraClass('class2 class3'));
}

public function testRemoveExtraClass()
{
$field = new FormField('MyField');
Expand Down
13 changes: 13 additions & 0 deletions tests/php/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,19 @@ public function testAddExtraClass()
$this->assertStringEndsWith('class1 class2', $form->extraClass());
}

public function testHasExtraClass()
{
$form = $this->getStubForm();
$form->addExtraClass('class1');
$form->addExtraClass('class2');
$this->assertTrue($form->hasExtraClass('class1'));
$this->assertTrue($form->hasExtraClass('class2'));
$this->assertTrue($form->hasExtraClass('class1 class2'));
$this->assertTrue($form->hasExtraClass('class2 class1'));
$this->assertFalse($form->hasExtraClass('class3'));
michalkleiner marked this conversation as resolved.
Show resolved Hide resolved
$this->assertFalse($form->hasExtraClass('class2 class3'));
}

public function testRemoveExtraClass()
{
$form = $this->getStubForm();
Expand Down