form.php
来自「Cake Framwork , Excellent」· PHP 代码 · 共 1,711 行 · 第 1/4 页
PHP
1,711 行
} else { return null; } }/** * Returns a formatted LABEL element for HTML FORMs. * * @param string $fieldName This should be "Modelname.fieldname", "Modelname/fieldname" is deprecated * @param string $text Text that will appear in the label field. * @return string The formatted LABEL element */ function label($fieldName = null, $text = null, $attributes = array()) { if (empty($fieldName)) { $view = ClassRegistry::getObject('view'); $fieldName = implode('.', $view->entity()); } if ($text === null) { if (strpos($fieldName, '/') !== false || strpos($fieldName, '.') !== false) { $text = array_pop(preg_split('/[\/\.]+/', $fieldName)); } else { $text = $fieldName; } if (substr($text, -3) == '_id') { $text = substr($text, 0, strlen($text) - 3); } $text = __(Inflector::humanize(Inflector::underscore($text)), true); } if (isset($attributes['for'])) { $labelFor = $attributes['for']; unset($attributes['for']); } else { $labelFor = $this->domId($fieldName); } return $this->output(sprintf($this->Html->tags['label'], $labelFor, $this->_parseAttributes($attributes), $text)); }/** * Will display all the fields passed in an array expects fieldName as an array key * replaces generateFields * * @access public * @param array $fields works well with Controller::generateFields() or on its own; * @param array $blacklist a simple array of fields to skip * @return output */ function inputs($fields = null, $blacklist = null) { $fieldset = $legend = true; if (is_array($fields)) { if (array_key_exists('legend', $fields)) { $legend = $fields['legend']; unset($fields['legend']); } if (isset($fields['fieldset'])) { $fieldset = $fields['fieldset']; unset($fields['fieldset']); } } elseif ($fields !== null) { $fieldset = $legend = $fields; if (!is_bool($fieldset)) { $fieldset = true; } $fields = array(); } if (empty($fields)) { $fields = array_keys($this->fieldset['fields']); } if ($legend === true) { $actionName = __('New', true); if (strpos($this->action, 'update') !== false || strpos($this->action, 'edit') !== false) { $actionName = __('Edit', true); } $modelName = Inflector::humanize(Inflector::underscore($this->model())); $legend = $actionName .' '. __($modelName, true); } $out = null; foreach ($fields as $name => $options) { if (is_numeric($name) && !is_array($options)) { $name = $options; $options = array(); } if (is_array($blacklist) && in_array($name, $blacklist)) { continue; } $out .= $this->input($name, $options); } if (is_string($fieldset)) { $fieldsetClass = sprintf(' class="%s"', $fieldset); } else { $fieldsetClass = ''; } if ($fieldset && $legend) { return sprintf($this->Html->tags['fieldset'], $fieldsetClass, sprintf($this->Html->tags['legend'], $legend) . $out); } elseif ($fieldset) { return sprintf($this->Html->tags['fieldset'], $fieldsetClass, $out); } else { return $out; } }/** * Generates a form input element complete with label and wrapper div * * @param string $fieldName This should be "Modelname.fieldname", "Modelname/fieldname" is deprecated * @param array $options - Each type of input takes different options. See each field type method for more information. * @return string */ function input($fieldName, $options = array()) { $view =& ClassRegistry::getObject('view'); $this->setEntity($fieldName); $options = array_merge(array('before' => null, 'between' => null, 'after' => null), $options); if (!isset($options['type'])) { $options['type'] = 'text'; if (isset($options['options'])) { $options['type'] = 'select'; } elseif (in_array($this->field(), array('psword', 'passwd', 'password'))) { $options['type'] = 'password'; } elseif (isset($this->fieldset['fields'][$this->field()])) { $fieldDef = $this->fieldset['fields'][$this->field()]; $type = $fieldDef['type']; $primaryKey = $this->fieldset['key']; } elseif (ClassRegistry::isKeySet($this->model())) { $model =& ClassRegistry::getObject($this->model()); $type = $model->getColumnType($this->field()); $fieldDef = $model->schema(); if (isset($fieldDef[$this->field()])) { $fieldDef = $fieldDef[$this->field()]; } else { $fieldDef = array(); } $primaryKey = $model->primaryKey; } if (isset($type)) { $map = array( 'string' => 'text', 'datetime' => 'datetime', 'boolean' => 'checkbox', 'timestamp' => 'datetime', 'text' => 'textarea', 'time' => 'time', 'date' => 'date', 'float' => 'text' ); if (isset($this->map[$type])) { $options['type'] = $this->map[$type]; } elseif (isset($map[$type])) { $options['type'] = $map[$type]; } if ($this->field() == $primaryKey) { $options['type'] = 'hidden'; } } if ($this->model() === $this->field()) { $options['type'] = 'select'; if (!isset($options['multiple'])) { $options['multiple'] = 'multiple'; } } } if (!isset($options['options']) && in_array($options['type'], array('text', 'checkbox', 'radio', 'select'))) { $view =& ClassRegistry::getObject('view'); $varName = Inflector::variable(Inflector::pluralize(preg_replace('/_id$/', '', $this->field()))); $varOptions = $view->getVar($varName); if (is_array($varOptions)) { if ($options['type'] !== 'radio') { $options['type'] = 'select'; } $options['options'] = $varOptions; } } $autoLength = (!array_key_exists('maxlength', $options) && isset($fieldDef['length'])); if ($autoLength && $options['type'] == 'text') { $options['maxlength'] = $fieldDef['length']; } if ($autoLength && $fieldDef['type'] == 'float') { $options['maxlength'] = array_sum(explode(',', $fieldDef['length']))+1; } $out = ''; $div = true; $divOptions = array(); if (array_key_exists('div', $options)) { $div = $options['div']; unset($options['div']); } if (!empty($div)) { $divOptions['class'] = 'input'; $divOptions = $this->addClass($divOptions, $options['type']); if (is_string($div)) { $divOptions['class'] = $div; } elseif (is_array($div)) { $divOptions = array_merge($divOptions, $div); } if (in_array($this->field(), $this->fieldset['validates'])) { $divOptions = $this->addClass($divOptions, 'required'); } if (!isset($divOptions['tag'])) { $divOptions['tag'] = 'div'; } } $label = null; if (isset($options['label']) && $options['type'] !== 'radio') { $label = $options['label']; unset($options['label']); } if ($options['type'] === 'radio') { $label = false; if (isset($options['options'])) { if (is_array($options['options'])) { $radioOptions = $options['options']; } else { $radioOptions = array($options['options']); } unset($options['options']); } } if ($label !== false) { $labelAttributes = array(); if (in_array($options['type'], array('date', 'datetime'))) { $labelAttributes = $this->domId($labelAttributes, 'for'); $labelAttributes['for'] .= 'Month'; } if (is_array($label)) { $labelText = null; if (isset($label['text'])) { $labelText = $label['text']; unset($label['text']); } $labelAttributes = array_merge($labelAttributes, $label); } else { $labelText = $label; } if (isset($options['id'])) { $labelAttributes = array_merge($labelAttributes, array('for' => $options['id'])); } $out = $this->label($fieldName, $labelText, $labelAttributes); } $error = null; if (isset($options['error'])) { $error = $options['error']; unset($options['error']); } $selected = null; if (array_key_exists('selected', $options)) { $selected = $options['selected']; unset($options['selected']); } if (isset($options['rows']) || isset($options['cols'])) { $options['type'] = 'textarea'; } $empty = false; if (isset($options['empty'])) { $empty = $options['empty']; unset($options['empty']); } $timeFormat = 12; if (isset($options['timeFormat'])) { $timeFormat = $options['timeFormat']; unset($options['timeFormat']); } $dateFormat = 'MDY'; if (isset($options['dateFormat'])) { $dateFormat = $options['dateFormat']; unset($options['dateFormat']); } $type = $options['type']; $before = $options['before']; $between = $options['between']; $after = $options['after']; unset($options['type'], $options['before'], $options['between'], $options['after']); switch ($type) { case 'hidden': $out = $this->hidden($fieldName, $options); unset($divOptions); break; case 'checkbox': $out = $before . $this->checkbox($fieldName, $options) . $between . $out; break; case 'radio': $out = $before . $out . $this->radio($fieldName, $radioOptions, $options) . $between; break; case 'text': case 'password': $out = $before . $out . $between . $this->{$type}($fieldName, $options); break; case 'file': $out = $before . $out . $between . $this->file($fieldName, $options); break; case 'select': $options = array_merge(array('options' => array()), $options); $list = $options['options']; unset($options['options']); $out = $before . $out . $between . $this->select($fieldName, $list, $selected, $options, $empty); break; case 'time': $out = $before . $out . $between . $this->dateTime($fieldName, null, $timeFormat, $selected, $options, $empty); break; case 'date': $out = $before . $out . $between . $this->dateTime($fieldName, $dateFormat, null, $selected, $options, $empty); break; case 'datetime': $out = $before . $out . $between . $this->dateTime($fieldName, $dateFormat, $timeFormat, $selected, $options, $empty); break; case 'textarea': default: $out = $before . $out . $between . $this->textarea($fieldName, array_merge(array('cols' => '30', 'rows' => '6'), $options)); break; } if ($type != 'hidden') { $out .= $after; if ($error !== false) { $errMsg = $this->error($fieldName, $error); if ($errMsg) { $out .= $errMsg; $divOptions = $this->addClass($divOptions, 'error'); } } } if (isset($divOptions) && isset($divOptions['tag'])) { $tag = $divOptions['tag']; unset($divOptions['tag']); $out = $this->Html->tag($tag, $out, $divOptions); } return $out; }/** * Creates a checkbox input widget. * * @param string $fieldNamem Name of a field, like this "Modelname.fieldname", "Modelname/fieldname" is deprecated * @param array $options Array of HTML attributes. * 'value' the value of the checkbox * @return string An HTML text input element */ function checkbox($fieldName, $options = array()) { $value = 1; if (isset($options['value'])) { $value = $options['value']; unset($options['value']); } $options = $this->__initInputField($fieldName, $options); $this->__secure(); $model = $this->model(); if (ClassRegistry::isKeySet($model)) { $object =& ClassRegistry::getObject($model); } $output = null; if (isset($object) && isset($options['value']) && ($options['value'] == 0 || $options['value'] == 1)) { $db =& ConnectionManager::getDataSource($object->useDbConfig); $value = $db->boolean($options['value'], false); $options['value'] = 1; } $output = $this->hidden($fieldName, array('value' => '0', 'id' => $options['id'] . '_'), true); if (isset($options['value']) && $value == $options['value']) { $options['checked'] = 'checked'; } elseif (!empty($value)) { $options['value'] = $value; } $output .= sprintf($this->Html->tags['checkbox'], $options['name'], $this->_parseAttributes($options, array('name'), null, ' ')); return $this->output($output); }/** * Creates a set of radio widgets. * * @param string $fieldName Name of a field, like this "Modelname.fieldname" * @param array $options Radio button options array. * @param array $attributes Array of HTML attributes. Use the 'separator' key to * define the string in between the radio buttons * @return string */ function radio($fieldName, $options = array(), $attributes = array()) { $attributes = $this->__initInputField($fieldName, $attributes); $this->__secure(); $legend = false; if (isset($attributes['legend'])) { $legend = $attributes['legend']; unset($attributes['legend']); } elseif (count($options) > 1) { $legend = __(Inflector::humanize($this->field()), true); } $label = true; if (isset($attributes['label'])) { $label = $attributes['label']; unset($attributes['label']); } $inbetween = null; if (isset($attributes['separator'])) { $inbetween = $attributes['separator']; unset($attributes['separator']); } if (isset($attributes['value'])) { $value = $attributes['value']; } else { $value = $this->value($fieldName); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?