/** * Create a Multifield element * * Multifield elements allow you to combine multiple fields horizontally with one label. * For example, create a first name + last name field with label "Full name" * * ```php * $objMulti = $objForm->addMultifield("Full name"); * // Note: when using addField on a multifield, we don't set a label! * $objMulti->addField( * "first-name", * ValidForm::VFORM_STRING, * array(), * array(), * // Keep it short, this is just a first name field * array("style" => "width: 50px") * ); * $objMulti->addField("last-name", ValidForm::VFORM_STRING); * ``` * * You can also combine select elements to create a date picker: * * ```php * $objMulti = $objForm->addMultiField("Birthdate"); * $objMulti->addField( * "year", * ValidForm::VFORM_SELECT_LIST, * array(), * array(), * array( * "start" => 1920, * "end" => 2014, * // 'fieldstyle' gets applied on the <select> * // regular 'style' applies on the wrapping <div> * "fieldstyle" => "width: 75px" * ) * ); * $objMulti->addField( * "month", * ValidForm::VFORM_SELECT_LIST, * array(), * array(), * array( * "start" => 01, * "end" => 12, * "fieldstyle" => "width: 75px" * ) * ); * $objMulti->addField( * "day", * ValidForm::VFORM_SELECT_LIST, * array(), * array(), * array( * "start" => 1, * "end" => 31, * "fieldstyle" => "width: 75px" * ) * ); * ``` * * @param string $label * @param array $meta The meta array * @return \ValidFormBuilder\MultiField */ public function addMultiField($label = null, $meta = array()) { $objField = new MultiField($label, $meta); $objField->setRequiredStyle($this->__requiredstyle); // *** Fieldset already defined? $objFieldset = $this->__elements->getLast("ValidFormBuilder\\Fieldset"); if ($this->__elements->count() == 0 || !is_object($objFieldset)) { $objFieldset = $this->addFieldset(); } $objField->setMeta("parent", $objFieldset, true); // *** Add field to the fieldset. $objFieldset->addField($objField); return $objField; }
/** * Check if this area contains child objects. * @internal * @return boolean True if fields collection > 0, false if not. */ public function hasFields() { return $this->__fields->count() > 0 ? true : false; }
/** * @internal * @see \ValidFormBuilder\Element::__toHtml() */ public function __toHtml($submitted = false, $blnSimpleLayout = false, $blnLabel = true, $blnDisplayErrors = true, $intCount = 0) { $strOutput = ""; $strName = $intCount == 0 ? $this->__name : $this->__name . "_" . $intCount; $strId = $intCount == 0 ? $this->__id : $this->__id . "_" . $intCount; $blnError = $submitted && !$this->__validator->validate($intCount) && $blnDisplayErrors ? true : false; if (!$blnSimpleLayout) { // *** We asume that all dynamic fields greater than 0 are never required. if ($this->__validator->getRequired() && $intCount == 0) { $this->setMeta("class", "vf__required"); } else { $this->setMeta("class", "vf__optional"); } // *** Set custom meta. if ($blnError) { $this->setMeta("class", "vf__error"); } if (!$blnLabel) { $this->setMeta("class", "vf__nolabel"); } // Call this right before __getMetaString(); $this->setConditionalMeta(); $strOutput .= "<div{$this->__getMetaString()}>\n"; if ($blnError) { $strOutput .= "<p class=\"vf__error\">{$this->__validator->getError($intCount)}</p>"; } $strLabel = !empty($this->__requiredstyle) && $this->__validator->getRequired() ? sprintf($this->__requiredstyle, $this->__label) : $this->__label; if (!empty($this->__label)) { $strOutput .= "<label for=\"{$strId}\"{$this->__getLabelMetaString()}>{$strLabel}</label>\n"; } } else { if ($blnError) { $this->setMeta("class", "vf__error"); } $this->setMeta("class", "vf__multifielditem"); // Call this right before __getMetaString(); $this->setConditionalMeta(); $strOutput = "<div{$this->__getMetaString()}>\n"; } $strOutput .= "<select name=\"{$strName}\" id=\"{$strId}\" {$this->__getFieldMetaString()}>\n"; // *** If no option elements are available, parse ranges if ($this->__options->count() == 0) { $this->__parseRanges(); } foreach ($this->__options as $option) { $strOutput .= $option->toHtmlInternal($this->__getValue($submitted, $intCount)); } $strOutput .= "</select>\n"; if ($this->getMeta("tip") !== "") { $this->__tip = $this->getMeta("tip"); } if (!empty($this->__tip)) { $strOutput .= "<small class=\"vf__tip\"{$this->__getTipMetaString()}>{$this->__tip}</small>\n"; } $strOutput .= "</div>\n"; if (!$blnSimpleLayout && $intCount == $this->getDynamicCount()) { $strOutput .= $this->__addDynamicHtml(); } return $strOutput; }