Jump to content

Forms.php 1.0.0

   (0 reviews)
Sign in to follow this  

IPBDesign

2 Screenshots

About This File

Any one who follows me over on IPS knows I have had many complaints about the forms and form helpers. Tired of sounds of crickets chirping while waiting for IPS to make improvements or take in considerations from the community, I built this class file for applications. 

 

Usage:

\IPS\myapp\Forms::i( $elements, $instancename, $form ) 

( after $form, all the parameters are the same that you would find for \IPS\Helpers\Form\__construct method, but most of the time you will only need to set the $elements, $instancename is if you have multiple forms on the same page).

$elements:

tab, header, sidebar:

'type' = tab|header|sidebar
'name' =  used for all these to create them, if it is a lang/string, this is what is passed to the appropriate form command.

example:

$el[] = [
	'type' => 'tab|header|sidebar',
	'name' => 'my_lang_string'
];

separator:

'type' = 'separator'

example:

$el[] = [
	'type' => 'separator'
];

message: ->addMessage( $lang, $css='', $parse=TRUE, $_id=NULL)

'type' = 'message'
'msg' = this options is require for this type, the message you want to display

optional:

'css' = any additional css classes
'parse' = default is true, set to false if msg is an already parsed lang string
'id' = id of the element, default is null

Example:

$el[] = [
	'type' => 'message',
	'msg' => 'my_message_lang_string'
	'css' => 'myMessageClass myMessageClassWarning"
];

helper:

required:

'name' = name of the element
'class' = IPS Form Help Class (without the \IPS\Helpers\Form portion) or a FQN for a custom class.

optional:

'default' = the default value
'required' = is it required or not
'options' = an array of options for the class (identical to how options are set for form helpers)
'validation' = validation method, can use a closure here, identical to how its set on the form helper.
'prefix' = prefix for the form helper
'suffix' = suffix for the form helper
'id' = id for the form helper (if not set, the class will automatically configure name parameter with js_ prepend to it)
'skip_id' = (bool) if 'id' isn't set, and you want it not to set it to js_name
'label' = a lang string to use as the label instead of 'name'
'description' => a lang string to use as an alternative description
'appearRequired' => (bool) make the form helper appear required.
 

Example:

$el[] = [
	'class' => 'Select',
	'name' => 'my_select_element',
	'required' => true,
	'options' => [
		'options' => [ 1, 2, 3, 4 ]
	]
];

dummy: ->addDummy( $langKey, $value, $desc='', $warning='', $id='' )

Required:

'type' = 'dummy'
'name' = name of the element
'default' = value that will be display

Optional:

'desc' = description
'warning' = warning
'id' = html id (if not set, js_$name is used)

Example:

$el[] = [
	'type' => 'dummy',
	'name' => 'my_dummy_element',
	'default' => "content for the dummy element"
];

html: ->addHtml( $html )

Required:

'type' = 'html'
'html' = the html to display

Example:

$el[] = [
	'type' => 'html',
	'html' => "<a href='#'>some html</a>"
];

Matrix: ->addMatrix( $name, $matrix )

Required:

'type' = 'matrix'
'matrix = must be an instance of \IPS\Helpers\Form\Matrix
'name' = elements name.

Example:

$matrix = new \IPS\Helpers\Form\Matrix;

$el[] = [
	'type' => 'matrix',
	'matrix' => $matrix,
	'name' => 'my_matrix_element'
];

i() method:

public static function i( array $elements, $name = 'default', $form = null, $id = 'form', $submitLang = 'save', $action = null, $attributes = [] )

the instantiation method is pretty straight forward, you pass the array of elements to it, if you have multiple forms on the page, you can give the instance a name. if you have a form object already (like in nodes) just pass it Forms, the other parameters are for creating a new form, same parameters as \IPS\Form.

 

Bringing it all together:

$altDescription = "my_textarea_alt_desc";
$elements = [
	[
		'type' => 'tab',
		'name' => 'my_tab_1'
	],
	[
		'class' => 'Text',
		'name' => 'my_title',
		'required' => true
		'validation' => function( $data ){ if( !$data ) { throw new \InvalidArgumentException; } } 
	],
	[
		'class' => "Select",
		'name' => "my_select',
		'options' => [ 'options' => [ 1, 2, 3, 4 ] ]
	],
	[
		'type' => 'tab',
		'name' => 'my_tab_2'
	],
	[
		'class' => "TexArea",
		'name' => 'my_text_area',
		'default' => 'im a default',
		'description' => $altDescription
	],
	[
		'type' => 'separator'
	],
	[
		'class' => 'Text',
		'name' => 'my_text_2'
	]
];

$form = \IPS\myapp\Form::i( $elements );

\IPS\Output::i()->output = $form;

this will return an instance of \IPS\Form. 

2016-09-26_1926.png.9e538abf7bf2a5989abcd1f9580f9087.png

2016-09-26_1930.png.d9cec8f0d1a86cb830b282f0fb2edba1.png




User Feedback

Create an account or sign in to leave a review

You need to be a member in order to leave a review

There are no reviews to display.

×