Skip to content

Commit

Permalink
FIX Show custom validation message for checkbox and radio groups
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jul 21, 2020
1 parent 46c6d93 commit 81fcd66
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 6 deletions.
20 changes: 20 additions & 0 deletions code/FormField/UserFormsCheckboxSetField.php
Expand Up @@ -3,13 +3,33 @@
namespace SilverStripe\UserForms\FormField;

use SilverStripe\Forms\CheckboxSetField;
use SilverStripe\UserForms\Model\EditableFormField;

/**
* @package userforms
*/
class UserFormsCheckboxSetField extends CheckboxSetField
{

/**
* 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);
}

/**
* jQuery validate requires that the value of the option does not contain
* the actual value of the input.
Expand Down
32 changes: 32 additions & 0 deletions code/FormField/UserFormsOptionSetField.php
@@ -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);
}
}
6 changes: 3 additions & 3 deletions code/Model/EditableFormField/EditableRadioField.php
Expand Up @@ -2,7 +2,7 @@

namespace SilverStripe\UserForms\Model\EditableFormField;

use SilverStripe\Forms\OptionsetField;
use SilverStripe\UserForms\FormField\UserFormsOptionSetField;
use SilverStripe\UserForms\Model\EditableCustomRule;

/**
Expand Down Expand Up @@ -35,9 +35,9 @@ public function getCMSFields()

public function getFormField()
{
$field = OptionsetField::create($this->Name, $this->Title ?: false, $this->getOptionsMap())
$field = UserFormsOptionSetField::create($this->Name, $this->Title ?: false, $this->getOptionsMap())
->setFieldHolderTemplate(EditableMultipleOptionField::class . '_holder')
->setTemplate('SilverStripe\\UserForms\\FormField\\UserFormsOptionSetField');
->setTemplate(UserFormsOptionSetField::class);

// Set default item
$defaultOption = $this->getDefaultOptions()->first();
Expand Down
6 changes: 6 additions & 0 deletions docs/en/troubleshooting.md
Expand Up @@ -7,6 +7,12 @@ Check the below if you have any issues during installation or use
After installation make sure you have done a `dev/build` you may also need to flush the admin view by appending
`?flush=1` to the URL, e.g. `http://yoursite.com/admin?flush=1`

## Checkbox or Radio group custom messages not showing

If your project has a custom template for `UserFormsCheckboxSetField.ss` or `UserFormsOptionSetField.ss`, then you will need to ensure they include `$Top.getValidationAttributesHTML().RAW`. See
* [UserFormsCheckboxSetField.ss](../../templates/SilverStripe/UserForms/FormField/UserFormsCheckboxSetField.ss)
* [UserFormsOptionSetField.ss](../../templates/SilverStripe/UserForms/FormField/UserFormsOptionSetField.ss)

## UserForms EditableFormField Column Clean task

This task is used to clear unused columns from EditableFormField database tables.
Expand Down
Expand Up @@ -2,7 +2,8 @@
<% loop $Options %>
<div class="$Class">
<input id="$ID" class="checkbox" name="$Name" type="checkbox" value="$Value.ATT"<% if $isChecked %>
checked="checked"<% end_if %><% if $isDisabled %> disabled="disabled"<% end_if %> />
checked="checked"<% end_if %><% if $isDisabled %> disabled="disabled"<% end_if %>
$Top.getValidationAttributesHTML().RAW />
<label for="$ID">$Title.XML</label>
</div>
<% end_loop %>
Expand Down
@@ -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 %>
15 changes: 15 additions & 0 deletions tests/FormField/UserFormsCheckboxSetFieldTest.php
Expand Up @@ -5,9 +5,12 @@
use SilverStripe\Dev\SapphireTest;
use SilverStripe\UserForms\Form\UserFormsRequiredFields;
use SilverStripe\UserForms\FormField\UserFormsCheckboxSetField;
use SilverStripe\UserForms\Model\EditableFormField\EditableCheckboxGroupField;

class UserFormsCheckboxSetFieldTest extends SapphireTest
{
protected static $fixture_file = '../UserFormsTest.yml';

public function testValidate()
{
$field = new UserFormsCheckboxSetField('Field', 'My field', ['One' => 'One', 'Two' => 'Two']);
Expand All @@ -33,4 +36,16 @@ public function testValidate()
$field->setValue(array('Three', 'Four'));
$this->assertFalse($field->validate($validator));
}

public function testCustomErrorMessageValidationAttributesHTML()
{
/** @var EditableCheckboxGroupField $editableCheckboxGroupField */
$editableCheckboxGroupField = $this->objFromFixture(EditableCheckboxGroupField::class, 'checkbox-group');
$editableCheckboxGroupField->Required = true;
$editableCheckboxGroupField->CustomErrorMessage = 'My custom error message with \'single\' and "double" quotes';
$userFormsCheckboxSetField = $editableCheckboxGroupField->getFormField();
$html = $userFormsCheckboxSetField->renderWith(UserFormsCheckboxSetField::class)->getValue();
$attributesHTML = 'data-rule-required="true" data-msg-required="My custom error message with &amp;#039;single&amp;#039; and &amp;quot;double&amp;quot; quotes"';
$this->assertTrue(strpos($html, $attributesHTML) > 0);
}
}
24 changes: 24 additions & 0 deletions tests/FormField/UserFormsOptionSetFieldTest.php
@@ -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 &amp;#039;single&amp;#039; and &amp;quot;double&amp;quot; quotes"';
$this->assertTrue(strpos($html, $attributesHTML) > 0);
}
}
3 changes: 2 additions & 1 deletion tests/Model/EditableFormField/EditableRadioFieldTest.php
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\UserForms\Tests\Model\EditableFormField;

use SilverStripe\Dev\SapphireTest;
use SilverStripe\UserForms\FormField\UserFormsOptionSetField;
use SilverStripe\UserForms\Model\EditableFormField\EditableRadioField;

class EditableRadioFieldTest extends SapphireTest
Expand All @@ -16,7 +17,7 @@ public function testRenderedWithCustomTemplate()
{
$radio = $this->objFromFixture(EditableRadioField::class, 'radio-field');
$this->assertSame(
'SilverStripe\\UserForms\\FormField\\UserFormsOptionSetField',
UserFormsOptionSetField::class,
$radio->getFormField()->getTemplate()
);
}
Expand Down

0 comments on commit 81fcd66

Please sign in to comment.