/**
  * @param Field $field
  *
  * @return array
  */
 public function resolve(Field $field)
 {
     $defaults = ['required' => false, 'label' => $field->getLabel()];
     $definition = $this->registry->findDefinitionFor($field->getDocument());
     $attributes = $definition->getAttributes($field);
     return array_merge($defaults, $this->trim($attributes));
 }
 /**
  * Renders a field.
  *
  * @param Field $field A field.
  *
  * @return string (X)HTML
  */
 protected function renderField(Field $field)
 {
     $name = 'advfrm-' . $field->getName();
     $o = '<tr><td class="label">' . XH_hsc($field->getLabel()) . '</td><td class="field">';
     if (isset($_POST[$name])) {
         if (is_array($_POST[$name])) {
             foreach ($_POST[$name] as $val) {
                 $o .= '<div>' . XH_hsc(stsl($val)) . '</div>';
             }
         } else {
             $o .= $this->nl2br(XH_hsc(stsl($_POST[$name])));
         }
     } elseif (isset($_FILES[$name])) {
         $o .= stsl($_FILES[$name]['name']);
     }
     $o .= '</td></tr>' . PHP_EOL;
     return $o;
 }
Exemple #3
0
 protected static function renderButtonInput(Field $field)
 {
     return sprintf('<button type="%s" name="%s" value="%s" %s>%s</button>', $field->getType(), $field->getName(), $field->getValue(), $field->getAttributes(), $field->getLabel());
 }
 /**
  * Returns the label of a field.
  *
  * @param Field $field A field.
  *
  * @return string
  *
  * @global array The configuration of the plugins.
  */
 protected function getLabel(Field $field)
 {
     global $plugin_cf;
     if ($field->getType() == 'hidden') {
         $label = '';
     } elseif (!$field->isRequired()) {
         $label = $field->getLabel();
     } else {
         $label = sprintf($plugin_cf['advancedform']['required_field_mark'], $field->getLabel());
     }
     if ($this->isLabelled($field)) {
         $label = '<label for="advfrm-' . $this->id . '-' . $field->getName() . '">' . $label . '</label>';
     }
     return $label;
 }
 /**
  * Validates a filled in field.
  *
  * @return string (X)HTML.
  */
 protected function validateFilledInField()
 {
     $o = '';
     switch ($this->field->getType()) {
         case 'from':
         case 'mail':
             if (!preg_match($this->config['mail_regexp'], stsl($_POST[$this->name]))) {
                 $o .= '<li>' . sprintf($this->l10n['error_invalid_email'], XH_hsc($this->field->getLabel())) . '</li>' . PHP_EOL;
                 Controller::focusField($this->formId, $this->name);
             }
             break;
         case 'date':
             $pattern = '/^([0-9]+)\\' . $this->l10n['date_delimiter'] . '([0-9]+)\\' . $this->l10n['date_delimiter'] . '([0-9]+)$/';
             $matched = preg_match($pattern, stsl($_POST[$this->name]), $matches);
             if (count($matches) == 4) {
                 $month = $matches[strpos($this->l10n['date_order'], 'm') + 1];
                 $day = $matches[strpos($this->l10n['date_order'], 'd') + 1];
                 $year = $matches[strpos($this->l10n['date_order'], 'y') + 1];
             }
             if (!$matched || !checkdate($month, $day, $year)) {
                 $o .= '<li>' . sprintf($this->l10n['error_invalid_date'], XH_hsc($this->field->getLabel())) . '</li>' . PHP_EOL;
                 Controller::focusField($this->formId, $this->name);
             }
             break;
         case 'number':
             if (!ctype_digit(stsl($_POST[$this->name]))) {
                 $o .= '<li>' . sprintf($this->l10n['error_invalid_number'], XH_hsc($this->field->getLabel())) . '</li>' . PHP_EOL;
                 Controller::focusField($this->formId, $this->name);
             }
             break;
         case 'file':
             switch ($_FILES[$this->name]['error']) {
                 case UPLOAD_ERR_OK:
                     if ($this->field->getMaxLength() && $_FILES[$this->name]['size'] > $this->field->getMaxLength()) {
                         $o .= '<li>' . sprintf($this->l10n['error_upload_too_large'], XH_hsc($this->field->getLabel())) . '</li>' . PHP_EOL;
                         Controller::focusField($this->formId, $this->name);
                     }
                     break;
                 case UPLOAD_ERR_INI_SIZE:
                 case UPLOAD_ERR_FORM_SIZE:
                     $o .= '<li>' . sprintf($this->l10n['error_upload_too_large'], XH_hsc($this->field->getLabel())) . '</li>' . PHP_EOL;
                     Controller::focusField($this->formId, $this->name);
                     break;
                 default:
                     $o .= '<li>' . sprintf($this->l10n['error_upload_general'], XH_hsc($this->field->getLabel())) . '</li>' . PHP_EOL;
                     Controller::focusField($this->formId, $this->name);
             }
             $ext = pathinfo($_FILES[$this->name]['name'], PATHINFO_EXTENSION);
             if ($this->field->getFileTypes() != '' && !in_array($ext, explode(',', $this->field->getFileTypes()))) {
                 $o .= '<li>' . sprintf($this->l10n['error_upload_illegal_ftype'], XH_hsc($this->field->getLabel()), XH_hsc($ext)) . '</li>' . PHP_EOL;
                 Controller::focusField($this->formId, $this->name);
             }
             break;
         case 'custom':
             $pattern = $this->field->getConstraint();
             if (!empty($pattern) && !preg_match($pattern, stsl($_POST[$this->name]))) {
                 $msg = $this->field->getErrorMessage() != '' ? $this->field->getErrorMessage() : $this->l10n['error_invalid_custom'];
                 $o .= '<li>' . sprintf($msg, $this->field->getLabel()) . '</li>' . PHP_EOL;
                 Controller::focusField($this->formId, $this->name);
             }
     }
     return $o;
 }
 /**
  * Renders a field.
  *
  * @param Field $field A field.
  *
  * @return string
  */
 protected function renderField(Field $field)
 {
     $name = 'advfrm-' . $field->getName();
     $o = $field->getLabel() . PHP_EOL;
     if (isset($_POST[$name])) {
         if (is_array($_POST[$name])) {
             foreach ($_POST[$name] as $val) {
                 $o .= '  ' . stsl($val) . PHP_EOL;
             }
         } else {
             $o .= '  ' . $this->indent(stsl($_POST[$name])) . PHP_EOL;
         }
     } elseif (isset($_FILES[$name])) {
         $o .= '  ' . stsl($_FILES[$name]['name']) . PHP_EOL;
     }
     return $o;
 }