1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
*/
namespace MvcCore\Ext\Form;
/**
* Trait for class `MvcCore\Ext\Form` containing getters and setters methods for
* form field instances and methods to add, search or remove field instance from
* form.
*/
trait FieldMethods {
/**
* Get all form field controls.
* After adding any field into form instance by `$form->AddField()` method
* field is added under it's name into this array with all another form fields
* except CSRF `input:hidden`s. Fields are rendered by order in this array.
* @return \MvcCore\Ext\Forms\Field[]
*/
public function & GetFields() {
return $this->fields;
}
/**
* Replace all previously configured fields with given fully configured fields array.
* This method is dangerous - it will remove all previously added form fields
* and add given fields. If you want only to add another field(s) into form,
* use functions:
* - `$form->AddField($field);`
* - `$form->AddFields($field1, $field2, $field3...);`
* @param \MvcCore\Ext\Forms\Field[] $fields Array with `\MvcCore\Ext\Forms\IField` instances to set into form.
* @return \MvcCore\Ext\Form
*/
public function SetFields ($fields = []) {
/** @var $this \MvcCore\Ext\IForm */
$this->fields = [];
foreach ($fields as $field)
$this->AddField($field);
return $this;
}
/**
* Add multiple fully configured form field instances,
* function have infinite params with new field instances.
* @param \MvcCore\Ext\Forms\Field[] $fields,... Any `\MvcCore\Ext\Forms\IField` fully configured instance to add into form.
* @return \MvcCore\Ext\Form
*/
public function AddFields ($fields) {
/** @var $this \MvcCore\Ext\Form */
$fields = func_get_args();
if (count($fields) === 1 && is_array($fields[0])) $fields = $fields[0];
foreach ($fields as $field)
$this->AddField($field);
return $this;
}
/**
* Add fully configured form field instance.
* @param \MvcCore\Ext\Forms\Field $field
* @return \MvcCore\Ext\Form
*/
public function AddField (\MvcCore\Ext\Forms\IField $field) {
/** @var $this \MvcCore\Ext\Form */
/** @var $field \MvcCore\Ext\Forms\Field */
if ($this->dispatchState < \MvcCore\IController::DISPATCH_STATE_INITIALIZED)
$this->Init();
$fieldName = $field->GetName();
$field->SetForm($this);
$this->fields[$fieldName] = $field;
if ($field instanceof \MvcCore\Ext\Forms\Fields\ISubmit) {
$this->submitFields[$fieldName] = $field;
$fieldCustomResultState = $field->GetCustomResultState();
if ($fieldCustomResultState !== NULL)
$this->customResultStates[$fieldName] = $fieldCustomResultState;
}
return $this;
}
/**
* If `TRUE` if given field instance or given
* field name exists in form, `FALSE` otherwise.
* @param \MvcCore\Ext\Forms\Field|string $fieldOrFieldName
* @return bool
*/
public function HasField ($fieldOrFieldName = NULL) {
$fieldName = NULL;
if ($fieldOrFieldName instanceof \MvcCore\Ext\Forms\IField) {
$fieldName = $fieldOrFieldName->GetName();
} else if (is_string($fieldOrFieldName)) {
$fieldName = $fieldOrFieldName;
}
return isset($this->fields[$fieldName]);
}
/**
* Remove configured form field instance by given instance or given field name.
* If field is not found by it's name, no error happened.
* @param \MvcCore\Ext\Forms\Field|string $fieldOrFieldName
* @return \MvcCore\Ext\Form
*/
public function RemoveField ($fieldOrFieldName = NULL) {
/** @var $this \MvcCore\Ext\Form */
if ($this->dispatchState < \MvcCore\IController::DISPATCH_STATE_INITIALIZED)
$this->Init();
$fieldName = NULL;
if ($fieldOrFieldName instanceof \MvcCore\Ext\Forms\IField) {
$fieldName = $fieldOrFieldName->GetName();
} else if (is_string($fieldOrFieldName)) {
$fieldName = $fieldOrFieldName;
}
if (isset($this->fields[$fieldName]))
unset($this->fields[$fieldName]);
return $this;
}
/**
* Return form field instance by form field name if it exists, else return null;
* @param string $fieldName
* @return \MvcCore\Ext\Forms\Field|NULL
*/
public function GetField ($fieldName = '') {
$result = NULL;
if (isset($this->fields[$fieldName]))
$result = $this->fields[$fieldName];
return $result;
}
/**
* Return form field instances by given field type string.
* If no field(s) found, it's returned empty array.
* Result array is keyed by field names.
* @param string $fieldType
* @return \MvcCore\Ext\Forms\Field[]|array
*/
public function GetFieldsByType ($fieldType = '') {
$result = [];
foreach ($this->fields as $field) {
if ($field->GetType() == $fieldType)
$result[$field->GetName()] = $field;
}
return $result;
}
/**
* Return first caught form field instance by given field type string.
* If no field found, `NULL` is returned.
* @param string $fieldType
* @return \MvcCore\Ext\Forms\Field|NULL
*/
public function GetFirstFieldByType ($fieldType = '') {
$result = NULL;
foreach ($this->fields as $field) {
if ($field->GetType() == $fieldType) {
$result = $field;
}
}
return $result;
}
/**
* Return form field instances by field class name
* compared by `is_a($field, $fieldClassName)` check.
* If no field(s) found, it's returned empty array.
* Result array is keyed by field names.
* @param string $fieldClassName Full php class name or full interface name.
* @param bool $directTypesOnly Get only instances created directly from called type, no instances extended from given class name.
* @return \MvcCore\Ext\Forms\Field[]|array
*/
public function GetFieldsByPhpClass ($fieldClassName = '', $directTypesOnly = FALSE) {
$result = [];
foreach ($this->fields as $field) {
if (is_a($field, $fieldClassName)) {
if ($directTypesOnly)
if (is_subclass_of($field, $fieldClassName))
continue;
$result[$field->GetName()] = $field;
}
}
return $result;
}
/**
* Return first caught form field instance by field class name
* compared by `is_a($field, $fieldClassName)` check.
* If no field found, it's returned `NULL`.
* @param string $fieldClassName Full php class name or full interface name.
* @param bool $directTypesOnly Get only instances created directly from called type, no instances extended from given class name.
* @return \MvcCore\Ext\Forms\Field|NULL
*/
public function GetFirstFieldByPhpClass ($fieldClassName = '', $directTypesOnly = FALSE) {
$result = NULL;
foreach ($this->fields as $field) {
if (is_a($field, $fieldClassName)) {
if ($directTypesOnly)
if (is_subclass_of($field, $fieldClassName))
continue;
$result = $field;
break;
}
}
return $result;
}
}