Cake\View\Helper\FormHelper::create PHP Метод

create() публичный Метод

### Options: - type Form method defaults to autodetecting based on the form context. If the form context's isCreate() method returns false, a PUT request will be done. - method Set the form's method attribute explicitly. - action The controller action the form submits to, (optional). Use this option if you don't need to change the controller from the current request's controller. Deprecated since 3.2, use url. - url The URL the form submits to. Can be a string or a URL array. If you use 'url' you should leave 'action' undefined. - encoding Set the accept-charset encoding for the form. Defaults to Configure::read('App.encoding') - enctype Set the form encoding explicitly. By default type => file will set enctype to multipart/form-data. - templates The templates you want to use for this form. Any templates will be merged on top of the already loaded templates. This option can either be a filename in /config that contains the templates you want to load, or an array of templates to use. - context Additional options for the context class. For example the EntityContext accepts a 'table' option that allows you to set the specific Table class the form should be based on. - idPrefix Prefix for generated ID attributes. - templateVars Provide template variables for the formStart template.
public create ( mixed $model = null, array $options = [] ) : string
$model mixed 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.
$options array An array of html attributes and options.
Результат string An formatted opening FORM tag.
    public function create($model = null, array $options = [])
    {
        $append = '';
        if (empty($options['context'])) {
            $options['context'] = [];
        }
        $options['context']['entity'] = $model;
        $context = $this->_getContext($options['context']);
        unset($options['context']);
        $isCreate = $context->isCreate();
        $options += ['type' => $isCreate ? 'post' : 'put', 'action' => null, 'url' => null, 'encoding' => strtolower(Configure::read('App.encoding')), 'templates' => null, 'idPrefix' => null];
        if (isset($options['action'])) {
            trigger_error('Using key `action` is deprecated, use `url` directly instead.', E_USER_DEPRECATED);
        }
        if ($options['idPrefix'] !== null) {
            $this->_idPrefix = $options['idPrefix'];
        }
        $templater = $this->templater();
        if (!empty($options['templates'])) {
            $templater->push();
            $method = is_string($options['templates']) ? 'load' : 'add';
            $templater->{$method}($options['templates']);
        }
        unset($options['templates']);
        if ($options['action'] === false || $options['url'] === false) {
            $url = $this->request->here(false);
            $action = null;
        } else {
            $url = $this->_formUrl($context, $options);
            $action = $this->Url->build($url);
        }
        $this->_lastAction($url);
        unset($options['url'], $options['action'], $options['idPrefix']);
        $htmlAttributes = [];
        switch (strtolower($options['type'])) {
            case 'get':
                $htmlAttributes['method'] = 'get';
                break;
                // Set enctype for form
            // Set enctype for form
            case 'file':
                $htmlAttributes['enctype'] = 'multipart/form-data';
                $options['type'] = $isCreate ? 'post' : 'put';
                // Move on
            // Move on
            case 'post':
                // Move on
            // Move on
            case 'put':
                // Move on
            // Move on
            case 'delete':
                // Set patch method
            // Set patch method
            case 'patch':
                $append .= $this->hidden('_method', ['name' => '_method', 'value' => strtoupper($options['type']), 'secure' => static::SECURE_SKIP]);
                // Default to post method
            // Default to post method
            default:
                $htmlAttributes['method'] = 'post';
        }
        if (isset($options['method'])) {
            $htmlAttributes['method'] = strtolower($options['method']);
        }
        if (isset($options['enctype'])) {
            $htmlAttributes['enctype'] = strtolower($options['enctype']);
        }
        $this->requestType = strtolower($options['type']);
        if (!empty($options['encoding'])) {
            $htmlAttributes['accept-charset'] = $options['encoding'];
        }
        unset($options['type'], $options['encoding']);
        $htmlAttributes += $options;
        $this->fields = [];
        if ($this->requestType !== 'get') {
            $append .= $this->_csrfField();
        }
        if (!empty($append)) {
            $append = $templater->format('hiddenBlock', ['content' => $append]);
        }
        $actionAttr = $templater->formatAttributes(['action' => $action, 'escape' => false]);
        return $this->formatTemplate('formStart', ['attrs' => $templater->formatAttributes($htmlAttributes) . $actionAttr, 'templateVars' => isset($options['templateVars']) ? $options['templateVars'] : []]) . $append;
    }

Usage Example

 public function create($model = null, array $options = [])
 {
     $options += ['role' => 'form'];
     if (isset($options['class']) and $options['class'] == 'form-horizontal') {
         $options['templates'] = $this->templates_horizontal;
     }
     return parent::create($model, $options);
 }
All Usage Examples Of Cake\View\Helper\FormHelper::create