mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENH: Display validation messages for GridFields (#10015)
* FIX: Display validation messages for GridFields * ENH: Add test for GridField validation messages.
This commit is contained in:
parent
3a852bed36
commit
49a7f08c07
@ -9,6 +9,7 @@ use SilverStripe\Control\HTTPRequest;
|
|||||||
use SilverStripe\Control\HTTPResponse;
|
use SilverStripe\Control\HTTPResponse;
|
||||||
use SilverStripe\Control\HTTPResponse_Exception;
|
use SilverStripe\Control\HTTPResponse_Exception;
|
||||||
use SilverStripe\Control\RequestHandler;
|
use SilverStripe\Control\RequestHandler;
|
||||||
|
use SilverStripe\Core\Convert;
|
||||||
use SilverStripe\Core\Injector\Injector;
|
use SilverStripe\Core\Injector\Injector;
|
||||||
use SilverStripe\Forms\Form;
|
use SilverStripe\Forms\Form;
|
||||||
use SilverStripe\Forms\FormField;
|
use SilverStripe\Forms\FormField;
|
||||||
@ -654,6 +655,18 @@ class GridField extends FormField
|
|||||||
$tableAttributes,
|
$tableAttributes,
|
||||||
$header . "\n" . $footer . "\n" . $body
|
$header . "\n" . $footer . "\n" . $body
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$message = Convert::raw2xml($this->getMessage());
|
||||||
|
if (is_array($message)) {
|
||||||
|
$message = $message['message'];
|
||||||
|
}
|
||||||
|
if ($message) {
|
||||||
|
$content['after'] .= HTML::createTag(
|
||||||
|
'p',
|
||||||
|
['class' => 'message ' . $this->getMessageType()],
|
||||||
|
$message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return HTML::createTag(
|
return HTML::createTag(
|
||||||
'fieldset',
|
'fieldset',
|
||||||
|
@ -20,6 +20,7 @@ use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
|
|||||||
use SilverStripe\Forms\GridField\GridState;
|
use SilverStripe\Forms\GridField\GridState;
|
||||||
use SilverStripe\Forms\GridField\GridState_Component;
|
use SilverStripe\Forms\GridField\GridState_Component;
|
||||||
use SilverStripe\Forms\GridField\GridState_Data;
|
use SilverStripe\Forms\GridField\GridState_Data;
|
||||||
|
use SilverStripe\Forms\RequiredFields;
|
||||||
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader;
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader;
|
||||||
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Component;
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Component;
|
||||||
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Component2;
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Component2;
|
||||||
@ -27,7 +28,9 @@ use SilverStripe\Forms\Tests\GridField\GridFieldTest\HTMLFragments;
|
|||||||
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Permissions;
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Permissions;
|
||||||
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Player;
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Player;
|
||||||
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
|
||||||
|
use SilverStripe\Forms\Tests\ValidatorTest\TestValidator;
|
||||||
use SilverStripe\ORM\ArrayList;
|
use SilverStripe\ORM\ArrayList;
|
||||||
|
use SilverStripe\ORM\ValidationResult;
|
||||||
use SilverStripe\Security\Group;
|
use SilverStripe\Security\Group;
|
||||||
use SilverStripe\Security\Member;
|
use SilverStripe\Security\Member;
|
||||||
use SilverStripe\Versioned\VersionedGridFieldStateExtension;
|
use SilverStripe\Versioned\VersionedGridFieldStateExtension;
|
||||||
@ -640,4 +643,27 @@ class GridFieldTest extends SapphireTest
|
|||||||
$endList = $gridField->getManipulatedList();
|
$endList = $gridField->getManipulatedList();
|
||||||
$this->assertEquals($endList->count(), 10);
|
$this->assertEquals($endList->count(), 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testValidationMessageInOutput()
|
||||||
|
{
|
||||||
|
$gridField = new GridField('testfield', 'testfield', new ArrayList(), new GridFieldConfig());
|
||||||
|
$fieldList = new FieldList([$gridField]);
|
||||||
|
$validator = new TestValidator();
|
||||||
|
$form = new Form(null, "testForm", $fieldList, new FieldList(), $validator);
|
||||||
|
|
||||||
|
// A form that fails validation should display the validation error in the FieldHolder output.
|
||||||
|
$form->validationResult();
|
||||||
|
$gridfieldOutput = $gridField->FieldHolder();
|
||||||
|
$this->assertContains('<p class="message ' . ValidationResult::TYPE_ERROR . '">error</p>', $gridfieldOutput);
|
||||||
|
|
||||||
|
// Clear validation error from previous assertion.
|
||||||
|
$validator->removeValidation();
|
||||||
|
$gridField->setMessage(null);
|
||||||
|
|
||||||
|
// A form that passes validation should not display a validation error in the FieldHolder output.
|
||||||
|
$form->setValidator(new RequiredFields());
|
||||||
|
$form->validationResult();
|
||||||
|
$gridfieldOutput = $gridField->FieldHolder();
|
||||||
|
$this->assertNotContains('<p class="message ' . ValidationResult::TYPE_ERROR . '">', $gridfieldOutput);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user