Adriano Faria Posted September 1, 2017 Posted September 1, 2017 I have a couple of apps that uses questions/answers. Currently I'm using textarea, which has no issue but a user requested to change it to textarea for formatting purposes. It shows fine in another template: But it shows like that in the form: Is it possible to parse HTML in forms?
bfarber Posted September 5, 2017 Posted September 5, 2017 Is that a Radio form field? If so, the template already uses the |raw modifier. <label for='elRadio_{$name}_{$k}_{$htmlId}' id='elField_{$name}_label'>{$v|raw}</label> What are you passing as the 'parse' option to the form helper? Sounds like you need to pass this option set to 'raw' to prevent the values you send in for the options from being escaped.
Adriano Faria Posted September 5, 2017 Author Posted September 5, 2017 24 minutes ago, bfarber said: Is that a Radio form field? If so, the template already uses the |raw modifier. <label for='elRadio_{$name}_{$k}_{$htmlId}' id='elField_{$name}_label'>{$v|raw}</label> What are you passing as the 'parse' option to the form helper? Sounds like you need to pass this option set to 'raw' to prevent the values you send in for the options from being escaped. Like that: $form->add( new \IPS\Helpers\Form\Radio( 'answer_' . $i, isset( $data['answer_' . $i ] ) ? $data['answer_' . $i ] : '', TRUE, array( 'options' => $answers, 'parse' => 'normal' ) ) ); Answers: /* Merge wrong and correct answers together */ $answers = $this->question->getAnswers( $question['question_id'] ); /* Shuffle them up */ shuffle( $answers ); /* Give each answer a "unique" ID */ $temp = array(); foreach( $answers as $answer ) { $temp[ md5( $answer['answer_title'] ) ] = $answer['answer_title']; } $answers = $temp; and it goes to the template: return \IPS\Theme::i()->getTemplate( 'view' )->startForm( $form, $data, $this->question, $question['question_id'] ); And: <div data-role="wizardContent"> {$form|raw} </div>
bfarber Posted September 6, 2017 Posted September 6, 2017 case 'normal': foreach ( $this->options['options'] as $k => $v ) { if ( is_array( $v ) ) { foreach ( $v as $x => $y ) { $options[ $k ][ $x ] = htmlspecialchars( $y, \IPS\HTMLENTITIES, 'UTF-8', FALSE ); } } else { $options[ $k ] = htmlspecialchars( $v, \IPS\HTMLENTITIES, 'UTF-8', FALSE ); } } break; You are passing parse as 'normal', which runs the value through htmlspecialchars(). That's why I suggested 'raw' in my last reply.
Adriano Faria Posted September 6, 2017 Author Posted September 6, 2017 20 minutes ago, bfarber said: case 'normal': foreach ( $this->options['options'] as $k => $v ) { if ( is_array( $v ) ) { foreach ( $v as $x => $y ) { $options[ $k ][ $x ] = htmlspecialchars( $y, \IPS\HTMLENTITIES, 'UTF-8', FALSE ); } } else { $options[ $k ] = htmlspecialchars( $v, \IPS\HTMLENTITIES, 'UTF-8', FALSE ); } } break; You are passing parse as 'normal', which runs the value through htmlspecialchars(). That's why I suggested 'raw' in my last reply. Not sure I follow. I'm parsing NORMAL, ok: array( 'options' => $answers, 'parse' => 'normal' ) So I removed it or changed it to 'none' as appears in other places. Same result. In IPS\Helpers\Form\Select there's only NORMAL and LANG for the options. Not sure what I have to do.
bfarber Posted September 6, 2017 Posted September 6, 2017 'parse' => 'lang', // Sets how the values for options should be parsed. Acceptable values are "lang" (language keys), "normal" (htmlentities), "raw" (no parsing) or "image" (image URLs, only for Radio fields). Default is "lang". Without testing further though it's hard to say from here. On the surface, it seems that passing 'raw' as the parse option should accomplish what you are after. If it doesn't, I would start digging to find out why.
Adriano Faria Posted September 6, 2017 Author Posted September 6, 2017 Damn, I only read the method itself; didn't see the option at the top: $array = array( 1 => "<span style='font-size:200%;color:blue;'><strong>1111</strong></span>", 2 => 2222, 3 => 3333 ); foreach( $array as $k => $v ) { $options[ $k ] = $v; } $form->add( new \IPS\Helpers\Form\Radio( 'answer', '', TRUE, array( 'options' => $options, 'parse' => 'raw' ) ) ); Quick test: Tks, @bfarber.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.