Jump to content

Possible to parse HTML in form helper


Adriano Faria

Recommended Posts

Posted

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:

image_uploaded_from_ios.jpg

But it shows like that in the form:

image_uploaded_from_ios.jpg

Is it possible to parse HTML in forms?

Posted

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.

Posted
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>

 

Posted
			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.

Posted
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.

Posted
'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.

Posted

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:

Capturar.png

Tks, @bfarber. :)

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...