'val1',
'namespace' => [
'key2' => 'val2',
'key3' => [
'key4' => 'val4',
]
],
'othernamespace' => [
'key5' => [
'key6' =>[
'key7' => 'val7'
]
]
]
];
$form->loadDataFrom($requestData);
$fields = $form->Fields();
$this->assertEquals('val1', $fields->fieldByName('key1')->Value());
$this->assertEquals('val2', $fields->fieldByName('namespace[key2]')->Value());
$this->assertEquals('val4', $fields->fieldByName('namespace[key3][key4]')->Value());
$this->assertEquals('val7', $fields->fieldByName('othernamespace[key5][key6][key7]')->Value());
}
public function testSubmitReadonlyFields()
{
$this->get('FormTest_Controller');
// Submitting a value for a readonly field should be ignored
$response = $this->post(
'FormTest_Controller/Form',
[
'Email' => 'invalid',
'Number' => '888',
'ReadonlyField' => ''
// leaving out "Required" field
]
);
// Number field updates its value
$this->assertContains('getBody());
// Readonly field remains
$this->assertContains(
'getBody()
);
$this->assertNotContains('hacxzored', $response->getBody());
}
public function testLoadDataFromUnchangedHandling()
{
$form = new Form(
Controller::curr(),
'Form',
new FieldList(
new TextField('key1'),
new TextField('key2')
),
new FieldList()
);
$form->loadDataFrom(
[
'key1' => 'save',
'key2' => 'dontsave',
'key2_unchanged' => '1'
]
);
$this->assertEquals(
$form->getData(),
[
'key1' => 'save',
'key2' => null,
],
'loadDataFrom() doesnt save a field if a matching "_unchanged" flag is set'
);
}
public function testLoadDataFromObject()
{
$form = new Form(
Controller::curr(),
'Form',
new FieldList(
new HeaderField('MyPlayerHeader', 'My Player'),
new TextField('Name'), // appears in both Player and Team
new TextareaField('Biography'),
new DateField('Birthday'),
new NumericField('BirthdayYear') // dynamic property
),
new FieldList()
);
$captainWithDetails = $this->objFromFixture(Player::class, 'captainWithDetails');
$form->loadDataFrom($captainWithDetails);
$this->assertEquals(
$form->getData(),
[
'Name' => 'Captain Details',
'Biography' => 'Bio 1',
'Birthday' => '1982-01-01',
'BirthdayYear' => '1982',
],
'LoadDataFrom() loads simple fields and dynamic getters'
);
$captainNoDetails = $this->objFromFixture(Player::class, 'captainNoDetails');
$form->loadDataFrom($captainNoDetails);
$this->assertEquals(
$form->getData(),
[
'Name' => 'Captain No Details',
'Biography' => null,
'Birthday' => null,
'BirthdayYear' => 0,
],
'LoadNonBlankDataFrom() loads only fields with values, and doesnt overwrite existing values'
);
}
public function testLoadDataFromClearMissingFields()
{
$form = new Form(
Controller::curr(),
'Form',
new FieldList(
new HeaderField('MyPlayerHeader', 'My Player'),
new TextField('Name'), // appears in both Player and Team
new TextareaField('Biography'),
new DateField('Birthday'),
new NumericField('BirthdayYear'), // dynamic property
$unrelatedField = new TextField('UnrelatedFormField')
//new CheckboxSetField('Teams') // relation editing
),
new FieldList()
);
$unrelatedField->setValue("random value");
$captainWithDetails = $this->objFromFixture(Player::class, 'captainWithDetails');
$form->loadDataFrom($captainWithDetails);
$this->assertEquals(
$form->getData(),
[
'Name' => 'Captain Details',
'Biography' => 'Bio 1',
'Birthday' => '1982-01-01',
'BirthdayYear' => '1982',
'UnrelatedFormField' => 'random value',
],
'LoadDataFrom() doesnt overwrite fields not found in the object'
);
$captainWithDetails = $this->objFromFixture(Player::class, 'captainNoDetails');
$team2 = $this->objFromFixture(Team::class, 'team2');
$form->loadDataFrom($captainWithDetails);
$form->loadDataFrom($team2, Form::MERGE_CLEAR_MISSING);
$this->assertEquals(
$form->getData(),
[
'Name' => 'Team 2',
'Biography' => '',
'Birthday' => '',
'BirthdayYear' => 0,
'UnrelatedFormField' => null,
],
'LoadDataFrom() overwrites fields not found in the object with $clearMissingFields=true'
);
}
public function testLoadDataFromWithForceSetValueFlag()
{
// Get our data formatted in internal value and in submitted value
// We're using very esoteric date and time format
$dataInSubmittedValue = [
'SomeDateTimeField' => 'Fri, Jun 15, \'18 17:28:05',
'SomeTimeField' => '05 o\'clock PM 28 05'
];
$dataInInternalValue = [
'SomeDateTimeField' => '2018-06-15 17:28:05',
'SomeTimeField' => '17:28:05'
];
// Test loading our data with the MERGE_AS_INTERNAL_VALUE
$form = $this->getStubFormWithWeirdValueFormat();
$form->loadDataFrom($dataInInternalValue, Form::MERGE_AS_INTERNAL_VALUE);
$this->assertEquals(
$dataInInternalValue,
$form->getData()
);
// Test loading our data with the MERGE_AS_SUBMITTED_VALUE and an data passed as an object
$form = $this->getStubFormWithWeirdValueFormat();
$form->loadDataFrom(ArrayData::create($dataInSubmittedValue), Form::MERGE_AS_SUBMITTED_VALUE);
$this->assertEquals(
$dataInInternalValue,
$form->getData()
);
// Test loading our data without the MERGE_AS_INTERNAL_VALUE and without MERGE_AS_SUBMITTED_VALUE
$form = $this->getStubFormWithWeirdValueFormat();
$form->loadDataFrom($dataInSubmittedValue);
$this->assertEquals(
$dataInInternalValue,
$form->getData()
);
}
public function testLookupFieldDisabledSaving()
{
$object = new Team();
$form = new Form(
Controller::curr(),
'Form',
new FieldList(
new LookupField('Players', 'Players')
),
new FieldList()
);
$form->loadDataFrom(
[
'Players' => [
14,
18,
22
],
]
);
$form->saveInto($object);
$playersIds = $object->Players()->getIDList();
$this->assertTrue($form->validationResult()->isValid());
$this->assertEquals(
$playersIds,
[],
'saveInto() should not save into the DataObject for the LookupField'
);
}
public function testLoadDataFromIgnoreFalseish()
{
$form = new Form(
Controller::curr(),
'Form',
new FieldList(
new TextField('Biography', 'Biography', 'Custom Default')
),
new FieldList()
);
$captainNoDetails = $this->objFromFixture(Player::class, 'captainNoDetails');
$captainWithDetails = $this->objFromFixture(Player::class, 'captainWithDetails');
$form->loadDataFrom($captainNoDetails, Form::MERGE_IGNORE_FALSEISH);
$this->assertEquals(
$form->getData(),
['Biography' => 'Custom Default'],
'LoadDataFrom() doesn\'t overwrite fields when MERGE_IGNORE_FALSEISH set and values are false-ish'
);
$form->loadDataFrom($captainWithDetails, Form::MERGE_IGNORE_FALSEISH);
$this->assertEquals(
$form->getData(),
['Biography' => 'Bio 1'],
'LoadDataFrom() does overwrite fields when MERGE_IGNORE_FALSEISH set and values arent false-ish'
);
}
public function testFormMethodOverride()
{
$form = $this->getStubForm();
$form->setFormMethod('GET');
$this->assertNull($form->Fields()->dataFieldByName('_method'));
$form = $this->getStubForm();
$form->setFormMethod('PUT');
$this->assertEquals(
$form->Fields()->dataFieldByName('_method')->Value(),
'PUT',
'PUT override in forms has PUT in hiddenfield'
);
$this->assertEquals(
$form->FormMethod(),
'POST',
'PUT override in forms has POST in