You are here

public function DoubleFieldWidget::getFormValidators in Double Field 7.2

Create widget form validators.

File

tests/double_field_widget.inc, line 188
The file contains a class that assists to test Double field widget types.

Class

DoubleFieldWidget
Helper class to test Double field widget types.

Code

public function getFormValidators($field_name) {
  $prefix = "//div[@id='edit-{$field_name}']";
  $class = $this->settings['inline'] ? 'double-field-elements container-inline form-wrapper' : 'double-field-elements form-wrapper';

  // Verify prefix and suffix.
  $validators[] = "{$prefix}//div[@class='{$class}' and starts-with(text(), {$this->settings['first']['general']['prefix']})]";
  $validators[] = "{$prefix}//div[@class='{$class}' and contains(text(), {$this->settings['first']['general']['suffix']})]";
  $validators[] = "{$prefix}//div[@class='{$class}' and contains(text(), {$this->settings['second']['general']['prefix']})]";

  // It seems that ends-with() is an XPath 2.0 function.
  $validators[] = "{$prefix}//div[@class='{$class}' and contains(text(), {$this->settings['second']['general']['suffix']})]";
  $prefix .= "//div[@class='{$class}']";
  foreach ($this->subwidgets as $index => $subwidget) {
    $name = $field_name . '[' . LANGUAGE_NONE . '][0][' . $index . ']';
    $value = $this->value[0][$index];

    // Check subwidget related elements.
    switch ($subwidget) {
      case 'textfield':
        $size = $this->settings[$index][$subwidget]['size'];
        $placeholder_value = $this->settings[$index][$subwidget]['placeholder'];
        $placeholder = $placeholder_value ? "@placeholder='{$placeholder_value}'" : 'not(@checked)';
        $validators[] = "{$prefix}//input[@name='{$name}' and @type='text' and @size='{$size}' and @value='{$value}' and {$placeholder}]";
        break;
      case 'textarea':
        $cols = $this->settings[$index][$subwidget]['cols'];
        $rows = $this->settings[$index][$subwidget]['rows'];
        $wrapper_class = $this->settings[$index][$subwidget]['resizable'] ? 'form-textarea-wrapper resizable' : 'form-textarea-wrapper';
        $placeholder_value = $this->settings[$index][$subwidget]['placeholder'];
        $placeholder = $placeholder_value ? "@placeholder='{$placeholder_value}'" : 'not(@checked)';
        $validators[] = "{$prefix}//div[@class='{$wrapper_class}']//textarea[@name='{$name}' and @cols='{$cols}' and @rows='{$rows}' and text()='{$value}' and {$placeholder}]";
        break;
      case 'select':
        $validators[] = "{$prefix}//select[@name='{$name}']//option[@value='{$value}' and @selected='selected']";
        break;
      case 'checkbox':
        $checked = $value ? '@checked' : 'not(@checked)';
        $validators[] = "{$prefix}//input[@name='{$name}' and @type='checkbox' and '{$checked}']";
        break;
    }
  }
  return $validators;
}