douhu2370 2015-10-24 18:38
浏览 66

Silverstripe HTML字段验证

I am trying to figure out how to allow users to fill out their "About" field in their profile for the website I am making. The biggest drawback is that I want to allow users to be able to format their text like you can format it when you go to post on a forum, or the way Silverstripe users can format it when you go to admin panel.

I have tried making use of the HTML field that is in silverstripe. When I added the box it gave you this really bext text box that accepts anything, html, javscript, etc...

This was nice and dandy up to the point that I realized two twings. With the HTML field, there doesn't come any form of editor that users can use. They can't bold, change fonts, sizes, etc... The other problem is the fact that I entered some javascript in there and clicked save. Next time I open the page I was greeted with my javascript alerts I entered in the HTML field.

So here are my two questions:

How can I give the user an editor (of any kind) that will allow them to shape their about section.

How can I validate fields that allow you to do what I am asking in the first question? I am quite new to validating such inputs and I have been searching for quite some times with little to no answers.

EDIT:

I forgot to include that I did find this guide: https://docs.silverstripe.org/en/3.1/developer_guides/forms/field_types/htmleditorfield/

From what I did see here is how to include this on the back end (Admin Console) I can't though figure out how to include this on lets say AboutMe page. So www.mywebsite.com/AboutMe/edit/1

EDIT2:

Further research has led me a solution that allows me to showup the TinyMCE editor on my page. The document I listed above did not say that the init function was missing and I need to add it to the page in order to get the editor to work. Here is what was missing:

<script type="text/javascript">

tinyMCE.init({
theme : "advanced",
mode: "textareas", 
theme_advanced_toolbar_location : "top",
theme_advanced_buttons1 : "bold,italic,underline,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,outdent,indent,separator,undo,redo",
theme_advanced_buttons2 : "forecolor",
theme_advanced_buttons3 : "",
height:"250px",
width:"400px"
});

</script>

My second question though still stands. How do I validate all the text that comes in here? The TinyMCE will do some validation for me via javascript but we can always find a way around that. What kind of back-end (php) validation would I do to ensure that the text entered is safe?

The only thing that I can think of is create a white list of every combination of characters allowed, but that just seems a little too wrong.

  • 写回答

1条回答 默认 最新

  • douxing6434 2015-11-06 05:03
    关注

    A couple things come to mind.

    I know you've already gotten this far, but you could try extending the HTMLEditorField and customize the field the way you want with its own TinyMCE config.

    As for the validation, you could use onBeforeWrite() on your DataObject (or whatever you're saving the unsanitized HTML to) to cleanse the input. Possibly use a library for this, or if you're just blacklisting tags you could work with PHP's strip_tags function.

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入