-
Notifications
You must be signed in to change notification settings - Fork 0
Home
ClosureForm Form
Lightweight form generation/handling library that gives the end user a lot of flexibility though the use of Closures/Anonymous functions. Validations are Closures, field templates are Closures, buttons can be given actions though Closures that are executed automatically on form submit.
Link : https://github.com/warmans/ClosureForm
Author : warmans
`public` **__construct**( [string $name], [array $attributes] )Construct a new form.
Param Note: $name defaults to 'generic-form'. The name of this particular form.
Param Note: $attributes defaults to 'Array'. Attributes are rendered as attt="val" in the form tag. You should set method, action etc. here
`public` **isSubmitted**( )Check if the form has been submitted. This is based on the presence of an auto-generated field in the in the relevant superglobal (POST, GET).
Return : boolean
`public` **isValid**( )Check if validation has passed (and no external errors have been added). Validation is only run once.
Return : boolean
`public` **handleButtonActions**( )Causes the action given to the submitted button to be triggered.
Return : mixed - response of action Closure
`public` **getName**( )Get the name of the form as defined in the constructor.
Return : string
`public` **getField**( string $name )Get a field by name. Throws Exception if field is not found.
Return : Element\FieldProxy
Throws : \RuntimeException
`public` **getFields**( )Get all the fields added to the form.
Return : array Returns an array of FieldProxy elements
`public` **getButtons**( )Get all the buttons for the form.
Return : array Returns and array of ButtonProxy elements
`public` **addTextField**( string $name )Text type field (i.e. a standard input).
Return : Element\FieldProxy
`public` **addHiddenField**( string $name )Hidden field. Hidden fields do not render row or error elements either.
Return : Element\FieldProxy
`public` **addPasswordField**( string $name )Password field (text field with obsucred characters)
Return : Element\FieldProxy
`public` **addCheckboxField**( string $name )Checkbox type field.
Return : Element\FieldProxy
`public` **addSelectField**( string $name, array $keyVals )Select field. This method also takes an options array.
Param Note: $keyVals field options in key=>val (value=>display) pairs
Return : Element\FieldProxy
`public` **addInputField**( string $type, string $name )Field with a user defined type (e.g. text, password, etc.)
Param Note: $type The value given to the type attribute of the field.
Return : Element\FieldProxy
`public` **addTextareaField**( string $name )Textarea type field.
Return : Element\FieldProxy
`public` **addButton**( string $name )Adds a button to the form. A button is similar to a field but can be assigned an action to perform if the form is submitted using that button. You must only call handleButtonActions to trigger the relevant action.
Return : \ClosureForm\Button
`public` **addError**( string $errorMsg, [string $affectsField] )Add an external error (i.e. not based on internal validation) to the form. If a field is specified the error will be appended to that field's error array. Otherwise it'll just display as a generic error. Returns $this to allow error chaining.
Param Note: $affectsField fieldname of affected field
Return : Form
`public` **render**( )Render the entire form including all errors, fields and buttons and return as a string.
Return : string
`public` **getAttributeString**( array $attributes )Convert keyval pairs into an attribute string. Users should not need this.
Param Note: $attributes name=>val pairs
Return : string
`public` **getAttribute**( string $name )Get the value of a specific form attribute or NULL if empty.
Return : mixed
`public` **setSuperglobalOverride**( array $data )Override the superglobal specified by the form method e.g. rather than using _POST use $data.
`public` **getSuperglobal**( )Get the POST or GET array depending on the form method attribute.
Return : array
Throws : \RuntimeException
FieldProxy was initially used to provide a fluent interface for fields ultimately proxying the Form class. At this point they no longer really fit the GoF taxonomy for a Proxy because all the field behaviour was moved to the Field class by way of Closures.
Author : warmans
`public` **__construct**( ClosureForm\Form $form, string $fieldName, [string $fieldType] )Construct a new Field.
Param Note: $form The parent form
Param Note: $fieldType defaults to 'text'.
Throws : \RuntimeException
`public` **preRender**( Closure $preRenderAction )A function that is executed before the field is rendered .
Return : \ClosureForm\Element\FieldProxy
`public` **template**( Closure $template )Override the template for the field.
Return : \ClosureForm\FieldProxy
`public` **errorTemplate**( Closure $template )Override error template for the field.
Return : \ClosureForm\FieldProxy
`public` **attributes**( [array $attributes] )Set multiple attributes at once. Attributes are rendered in the format of key="value"
Return : \ClosureForm\FieldProxy
`public` **attribute**( string $name, string $value )Set the value of a specific attribute
Return : \ClosureForm\FieldProxy
`public` **label**( string $label )Set the label for the field
Return : \ClosureForm\FieldProxy
`public` **validator**( Closure $validator )Valdate the field using the supplied function. Returning an error message or FALSE will invalidate the field. Returning anything else (e.g. NULL, TRUE, 0, 1) will not invalidate the field.
Return : \ClosureForm\FieldProxy
`public` **getName**( )Get the name of the field.
Return : string
`public` **getLabel**( )Get the field label (not including label tags).
Return : string
`public` **getType**( )Get the type of the field (e.g. text, password, textarea).
Return : tring
`public` **getSubmittedValue**( )Get the submitted value for this field. If the form hasn't been submitted you will get NULL. If the fiels was not set you will get FALSE (e.g. a non-checked checkbox).
Return : mixed
`public` **getAttributeString**( )Get the attribute string for the field e.g. class="foo" id="bar".
Return : string
`public` **getAttribute**( string $name )Get the value of a single attribute.
Return : string
`public` **extractAttribute**( string $name )Get attribute value and remove from attribute array.
Return : mixed
`public` **isValid**( )Test the field against its validators.
Return : boolean
`public` **getErrors**( )Get an array of errors or empty array if non were found.
Return : array
`public` **addError**( string $error )Add an error to the field.
Return : \ClosureForm\FieldProxy
`public` **render**( )Render the field and return it as a string.
Return : string
ButtonProxy - Buttons only offer a small subset of options of a field but do allow for an action Closure to be set and called automatically by the form. As per FieldProxy buttons aren't really GoF Proxies anymore but retain the name for histrorical reasons.
Author : warmans
`public` **__construct**( ClosureForm\Form $form, string $buttonName )Create a new Button Proxy
Param Note: $form The parent form
`public` **label**( type $label )Set the button label (the text appearing IN the button).
Return : \ClosureForm\Element\ButtonProxy
`public` **action**( Closure $action )Define the action that is triggered when the button is pressed. This is optional - you can handle the form in the usual way.
Return : \ClosureForm\Element\ButtonProxy
`public` **template**( Closure $template )Set the button template.
Return : \ClosureForm\Element\ButtonProxy
`public` **getName**( )Get the button's name attribute value.
Return : string
`public` **getLabel**( )Get the label value (the text shown on the button)
Return : string
`public` **getAttributeString**( )Get the attribute array of the button as a string.
Return : string
`public` **trigger**( )Call the action closure assigned to the button. This is called automatically by the form.
Return : boolean
Throws : \RuntimeException
`public` **render**( )Render the button and return as a string.
Return : string