在 Silverstripe 中将 JSON 编码的字符串格式化为更易于阅读的格式
Format JSON encoded string into more human readable in Silverstripe
在 Silverstripe 网站中,用户提交了一个表单,该表单存储在 CMS 上,供内容管理员查看提交的表单。
该功能有效,但问题是我在 PHP 数组中返回了 $data
,我想将它输出到 CMS。
我想出的唯一方法是将它转换为 JSON,但它只是输出一个 JSON 字符串,我想要 HTML table 以使其更易于阅读。我该怎么做?
到目前为止我的代码是:
// converts array to jason, on controller
$SubmitedResult->SerialisedForm = Convert::array2json($data);
// $db on dataobject
private static $db = array(
'SerialisedForm' => 'Text',
);
// JSON string received below
{"url":"\/test\/test-test\/testSubmit","Name":"Tom","Email":"tom@gmail.com","Phone":"564456","SecurityID":"c5efe841e26d6d088dd94dfcfe76f6ec80acac86","action_submit":"Submit"}
通常您希望构建一个 DataObject
来存储您提交的表单数据。看起来您已经有了它,但是您使用它来将所有数据存储在一个名为 SerialisedForm
的字段中。我建议您改为为所有表单字段创建一个单独的字段。
示例:
class FormSubmission extends DataObject
{
private static $db = [
'Name' => 'Varchar(255)',
'Email' => 'Varchar(255)',
'Phone' => 'Varchar(64)'
];
// The summary_fields ensure that your fields directly show up in the GridField
private static $summary_fields = [
'Name' => 'Name',
'Email' => 'Email',
'Phone' => 'Phone'
];
}
然后在您的表单提交处理程序中,您执行:
public function testSubmit($data, $form)
{
$submittedResult = FormSubmission::create();
$form->saveInto($submittedResult);
$submittedResult->write();
// redirect back or somewhere else…
}
在 CMS 中,您可以在主页上使用 GridField
或 ModelAdmin
实例来查看提交内容。
在 Silverstripe 网站中,用户提交了一个表单,该表单存储在 CMS 上,供内容管理员查看提交的表单。
该功能有效,但问题是我在 PHP 数组中返回了 $data
,我想将它输出到 CMS。
我想出的唯一方法是将它转换为 JSON,但它只是输出一个 JSON 字符串,我想要 HTML table 以使其更易于阅读。我该怎么做?
到目前为止我的代码是:
// converts array to jason, on controller
$SubmitedResult->SerialisedForm = Convert::array2json($data);
// $db on dataobject
private static $db = array(
'SerialisedForm' => 'Text',
);
// JSON string received below
{"url":"\/test\/test-test\/testSubmit","Name":"Tom","Email":"tom@gmail.com","Phone":"564456","SecurityID":"c5efe841e26d6d088dd94dfcfe76f6ec80acac86","action_submit":"Submit"}
通常您希望构建一个 DataObject
来存储您提交的表单数据。看起来您已经有了它,但是您使用它来将所有数据存储在一个名为 SerialisedForm
的字段中。我建议您改为为所有表单字段创建一个单独的字段。
示例:
class FormSubmission extends DataObject
{
private static $db = [
'Name' => 'Varchar(255)',
'Email' => 'Varchar(255)',
'Phone' => 'Varchar(64)'
];
// The summary_fields ensure that your fields directly show up in the GridField
private static $summary_fields = [
'Name' => 'Name',
'Email' => 'Email',
'Phone' => 'Phone'
];
}
然后在您的表单提交处理程序中,您执行:
public function testSubmit($data, $form)
{
$submittedResult = FormSubmission::create();
$form->saveInto($submittedResult);
$submittedResult->write();
// redirect back or somewhere else…
}
在 CMS 中,您可以在主页上使用 GridField
或 ModelAdmin
实例来查看提交内容。