mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
6e589aac75
API Implement form schema "errors" handling
89 lines
2.6 KiB
PHP
89 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
use SilverStripe\Dev\SapphireTest;
|
|
use SilverStripe\Forms\OptionsetField;
|
|
use SilverStripe\Forms\RequiredFields;
|
|
use SilverStripe\Forms\FieldList;
|
|
use SilverStripe\Forms\Form;
|
|
|
|
class OptionsetFieldTest extends SapphireTest {
|
|
public function testSetDisabledItems() {
|
|
$f = new OptionsetField(
|
|
'Test',
|
|
false,
|
|
array(0 => 'Zero', 1 => 'One')
|
|
);
|
|
|
|
$f->setDisabledItems(array(0));
|
|
$p = new CSSContentParser($f->Field());
|
|
$item0 = $p->getBySelector('#Test_0');
|
|
$item1 = $p->getBySelector('#Test_1');
|
|
$this->assertEquals(
|
|
(string)$item0[0]['disabled'],
|
|
'disabled'
|
|
);
|
|
$this->assertEquals(
|
|
(string)$item1[0]['disabled'],
|
|
''
|
|
);
|
|
}
|
|
|
|
public function testValidation() {
|
|
$field = OptionsetField::create('Test', 'Testing', array(
|
|
"One" => "One",
|
|
"Two" => "Two",
|
|
"Five" => "Five"
|
|
));
|
|
$validator = new RequiredFields('Test');
|
|
/** @skipUpgrade */
|
|
$form = new Form($this, 'Form', new FieldList($field), new FieldList(), $validator);
|
|
|
|
$field->setValue("One");
|
|
$this->assertTrue($field->validate($validator));
|
|
|
|
//non-existent value should make the field invalid
|
|
$field->setValue("Three");
|
|
$this->assertFalse($field->validate($validator));
|
|
|
|
//empty string should pass field-level validation...
|
|
$field->setValue('');
|
|
$this->assertTrue($field->validate($validator));
|
|
|
|
// ... but should not pass "RequiredFields" validation
|
|
$this->assertFalse($form->validationResult()->isValid());
|
|
|
|
//disabled items shouldn't validate
|
|
$field->setDisabledItems(array('Five'));
|
|
$field->setValue('Five');
|
|
$this->assertFalse($field->validate($validator));
|
|
}
|
|
|
|
public function testReadonlyField() {
|
|
$sourceArray = array(0 => 'No', 1 => 'Yes');
|
|
$field = new OptionsetField('FeelingOk', 'are you feeling ok?', $sourceArray, 1);
|
|
$field->setEmptyString('(Select one)');
|
|
$field->setValue(1);
|
|
$readonlyField = $field->performReadonlyTransformation();
|
|
preg_match('/Yes/', $readonlyField->Field(), $matches);
|
|
$this->assertEquals($matches[0], 'Yes');
|
|
}
|
|
|
|
public function testSafelyCast() {
|
|
$field1 = new OptionsetField('Options', 'Options', array(
|
|
1 => 'One',
|
|
2 => 'Two & Three',
|
|
3 => DBField::create_field('HTMLText', 'Four & Five & Six')
|
|
));
|
|
$fieldHTML = (string)$field1->Field();
|
|
$this->assertContains('One', $fieldHTML);
|
|
$this->assertContains('Two & Three', $fieldHTML);
|
|
$this->assertNotContains('Two & Three', $fieldHTML);
|
|
$this->assertContains('Four & Five & Six', $fieldHTML);
|
|
$this->assertNotContains('Four & Five & Six', $fieldHTML);
|
|
}
|
|
}
|