FIX Ensure value is in the correct format after save#386
Conversation
5557390 to
418a05b
Compare
There was a problem hiding this comment.
The reason SiteConfig does this and other admin sections don't is that SiteConfigLeftAndMain (and CMSProfileController which presumably would have the same problem) don't use the response negotiator.
i.e the problem goes away if we make this change in SiteConfigLeftAndMain::save_siteconfig():
+ $this->setResponse($this->getResponseNegotiator()->respond($this->getRequest()));
$this->response->addHeader(
'X-Status',
rawurlencode(_t('SilverStripe\\Admin\\LeftAndMain.SAVEDUP', 'Saved.'))
);
- return $form->forTemplate();
+ return $this->getResponse();However, we then also need to update the validationResponseCallback defined in getEditForm. And that's still not the root cause - there's no sensible reason why rendering the form should result in adding extra brackets around the value. It speaks to a problem in the PHP code of the form field itself.
The root cause
The flow for most admin sections uses getResponseNegotiator() and renders the form fresh using getEditForm() rather than just rendering the form that came with the submission.
Calling getEditForm() fresh results in calling $form->loadDataFrom($record); which goes through MultiLinkField::setValue($value, $record) which loads the raw IDs from the relation, which do not have the '[]' brackets around the ID array.
The flow for SiteConfigLeftAndMain skips loading data from the record before rendering the form, which means it has the brackets around the value, because those brackets aren't filtered out when setSubmittedValue() is called on the field.
The fix
Add this method to MultiLinkField:
public function setSubmittedValue($value, $data = null)
{
$value = rtrim(ltrim($value, '['), ']');
return $this->setValue($value, $data);
}No need for additional javascript logic.
418a05b to
6b76021
Compare
|
Have updated to do it all in PHP. There was an existing method convertValueToArray() with unit tests to handle converting values so I just expanded on it |
6b76021 to
381a99a
Compare
|
#386 (comment) to be responded to |
381a99a to
f2b7fdc
Compare
Issue #385