\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
setError()
toJS()
isValid()
getValue()
setName()
getDefault()
setDefault()
addCondition()
setMeta()
setFieldMeta()
getFieldMeta()
setLabelMeta()
setTipMeta()
getMeta()
getLabelMeta()
getName()
setData()
getData()
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

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) : string

Generate corresponding javascript code for this element

Should be extended by child classes.

Parameters

integer $intDynamicPosition

Dynamic position

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.

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.

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

addCondition()

addCondition(string  $strType, boolean  $blnValue, array  $arrComparisons, integer  $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

integer $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

setMeta()

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

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.

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

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

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