\ValidFormBuilderText

Text Class is the most used.

Text objects are used to create input[type='text'] fields.

Example; Add a text field with some validation and custom classes to the form.

$objForm->addField(
    "first-name",
    "First name",
    ValidForm::VFORM_STRING,
    array(
        // Make this field required
        "required" => true,
        // It should have a maximum of 10 characters
        "maxLength" => 10,
        // It should have a minimum of 3 characters
        "minLength" => 3
    ),
    array(
        // Error message when required state isn't met
        "required" => "This is a required field",
        // Error message when input length is larger than 10 characters
        "maxLength" => "Maximum of %s characters allowed.",
        // Error message when input length is shorter than 3 characters
        "minLength" => "Minimum of %s characters required."
    ),
    array(
        // Add a custom class to the input element
        // This results in something like
        // <input type='text' class='vf__text custom-class'>
        "fieldclass" => "custom-class",
        // Add a custom class to the field container
        // This results in
        // <div class='vf__required container-class'>
        //     <input type='text'>
        // </div>
        "class" => "container-class"

    )
);

Summary

Methods
Properties
Constants
__construct()
isDynamicCounter()
toHtml()
__toHtml()
setError()
toJS()
getRandomId()
isValid()
getDynamicCount()
setDynamicCounter()
getValue()
hasFields()
setName()
getDefault()
setDefault()
__getValue()
isDynamic()
addCondition()
getConditions()
getCondition()
getMetCondition()
hasCondition()
hasConditions()
getConditionRecursive()
getDynamicButtonMeta()
setConditionalMeta()
setMeta()
setFieldMeta()
getFieldMeta()
setLabelMeta()
setTipMeta()
setDynamicLabelMeta()
setDynamicRemoveLabelMeta()
getMeta()
getLabelMeta()
getName()
getDynamicName()
getShortLabel()
setData()
getData()
__get()
__set()
__call()
getLabel()
setLabel()
getTip()
setTip()
getType()
setType()
getHint()
setHint()
getRequiredStyle()
setRequiredStyle()
getValidator()
setValidator()
getId()
setId()
setName()
getParent()
setParent()
setConditions()
getTipMeta()
getDynamicLabelMate()
getDynamicRemoveLabelMate()
getMagicMeta()
getMagicReservedMeta()
getReservedFieldMeta()
getReservedLabelMeta()
getReservedMeta()
hasFields()
getFields()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

__construct()

__construct(string  $name, integer  $type, string  $label = "", array  $validationRules = array(), array  $errorHandlers = array(), array  $meta = array()) 

Create new element

Parameters

string $name

Field name

integer $type

Field type

string $label

Field label

array $validationRules

Validation rules

array $errorHandlers

Error rules

array $meta

The meta array

isDynamicCounter()

isDynamicCounter() : boolean

Checks if this element is a dynamic counter for another element

Returns

boolean —

True if it is, false if not. Default false.

toHtml()

toHtml(boolean  $submitted = false, boolean  $blnSimpleLayout = false, boolean  $blnLabel = true, boolean  $blnDisplayErrors = true) : string

Generate HTML output

Parameters

boolean $submitted

Force if this field should behave like a submitted field or not (e.g. validate etc.)

boolean $blnSimpleLayout

Force 'simple layout' output -- no labels and wrapping divs.

boolean $blnLabel

Show label. Don't show if false.

boolean $blnDisplayErrors

Show errors (default true). Don't show errors if false.

Returns

string

__toHtml()

__toHtml(boolean  $submitted = false, boolean  $blnSimpleLayout = false, boolean  $blnLabel = true, boolean  $blnDisplayErrors = true, integer  $intCount) : string

Generate HTML output for specific dynamic count

Parameters

boolean $submitted
boolean $blnSimpleLayout
boolean $blnLabel
boolean $blnDisplayErrors
integer $intCount

Returns

string

setError()

setError(string  $strError, integer  $intDynamicPosition) 

Set a (custom) error message on this specific element

This is mostly used when doing custom server-side validation like validating a username existance. Example:

if ($objForm->isSubmitted() && $objForm->isValid()) {
    $objUserNameField = $objForm->getValidField("username");
    $strUserName = $objUserNameField->getValue();
    if (User::exists($strUserName)) {
        $objUserNameField->setError("User already exists.");
        $strOutput = $objForm->toHtml();
    } else {
        $strOutput = "Account created successfully with the following details:<br />";
        $strOutput .= $objForm->valuesAsHtml();
    }
}

Parameters

string $strError

The error message

integer $intDynamicPosition

Set the error message on a specific dynamic field with this index

toJS()

toJS(integer  $intDynamicPosition = false) : string

Placeholder method

Should be extended by child classes.

Parameters

integer $intDynamicPosition

Dynamic position counter

Returns

string

getRandomId()

getRandomId(string  $name) : string

Generate a random ID for a given field name to prevent having two fields with the same name

Parameters

string $name

Field name

Returns

string

isValid()

isValid(null  $intCount = null) : boolean

Validate the current field.

This is a wrapper method to call the Validator->validate() method. Although you could validate fields on a per-field basis with this method, this is mostly used internally. For instance, when \ValidFormBuilder\ValidForm::validate() is called, it loops trough it's elements collection and calls this method for each element it finds.

Parameters

null $intCount

Optional. If set, only the dynamic field with this index will be validated.

Returns

boolean —

True if field validates, false if not.

getDynamicCount()

getDynamicCount(boolean  $blnParentIsDynamic = false) : integer

Get the number of dynamic fields from the dynamic counter field.

Parameters

boolean $blnParentIsDynamic

Returns

integer —

The dynamic count of this field

setDynamicCounter()

setDynamicCounter(\ValidFormBuilder\Element  $objCounter) 

Add a dynamic counter object

Parameters

\ValidFormBuilder\Element $objCounter

getValue()

getValue(integer  $intDynamicPosition) : mixed

Get the *valid* value of the current field.

Parameters

integer $intDynamicPosition

Optional parameter to get the value of a dynamic field.

Returns

mixed —

The valid value of this field. If validation fails, it returns null.

hasFields()

hasFields() : boolean

Placeholder function to determine wheter or not a field contains other fields.

Returns

boolean —

Return false by default.

setName()

setName(string  $strName) 

Set a new name for this element

This method also updates the name in the elements Validator instance.

Parameters

string $strName

The new name

getDefault()

getDefault() : array|string

Get default value

Returns

array|string

setDefault()

setDefault(array|string  $varValue) 

Set default value on this element

Parameters

array|string $varValue

The value to set as default value

__getValue()

__getValue(boolean  $submitted = false, integer  $intDynamicPosition) : string|null

Get the value of the field.

If the value is valid then it will return that value, otherwise the invalid value is returned.

Parameters

boolean $submitted

Indicate if the form is submitted.

integer $intDynamicPosition

The position of the field in a dynamic field setup.

Returns

string|null

isDynamic()

isDynamic() : boolean

Check if the current field is a dynamic field.

Returns

boolean —

True if dynamic, false if not.

addCondition()

addCondition(string  $strType, boolean  $blnValue, array  $arrComparisons, string  $intComparisonType = \ValidFormBuilder\ValidForm::VFORM_MATCH_ANY) 

Add a new condition to the current field

For examples, check \ValidFormBuilder\Condition

Parameters

string $strType

Define the condition type. This can be either required, visibile or enabled

boolean $blnValue

Define whether this condition activates if the comparison(s) are true or false.

array $arrComparisons

An array of Comparison objects

string $intComparisonType

The comparison type. Either ValidForm::VFORM_MATCH_ANY or ValidForm::VFORM_MATCH_ALL. With VFORM_MATCH_ANY, as soon as one of the comparisons validates the condition, the condition is enforced. With ValidForm::VFORM_MATCH_ALL, all of the comparisons must validate before the condition will be enforced.

Throws

\Exception

if Condition could not be set

\InvalidArgumentException

If invalid arguments are supplied

getConditions()

getConditions() : array

Get the conditions collection

Returns

array

getCondition()

getCondition(string  $strProperty) : \ValidFormBuilder\Condition|null

Get element's Condition object

Note: When chaining methods, always use hasCondition() first before chaining for example getCondition()->isMet().

Parameters

string $strProperty

Condition type e.g. 'required', 'visibile' and 'enabled'

Returns

\ValidFormBuilder\Condition|null —

Found condition or null if no condition is found.

getMetCondition()

getMetCondition(string  $strProperty) : null|\ValidFormBuilder\Condition

Only get a condition of a given type if that condition is met. If the condition is not met, this returns null

Parameters

string $strProperty

Condition type e.g. 'required', 'visibile' and 'enabled'

Returns

null|\ValidFormBuilder\Condition

hasCondition()

hasCondition(string  $strProperty) : boolean

Check if the current field contains a condition object of a specific type

Parameters

string $strProperty

Condition type e.g. required, visibile and enabled

Returns

boolean —

True if element has condition object set, false if not

hasConditions()

hasConditions() : boolean

Check if the current object contains any conditions at all.

Returns

boolean —

True if it contains conditions, false if not.

getConditionRecursive()

getConditionRecursive(string  $strProperty, \ValidFormBuilder\Element  $objContext = null) : \ValidFormBuilder\Condition|null

This gets the condition of a given property, just like {@link \ValidFormBuilder\Base::getCondition()}.

When no condition is found on the current element, the method searches for a condition in it's parent element.

Parameters

string $strProperty

Condition type e.g. required, visibile and enabled

\ValidFormBuilder\Element $objContext

Returns

\ValidFormBuilder\Condition|null

getDynamicButtonMeta()

getDynamicButtonMeta() 

This method determines wheter or not to show the 'add extra field' dynamic button based on it's parent's condition state.

setConditionalMeta()

setConditionalMeta() 

Based on which conditions are met, corresponding metadata is set on the object.

setMeta()

setMeta(string  $property, mixed  $value, boolean  $blnOverwrite = false) : array

Set meta property.

Parameters

string $property

Property name.

mixed $value

Property value.

boolean $blnOverwrite

Optionally use this boolean to force an overwrite of previous property value.

Returns

array

setFieldMeta()

setFieldMeta(string  $property, mixed  $value, boolean  $blnOverwrite = false) : mixed

Set field specific meta data

Parameters

string $property

Property name.

mixed $value

Property value.

boolean $blnOverwrite

Optionally use this boolean to force an overwrite of previous property value.

Returns

mixed —

The newly set value

getFieldMeta()

getFieldMeta(string  $property = null, string  $fallbackValue = "") : mixed

Get field meta property.

Parameters

string $property

Property to get from internal field meta array.

string $fallbackValue

Optional fallback value if no value is found for requested property

Returns

mixed

setLabelMeta()

setLabelMeta(string  $property, mixed  $value, boolean  $blnOverwrite = false) : mixed

Set label specific meta data

Parameters

string $property

Property name.

mixed $value

Property value.

boolean $blnOverwrite

Optionally use this boolean to force an overwrite of previous property value.

Returns

mixed —

The newly set value

setTipMeta()

setTipMeta(string  $property, mixed  $value, boolean  $blnOverwrite = false) : mixed

Set tip specific meta data

Parameters

string $property

Property name.

mixed $value

Property value.

boolean $blnOverwrite

Optionally use this boolean to force an overwrite of previous property value.

Returns

mixed —

The newly set value

setDynamicLabelMeta()

setDynamicLabelMeta(string  $property, mixed  $value, boolean  $blnOverwrite = false) : mixed

Set dynamic label specific meta data

Parameters

string $property

Property name.

mixed $value

Property value.

boolean $blnOverwrite

Optionally use this boolean to force an overwrite of previous property value.

Returns

mixed —

The newly set value

setDynamicRemoveLabelMeta()

setDynamicRemoveLabelMeta(string  $property, mixed  $value, boolean  $blnOverwrite = false) : mixed

Set dynamic remove label specific meta data

Parameters

string $property

Property name.

mixed $value

Property value.

boolean $blnOverwrite

Optionally use this boolean to force an overwrite of previous property value.

Returns

mixed —

The newly set value

getMeta()

getMeta(string  $property = null, string  $fallbackValue = "") : mixed

Get meta property.

Parameters

string $property

Property to get from internal meta array.

string $fallbackValue

Optional fallback value if requested property has no value

Returns

mixed

getLabelMeta()

getLabelMeta(string  $property = null, string  $fallbackValue = "") : string

Get label meta property.

Parameters

string $property

Property to get from internal label meta array.

string $fallbackValue

Optional fallback value if requested property has no value

Returns

string —

Property value or empty string of none is set.

getName()

getName() : string

Return the (original) name of the current field.

Use getDynamicName() to get the field name + dynamic count

Returns

string —

The original field name

getDynamicName()

getDynamicName(integer  $intCount) : string

Same as getName() except getDynamicName adds the current dynamic count to the fieldname as a suffix (_1, _2 etc)

When the dynamic count === 0, the return value equals the output of getName()

Parameters

integer $intCount

The dynamic count

Returns

string —

The field name

getShortLabel()

getShortLabel() : string

Get the short label (meta 'summaryLabel') if available.

Use the 'long' (regular) label as a fallback return value.

Returns

string —

The short or regular element label

setData()

setData(string  $strKey = null, mixed  $varValue = null) : boolean

Store data in the current object.

This data will not be visibile in any output and will only be used for internal purposes. For example, you can store some custom data from your CMS or an other library in a field object, for later use.

Note: Using this method will overwrite any previously set data with the same key!

Parameters

string $strKey

The key for this storage

mixed $varValue

The value to store

Returns

boolean —

True if set successful, false if not.

getData()

getData(string  $strKey = null) : mixed

Get a value from the internal data array.

Parameters

string $strKey

The key of the data attribute to return

Returns

mixed

__get()

__get(string  $property) 

Magic getter method

Parameters

string $property

Throws

\BadMethodCallException

__set()

__set(string  $property, mixed  $value) 

Magic setter method

Parameters

string $property
mixed $value

Throws

\BadMethodCallException

__call()

__call(string  $method, mixed  $values) 

Magic caller method

Parameters

string $method
mixed $values

Throws

\BadMethodCallException

getLabel()

getLabel() : string

Returns the value of $__label

Returns

string

setLabel()

setLabel(string  $value) : void

Overwrites the value of $__label

Parameters

string $value

getTip()

getTip() : string

Returns the value of $__tip

Returns

string

setTip()

setTip(string  $value) : void

Overwrites the value of $__tip

Parameters

string $value

getType()

getType() : integer

Returns the value of $__type

Returns

integer

setType()

setType(integer  $value) : void

Overwrites the value of $__type

Parameters

integer $value

getHint()

getHint() : string

Returns the value of $__hint

Returns

string

setHint()

setHint(string  $value) : void

Overwrites the value of $__hint

Parameters

string $value

getRequiredStyle()

getRequiredStyle() : string

Returns the value of $__requiredstyle

Returns

string

setRequiredStyle()

setRequiredStyle(string  $value) : void

Overwrites the value of $__requiredstyle

Parameters

string $value

getValidator()

getValidator() : \ValidFormBuilder\FieldValidator

Returns the value of $__validator

Returns

\ValidFormBuilder\FieldValidator

setValidator()

setValidator(\FieldValidator  $value) : void

Overwrites the value of $__validator

Parameters

\FieldValidator $value

getId()

getId() : string

Returns the value of $__id

Returns

string

setId()

setId(string  $value) : void

Overwrites the value of $__id

Parameters

string $value

setName()

setName(string  $value) : void

Overwrites the value of $__name

Parameters

string $value

getParent()

getParent() : \ValidFormBuilder\Base

Returns the value of $__parent

Returns

\ValidFormBuilder\Base

setParent()

setParent(\Base  $value) : void

Overwrites the value of $__parent

Parameters

\Base $value

setConditions()

setConditions(array  $value) : void

Overwrites the value of $__conditions

Parameters

array $value

getTipMeta()

getTipMeta() : array

Returns the value of $__tipmeta

Returns

array

getDynamicLabelMate()

getDynamicLabelMate() : array

Returns the value of $__dynamiclabelmeta

Returns

array

getDynamicRemoveLabelMate()

getDynamicRemoveLabelMate() : array

Returns the value of $__dynamicremovelabelmeta

Returns

array

getMagicMeta()

getMagicMeta() : array

Returns the value of $__magicmeta

Returns

array

getMagicReservedMeta()

getMagicReservedMeta() : array

Returns the value of $__magicreservedmeta

Returns

array

getReservedFieldMeta()

getReservedFieldMeta() : array

Returns the value of $__reservedfieldmeta

Returns

array

getReservedLabelMeta()

getReservedLabelMeta() : array

Returns the value of $__reservedlabelmeta

Returns

array

getReservedMeta()

getReservedMeta() : array

Returns the value of $__reservedmeta

Returns

array

hasFields()

hasFields() : boolean

Returns if this element has any child fields

Returns

boolean

getFields()

getFields() : \ValidFormBuilder\Collection

Returns a collection of child fields

Returns

\ValidFormBuilder\Collection