Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX Show custom validation message for checkbox and radio groups
- Loading branch information
1 parent
46c6d93
commit 81fcd66
Showing
9 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace SilverStripe\UserForms\FormField; | ||
|
||
use SilverStripe\Forms\OptionsetField; | ||
use SilverStripe\UserForms\Model\EditableFormField; | ||
|
||
/** | ||
* @package userforms | ||
*/ | ||
class UserFormsOptionSetField extends OptionsetField | ||
{ | ||
|
||
/** | ||
* If your project uses a custom UserFormsCheckboxSetField.ss, ensure that it includes | ||
* `$Top.getValidationAttributesHTML().RAW` so that custom validation messages work | ||
* For further details see | ||
* templates/SilverStripe/UserForms/FormField/UserFormsCheckboxSetField.ss | ||
* | ||
* Use on a template with .RAW - single and double quoted strings will be safely escaped | ||
* | ||
* @return string | ||
* @see EditableFormField::updateFormField() | ||
*/ | ||
public function getValidationAttributesHTML() | ||
{ | ||
$attrs = array_filter(array_keys($this->getAttributes()), function ($attr) { | ||
return !in_array($attr, ['data-rule-required', 'data-msg-required']); | ||
}); | ||
return $this->getAttributesHTML(...$attrs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
templates/SilverStripe/UserForms/FormField/UserFormsOptionSetField.ss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
<% loop $Options %> | ||
<div class="$Class"> | ||
<input id="$ID" class="radio" name="$Name" type="radio" value="$Value.ATT"<% if $isChecked %> | ||
checked<% end_if %><% if $isDisabled %> disabled<% end_if %> <% if $Up.Required %>required<% end_if %> /> | ||
checked<% end_if %><% if $isDisabled %> disabled<% end_if %> <% if $Up.Required %>required<% end_if %> | ||
$Top.getValidationAttributesHTML().RAW /> | ||
<label for="$ID">$Title</label> | ||
</div> | ||
<% end_loop %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace SilverStripe\UserForms\Tests\FormField; | ||
|
||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\UserForms\FormField\UserFormsOptionSetField; | ||
use SilverStripe\UserForms\Model\EditableFormField\EditableRadioField; | ||
|
||
class UserFormsOptionSetFieldTest extends SapphireTest | ||
{ | ||
protected static $fixture_file = '../UserFormsTest.yml'; | ||
|
||
public function testCustomErrorMessageValidationAttributesHTML() | ||
{ | ||
/** @var UserFormsOptionSetField $userFormsOptionSetField */ | ||
$radio = $this->objFromFixture(EditableRadioField::class, 'radio-field'); | ||
$radio->Required = true; | ||
$radio->CustomErrorMessage = 'My custom error message with \'single\' and "double" quotes'; | ||
$userFormsOptionSetField = $radio->getFormField(); | ||
$html = $userFormsOptionSetField->renderWith(UserFormsOptionSetField::class)->getValue(); | ||
$attributesHTML = 'data-rule-required="true" data-msg-required="My custom error message with &#039;single&#039; and &quot;double&quot; quotes"'; | ||
$this->assertTrue(strpos($html, $attributesHTML) > 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters