/** * Constructor * * Create a new Length field object * * @param FormHandler $form The form where this field is located on * @param string $name The name of the field * @return \FormHandler\Field\Length * @author Marien den Besten */ public function __construct(FormHandler $form, $name) { $this->length = new \FormHandler\Field\Number($form, $name . '_value'); $this->length->setValidator(function ($value) { return trim($value) == '' || is_numeric($value); }); $this->unit = new \FormHandler\Field\Select($form, $name . '_unit'); $this->unit->setOptions($this->units); //Classes $this->length->setExtra('class="length-field"'); $this->unit->setExtra('class="length-field"'); parent::__construct($form, $name)->setFocusName($name . '_value'); $form->_setJS('' . "var el = \$('#" . $name . "_unit');\n" . "el.data('oldvalue', el.val());\n" . "el.on('change',function()\n" . "{\n" . " var from_value = \$(this).data('oldvalue'),\n" . " to_value = \$(this).val(),\n" . " length = \$('#" . $name . "_value').val(),\n" . " conversion = " . json_encode($this->conversion_table) . ";\n" . " if(from_value in conversion && to_value in conversion[from_value])\n" . " {\n" . " \$('#" . $name . "_value').val(Math.round((length*100)*conversion[from_value][to_value])/100);" . " }\n" . " \$(this).data('oldvalue',to_value);\n" . "});\n", false, false); return $this; }
/** * Constructor * * @param FormHandler $form The form where this field is located on * @param string $name The name of the field * @return Percentage * @author Marien den Besten */ public function __construct(FormHandler $form, $name) { return parent::__construct($form, $name)->setMin(0)->setExtra('class="percentage-field"')->setValidator(new \FormHandler\Validator\Float()); }