mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
API CHANGE: Allow fieldList arguments to Form::loadDataFrom() and Form::saveInto(), for situations where the data passed only applies to a segment of the form.
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90872 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
6e23d7ebbb
commit
050b6057ce
@ -905,8 +905,10 @@ class Form extends RequestHandler {
|
|||||||
* a property or array-key of the passed {@link $data} argument are "left alone",
|
* a property or array-key of the passed {@link $data} argument are "left alone",
|
||||||
* meaning they retain any previous values (if present). If this flag is set to true,
|
* meaning they retain any previous values (if present). If this flag is set to true,
|
||||||
* those fields are overwritten with null regardless if they have a match in {@link $data}.
|
* those fields are overwritten with null regardless if they have a match in {@link $data}.
|
||||||
|
* @param $fieldList An optional list of fields to process. This can be useful when you have a
|
||||||
|
* form that has some fields that save to one object, and some that save to another.
|
||||||
*/
|
*/
|
||||||
function loadDataFrom($data, $clearMissingFields = false) {
|
function loadDataFrom($data, $clearMissingFields = false, $fieldList = null) {
|
||||||
if(!is_object($data) && !is_array($data)) {
|
if(!is_object($data) && !is_array($data)) {
|
||||||
user_error("Form::loadDataFrom() not passed an array or an object", E_USER_WARNING);
|
user_error("Form::loadDataFrom() not passed an array or an object", E_USER_WARNING);
|
||||||
return false;
|
return false;
|
||||||
@ -920,6 +922,9 @@ class Form extends RequestHandler {
|
|||||||
if($dataFields) foreach($dataFields as $field) {
|
if($dataFields) foreach($dataFields as $field) {
|
||||||
$name = $field->Name();
|
$name = $field->Name();
|
||||||
|
|
||||||
|
// Skip fields that have been exlcuded
|
||||||
|
if($fieldList && !in_array($name, $fieldList)) continue;
|
||||||
|
|
||||||
// First check looks for (fieldname)_unchanged, an indicator that we shouldn't overwrite the field value
|
// First check looks for (fieldname)_unchanged, an indicator that we shouldn't overwrite the field value
|
||||||
if(is_array($data) && isset($data[$name . '_unchanged'])) continue;
|
if(is_array($data) && isset($data[$name . '_unchanged'])) continue;
|
||||||
|
|
||||||
@ -961,12 +966,20 @@ class Form extends RequestHandler {
|
|||||||
/**
|
/**
|
||||||
* Save the contents of this form into the given data object.
|
* Save the contents of this form into the given data object.
|
||||||
* It will make use of setCastedField() to do this.
|
* It will make use of setCastedField() to do this.
|
||||||
|
*
|
||||||
|
* @param $dataObject The object to save data into
|
||||||
|
* @param $fieldList An optional list of fields to process. This can be useful when you have a
|
||||||
|
* form that has some fields that save to one object, and some that save to another.
|
||||||
*/
|
*/
|
||||||
function saveInto(DataObjectInterface $dataObject) {
|
function saveInto(DataObjectInterface $dataObject, $fieldList = null) {
|
||||||
$dataFields = $this->fields->saveableFields();
|
$dataFields = $this->fields->saveableFields();
|
||||||
$lastField = null;
|
$lastField = null;
|
||||||
|
|
||||||
if($dataFields) foreach($dataFields as $field) {
|
if($dataFields) foreach($dataFields as $field) {
|
||||||
|
// Skip fields that have been exlcuded
|
||||||
|
if($fieldList && !in_array($field->Name(), $fieldList)) continue;
|
||||||
|
|
||||||
|
|
||||||
$saveMethod = "save{$field->Name()}";
|
$saveMethod = "save{$field->Name()}";
|
||||||
|
|
||||||
if($field->Name() == "ClassName"){
|
if($field->Name() == "ClassName"){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user