Richard Jedlička Posted December 12, 2021 Share Posted December 12, 2021 Hi, in my project one page has a text field with a regext pattern. The field accepts signed (+/-) number and can contain spaces between digits. I would like to remove the spaces after the form is submitted. What is the good approach to do that? I have few ideas: Leave it be as it is and format the value on output. ☹️ Create new fieltype/inputfield with this funcionality. ? Hook processInput method of the Inputfield and remove the spaces. ? I would prefer the 3. option. But what about if I want to have it configurable (to remove spaces or more complex stuff), without creating new fieldtype/inputfield? Is it good to hook getConfigInputfields method? Is it enought to just append some input for it or do I have do some more logic somewhere? What would be your approach, and are there some other modules extending fieldtypes and inputfield the similar way? Thanks Link to comment Share on other sites More sharing options...
horst Posted December 12, 2021 Share Posted December 12, 2021 textformatter module 1 Link to comment Share on other sites More sharing options...
Richard Jedlička Posted December 12, 2021 Author Share Posted December 12, 2021 2 hours ago, horst said: textformatter module yes, but it affect only the output in a template, right? Or also the value which is filled in the input when editing page? Link to comment Share on other sites More sharing options...
Jan Romero Posted December 12, 2021 Share Posted December 12, 2021 Hi! If you want to accept any numeric value, you can just use Fieldtype Integer, Float or Decimal. As long as you don’t choose the HTML5 input, it will accept spaces and strip them when saving the page. If you still need further regex validation, it may be interesting to add regex validation to the numeric field instead of using a text field and dealing with the spaces. If you want to use Fieldtype Text, your idea of hooking ProcessInput seems good. Perhaps also look into hooking Fieldtype::sanitizeValue instead. Or maybe Fieldtype::sleepValue. All of these options will remove the spaces once when saving and then never display them again. If you want to show the spaces to editors for readability, consider horst’s idea or (assuming they’re deterministic like thousands separators) put them back in somehow. Fieldtype::wakeupValue will work, I imagine. Fieldtype::formatValue wouldn’t be the right choice here, because, as you noted, it only formats for output on the page, not for input fields in the page editor. 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now