Jump to content

IPS\Helpers\Form\Custom & Stack improvements


BomAle

Recommended Posts

Posted

I have tested some time this field but when try to get submitted values the result is not as expected.

//<?php
$type = new \IPS\Helpers\Form\Select( 'where[0][]', NULL, FALSE, array(
	'options' => array(
		'include' => 'include',
		'exclude' => 'exclude'
	)
) );

$location = new \IPS\Helpers\Form\Stack( 'where[1][]', array(), FALSE, array(), function($val){
	if ( $val )
	{
		foreach ( array_filter( is_array( $val ) ? $val : array( $val ) ) as $s )
		{
			try
			{
				$s = (mb_substr( $s, 0, mb_strlen( \IPS\Http\Url::baseUrl() ) ) === \IPS\Http\Url::baseUrl() OR filter_var( $s, FILTER_VALIDATE_URL ) !== FALSE) ? $s : \IPS\Http\Url::baseUrl() . $s;
				$url = \IPS\Http\Url::createFromString( str_replace('*','',$s), TRUE, TRUE );
				if ( !( $url instanceof \IPS\Http\Url\Internal) )
				{
					throw new \InvalidArgumentException( $s );
				}
			}
			catch ( \InvalidArgumentException $e )
			{
				throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack( 'ba_cta_popup_stack_err', FALSE, array( 'sprintf' => array( $e->getMessage() ) ) ) );
			}
		}
	}
});

$form->add( new \IPS\Helpers\Form\Custom( 'where', array(), FALSE, array(
	'getHtml'	=> function( $element ) use ($type,$location)
	{
		return $type->html() . $location->html();
	}
), NULL, NULL, NULL, 'where' ) );

if ( $values = $form->values() )
{
	echo "VAR_DUMP \$_REQUEST['where']";
	var_dump($_REQUEST['where']);
	echo "\n\n\nVAR DUMP \$values['where']";
	var_dump($values['where']);
	exit;
}

return $form;

 

custom_html.pngcustom.png

consider old bug reports:

@Matt  @Andy Millne @Ali Majrashi

Posted

I'm fairly certain the stack javascript is designed to swap out the input element's name array keys, however it is not designed to work with sub (or sub-sub, etc.) arrays.

In short, I'd stop trying to use a name like "where[1][]" and just use a unique form field name like "where_stack" or whatever. If that doesn't work please run the same test and let me know what you are seeing.

Posted

i don't remember but if i include input with different name (like where_first and where_two..) from Form\Custom name (where) the values submitted don't are present other different from "where" name.

Posted

custom_2_html.pngcustom_2_php.png

see code below.

//<?php
$type = new \IPS\Helpers\Form\Select( 'where_one', NULL, FALSE, array(
	'options' => array(
		'include' => 'include',
		'exclude' => 'exclude'
	)
) );

$location = new \IPS\Helpers\Form\Stack( 'where_two', array(), FALSE, array(), function($val){
	if ( $val )
	{
		foreach ( array_filter( is_array( $val ) ? $val : array( $val ) ) as $s )
		{
			try
			{
				$s = (mb_substr( $s, 0, mb_strlen( \IPS\Http\Url::baseUrl() ) ) === \IPS\Http\Url::baseUrl() OR filter_var( $s, FILTER_VALIDATE_URL ) !== FALSE) ? $s : \IPS\Http\Url::baseUrl() . $s;
				$url = \IPS\Http\Url::createFromString( str_replace('*','',$s), TRUE, TRUE );
				if ( !( $url instanceof \IPS\Http\Url\Internal) )
				{
					throw new \InvalidArgumentException( $s );
				}
			}
			catch ( \InvalidArgumentException $e )
			{
				throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack( 'ba_cta_popup_stack_err', FALSE, array( 'sprintf' => array( $e->getMessage() ) ) ) );
			}
		}
	}
});

$form->add( new \IPS\Helpers\Form\Custom( 'where', array(), FALSE, array(
	'getHtml'	=> function( $element ) use ($type,$location)
	{
		return $type->html() . $location->html();
	}
), NULL, NULL, NULL, 'where' ) );

if ( $values = $form->values() )
{
	echo "VAR_DUMP \$_REQUEST";
	var_dump($_REQUEST);
	echo "\n\n\nVAR DUMP \$values";
	var_dump($values);
	exit;
}

return $form;

Posted

Try this instead:

$form->add( new \IPS\Helpers\Form\Custom( 'where', array(), FALSE, array(
	'getHtml'	=> function( $element ) use ($type,$location)
	{
		return $type->html() . $location->html();
	},
	'formatValue' => function( $element ) use( $type, $location )
	{
		return array( 'where_one' => $type::stringValue( $type->value ), 'where_two' => $location::stringValue( $location->value ) );
	}
), NULL, NULL, NULL, 'where' ) );

2016-11-02_0836.png

 

Archived

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

  • Recently Browsing   0 members

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