Automatic generation of HTML FORMs from given data.
Inheritance: extends Cake\View\Helper, use trait Cake\View\Helper\IdGeneratorTrait, use trait Cake\View\Helper\SecureFieldTokenTrait, use trait Cake\View\StringTemplateTrait
 /**
  * Add prefix to field name if a prefix was set using FormHelper::prefix().
  *
  * @param \Cake\View\Helper\FormHelper $helper Field helper instance
  * @param string $name Field name
  * @return string Prefixed field name
  */
 protected function _fieldName(FormHelper $helper, $name)
 {
     $prefix = $helper->prefix();
     if (!empty($prefix) && strpos($name, $prefix) !== 0) {
         $name = "{$prefix}{$name}";
     }
     return $name;
 }
示例#2
0
 /**
  * @return void
  */
 public function testFormInput()
 {
     $Form = new FormHelper(new View());
     $entity = $this->Table->newEntity();
     $Form->create($entity);
     $x = $Form->input('year_of_birth', ['type' => 'year']);
     $this->assertContains('<select name="year_of_birth[year]" type="year"', $x);
     // <div class="input number"><label for="year-of-birth">Year Of Birth</label><input type="number" name="year_of_birth" id="year-of-birth"/></div>
 }
 protected function _getInput($fieldName, $options)
 {
     if (isset($options['type']) && !in_array($options['type'], ['radio', 'checkbox', 'datetime'])) {
         $options = $this->addClass($options, 'form-control');
     }
     return parent::_getInput($fieldName, $options);
 }
示例#4
0
 public function input($fieldName, array $options = array())
 {
     $defaultOptions = ['label' => false, 'div' => false, 'class' => 'form-control'];
     if ($this->useDefaults()) {
         return parent::input($fieldName, (array) array_merge($defaultOptions, $options));
     } else {
         return parent::input($fieldName, $options);
     }
 }
 public function input($fieldName, array $options = [])
 {
     if (!isset($options['type']) && !isset($options['options'])) {
         $modelKey = $this->model();
         if (preg_match('/^enum\\((.+)\\)$/ui', $this->fieldset[$modelKey]['fields'][$fieldName]['type'], $m)) {
             $match = trim($m[1]);
             $qOpen = substr($match, 0, 1);
             $qClose = substr($match, -1);
             $delimiter = $qOpen . ',' . $qClose;
             preg_match('/^' . $qOpen . '(.+)' . $qClose . '$/u', $match, $m);
             $_options = explode($delimiter, $m[1]);
             $options['type'] = 'select';
             $options['options'] = array_combine($_options, $_options);
         }
     }
     return parent::input($fieldName, $options);
 }
示例#6
0
 /**
  * {@inheritDoc}
  *
  * Allows to render Field (EAV virtual columns) in edit mode; it triggers the
  * event `Field.<handler>.Entity.edit`, it will try to append an `*` symbol to
  * input label if Field has been marked as "required".
  */
 public function input($fieldName, array $options = [])
 {
     if ($fieldName instanceof \Field\Model\Entity\Field) {
         if (!$this->_isRendering) {
             $this->_isRendering = true;
             $result = $fieldName->edit($this->_View);
             $this->_isRendering = false;
             return $result;
         } else {
             $options += ['value' => $fieldName->value, 'label' => $fieldName->label];
             if ($fieldName->metadata->required) {
                 $options['label'] .= ' *';
                 $options['required'] = 'required';
             }
             $fieldName = $fieldName->get('name');
         }
     }
     return parent::input($fieldName, $options);
 }
示例#7
0
 public function input($fieldName, array $options = [])
 {
     $context = $this->_getContext();
     $explodedFieldName = explode('.', $fieldName);
     if (!method_exists($context, 'entity')) {
         return parent::input($fieldName, $options);
     }
     $errors = $context->entity()->errors($explodedFieldName[0]);
     if (is_array($errors) && !empty($errors) && empty($this->error($fieldName))) {
         if (isset($errors[$explodedFieldName[1]][$explodedFieldName[2]])) {
             $error = array_values($errors[$explodedFieldName[1]][$explodedFieldName[2]])[0];
             $options['templates']['inputContainer'] = '<div class="input {{type}} required error">{{content}} <div class="error-message">' . $error . '</div></div>';
         }
     }
     if (isset($options['type']) && $options['type'] === 'translations') {
         unset($options['type']);
         $I18ns = TableRegistry::get('I18ns');
         $langs = $I18ns->find('list', ['keyField' => 'id', 'valueField' => 'locale', 'groupField' => 'is_required'])->toArray();
         $output = null;
         $fieldNames = [1 => 'translations', 0 => '_translations'];
         // ['required', 'not_required']
         foreach ($fieldNames as $key => $name) {
             if (array_key_exists($key, $langs)) {
                 foreach ($langs[$key] as $lang) {
                     $myOptions = $options;
                     if (!array_key_exists('required', $options)) {
                         $myOptions['required'] = $key === 1 ? true : false;
                     }
                     if (isset($context->entity()->_translations[$lang]->{$fieldName})) {
                         $myOptions['value'] = $context->entity()->_translations[$lang]->{$fieldName};
                     }
                     $myOptions['label'] = $lang . ' ' . $fieldName;
                     // Fix label
                     $output .= $this->input($name . '.' . $lang . '.' . $fieldName, $myOptions);
                 }
             }
         }
         return $output;
     }
     return parent::input($fieldName, $options);
 }
示例#8
0
 /**
  * Test that when specifying custom widgets config file and it should be
  * added to widgets array. WidgetRegistry will load widgets in constructor.
  *
  * @return void
  */
 public function testConstructWithWidgetsConfig()
 {
     $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
     $registry = $helper->widgetRegistry();
     $this->assertInstanceOf('Cake\\View\\Widget\\LabelWidget', $registry->get('text'));
 }
示例#9
0
 /**
  * Generates input options array
  *
  * @param string $fieldName The name of the field to parse options for.
  * @param array $options Options list.
  * @return array Options
  */
 protected function _parseOptions($fieldName, $options)
 {
     $options = parent::_parseOptions($fieldName, $options);
     $options += ['id' => $this->_domId($fieldName)];
     if (is_string($options['label'])) {
         $options['label'] = ['text' => $options['label']];
     }
     return $options;
 }
 public function dateTime($fieldName, array $options = [])
 {
     $options = $this->_injectStyles($options, 'form-control');
     return parent::dateTime($fieldName, $options);
 }
示例#11
0
 public function button($title, array $options = array())
 {
     $options = $this->addClass($options, 'btn btn-default');
     return parent::button($title, $options);
 }
 /**
  * 
  * Create & return a Twitter Like submit input.
  * 
  * New options:
  * 	- bootstrap-type: Twitter bootstrap button type (primary, danger, info, etc.)
  * 	- bootstrap-size: Twitter bootstrap button size (mini, small, large)
  * 
  * Unusable options: div
  * 
  **/
 public function submit($caption = null, array $options = array())
 {
     $options = $this->_addButtonClasses($options);
     return parent::submit($caption, $options);
 }
示例#13
0
 public function textarea($fieldName, array $options = array())
 {
     $default_options = ['autosize' => true];
     $options = array_merge($default_options, $options);
     $script_block = null;
     if ($options['autosize']) {
         $this->AlaxosHtml->includeTextareaAutosizeJS();
         if (isset($options['id'])) {
             $dom_id = $options['id'];
         } else {
             $dom_id = $this->_domId($fieldName);
         }
         $script = [];
         $script[] = $this->AlaxosHtml->config('jquery_variable') . '(document).ready(function(){';
         $script[] = '  if(typeof(' . $this->AlaxosHtml->config('jquery_variable') . '("#' . $dom_id . '").autosize) != "undefined"){';
         $script[] = '    ' . $this->AlaxosHtml->config('jquery_variable') . '("#' . $dom_id . '").autosize();';
         $script[] = '  }';
         $script[] = '});';
         $script_block = $this->AlaxosHtml->scriptBlock(implode("\n", $script));
     }
     unset($options['autosize']);
     return parent::textarea($fieldName, $options) . $script_block;
 }
 /**
  *
  * Create & return a Twitter Like submit input.
  *
  * New options:
  * 	- bootstrap-type: Twitter bootstrap button type (primary, danger, info, etc.)
  * 	- bootstrap-size: Twitter bootstrap button size (mini, small, large)
  *
  * Unusable options: div
  *
  **/
 public function submit($caption = null, array $options = array())
 {
     return parent::submit($caption, $this->_createButtonOptions($options));
 }
示例#15
0
 public function __construct(View $View, array $config = [])
 {
     parent::__construct($View, $config);
 }
示例#16
0
 /**
  * Create and render ace checkbox.
  *
  * @param string $fieldName
  * @param array $options
  * @return array|string
  */
 public function checkbox($fieldName, array $options = [])
 {
     $viewForm = $this->_View->Form;
     $options = $this->addClass($options, 'ace');
     if (is_object($viewForm->_context)) {
         if (!is_null($viewForm->_context->val($fieldName))) {
             $options['val'] = $viewForm->_context->val($fieldName);
         }
         if (!in_array($fieldName, $viewForm->fields)) {
             $this->_View->Form->fields[] = $fieldName;
         }
     }
     return parent::checkbox($fieldName, $options);
 }
示例#17
0
 /**
  * Reload core input.
  *
  * @param string $fieldName
  * @param array $options
  * @return string
  */
 public function input($fieldName, array $options = [])
 {
     //  Set label tooltip.
     if (isset($options['label']['tooltip'])) {
         $_toolTipPos = 'top';
         if (isset($options['label']['tooltipPosition'])) {
             $_toolTipPos = $options['label']['tooltipPosition'];
             unset($options['label']['tooltipPosition']);
         }
         if (is_string($options['label']['tooltip']) && !isset($options['label']['title'])) {
             $options['label']['title'] = $options['label']['tooltip'];
         }
         $toolTipOptions = ['label' => ['data-toggle' => 'tooltip', 'data-placement' => $_toolTipPos, 'title' => !isset($options['label']['title']) ? (string) $options['label']['tooltip'] : $options['label']['title']]];
         unset($options['label']['tooltip']);
         $options = Hash::merge($toolTipOptions, $options);
         if (!isset($options['label']['text'])) {
             $options['label']['text'] = $options['label']['title'];
         }
     }
     return parent::input($fieldName, $options);
 }
示例#18
0
 /**
  * Test construct() with the templates option.
  *
  * @return void
  */
 public function testConstructTemplatesFile()
 {
     $helper = new FormHelper($this->View, ['templates' => 'htmlhelper_tags.php']);
     $result = $helper->input('name');
     $this->assertContains('<input', $result);
 }
 /**
  * Generates an group template element
  *
  * @param array $options The options for group template
  * @return string The generated group template
  */
 protected function _groupTemplate($options)
 {
     if (isset($options['options']['formGroup']) && $options['options']['formGroup'] === false) {
         $groupTemplate = 'plainFormGroup';
         return $this->templater()->format('plainFormGroup', ['input' => $options['input'], 'label' => $options['label'], 'error' => $options['error']]);
     }
     return parent::_groupTemplate($options);
 }
示例#20
0
 /**
  * Creates a textarea widget
  * @param string $fieldName Field name, should be "Modelname.fieldname"
  * @param array $options HTML attributes and options
  * @return string
  */
 public function textarea($fieldName, array $options = [])
 {
     $options = $this->optionsDefaults(['cols' => null, 'rows' => null], $options);
     return parent::textarea($fieldName, $options);
 }
 /**
  * Construct the widgets and binds the default context providers.
  *
  * @param \Cake\View\View $View The View this helper is being attached to.
  * @param array $config Configuration settings for the helper.
  */
 public function __construct(View $View, array $config = [])
 {
     $this->_defaultWidgets = $this->_widgets + $this->_defaultWidgets;
     parent::__construct($View, $config);
 }
 public function reset($title, array $options = array())
 {
     $options = $this->addClass($options, 'btn btn-link');
     return parent::button($title, $options);
 }
示例#23
0
 public function __construct(\Cake\View\View $View, array $config = [])
 {
     $this->_defaultConfig['templates'] = array_merge($this->_defaultConfig['templates'], $this->templates);
     parent::__construct($View, $config);
 }
示例#24
0
 /**
  * Overwrite to allow FormConfig Configure settings to be applied.
  *
  * @param mixed $model The context for which the form is being defined. Can
  *   be an ORM entity, ORM resultset, or an array of meta data. You can use false or null
  *   to make a model-less form.
  * @param array $options An array of html attributes and options.
  * @return string An formatted opening FORM tag.
  */
 public function create($model = null, array $options = [])
 {
     $defaults = ['novalidate' => $this->_defaultConfig['novalidate']];
     $options += $defaults;
     return parent::create($model, $options);
 }
示例#25
0
 /**
  * Returns a formatted SELECT element.
  *
  * @param string $fieldName
  * @param array $options
  * @param array $attributes
  * @return string
  */
 public function select($fieldName, $options = [], array $attributes = [])
 {
     $attributes = $this->addClass($attributes, 'form-control');
     return parent::select($fieldName, $options, $attributes);
 }