Skip to content

Instantly share code, notes, and snippets.

@sminnee
Created November 5, 2009 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sminnee/226608 to your computer and use it in GitHub Desktop.
Save sminnee/226608 to your computer and use it in GitHub Desktop.
Index: sapphire/forms/Form.php
===================================================================
--- sapphire/forms/Form.php (revision 90851)
+++ sapphire/forms/Form.php (working copy)
@@ -905,8 +905,10 @@
* 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,
* 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)) {
user_error("Form::loadDataFrom() not passed an array or an object", E_USER_WARNING);
return false;
@@ -920,6 +922,9 @@
if($dataFields) foreach($dataFields as $field) {
$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
if(is_array($data) && isset($data[$name . '_unchanged'])) continue;
@@ -961,12 +966,20 @@
/**
* Save the contents of this form into the given data object.
* 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();
$lastField = null;
if($dataFields) foreach($dataFields as $field) {
+ // Skip fields that have been exlcuded
+ if(!$fieldList && !in_array($field->Name(), $fieldList)) continue;
+
+
$saveMethod = "save{$field->Name()}";
if($field->Name() == "ClassName"){
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment