Esempio n. 1
0
 /**
  * Initialize a form. This is usually done in the context of a view/controller in your application. When a form is
  * constructed, the fields populate their input based on the form
  *
  * @param array $options
  */
 public function __construct(array $options = [])
 {
     // Update the meta values for this form's meta object with a user-supplied array
     // of key=>value pairs mapping to properties on this form's meta object
     if (array_key_exists('meta', $options) && is_array($options['meta'])) {
         $this->meta->updateValues($options['meta']);
     }
     // if CSRF is enabled on the Meta field, then generate a CSRF field
     // automagically and attach it to the form
     if ($this->meta && $this->meta->csrf) {
         $this->csrf = $this->meta->buildCSRF($this);
         list($csrf_name, $csrf_field) = $this->csrf->setupForm($this);
         if (!property_exists($csrf_field->csrf_impl, 'form_meta') || !$csrf_field->csrf_impl->form_meta) {
             $csrf_field->csrf_impl->form_meta = $this->meta;
         }
         $this->__set($csrf_name, $csrf_field);
     }
     if (array_key_exists('prefix', $options)) {
         $this->prefix = $options['prefix'];
     }
     // Normalize the end of the prefix to contain only one dash
     if ($this->prefix && !str_contains("-_;:/.", substr($this->prefix, -1))) {
         $this->prefix .= "-";
     }
 }
Esempio n. 2
0
 /**
  * Render this field as HTML, using keyword args as additional attributes
  *
  * This delegates rendering to {@link WTForms\DefaultMeta\render_field}
  * whose default behavior is to call the field's widget, passing any
  * keyword arguments from this call to the widget.
  *
  * In all of WTForms HTML widgets, the keyword arguments are turned to
  * HTML attributes, though in theory a widget is free to do anything it
  * wants with the supplied keyword arguments, and widgets don't have to
  * even do anything related to HTML
  *
  * @param array $options
  *
  * @return mixed
  */
 public function __invoke($options = [])
 {
     return $this->meta->renderField($this, $options);
 }