Last active
March 28, 2017 12:13
-
-
Save anton-kotik/dc4ff7969534cb7626a99326e7fdc132 to your computer and use it in GitHub Desktop.
DV Form Template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Dostavista\Framework\Form\FormAbstract; | |
use Zend_Form_Decorator_Label; | |
/** | |
* @property Zend_Form_Element_Text $text_field_name | |
*/ | |
class TemplateForm extends FormAbstract { | |
/** | |
* Поля, которые будут принудительно преобразованы в NULL, если не были заполнены. | |
* Необходимо использовать метод $form->getValuesMapped() | |
*/ | |
protected $nullableFields = [ | |
'field_name', | |
]; | |
public function init() { | |
parent::init(); | |
$this->addText('text_field_name', [ | |
'label' => 'Field name', | |
'filters' => ['StringTrim'], | |
'validators' => [ | |
['StringLength', true, ['min' => 0, 'max' => 255, 'encoding' => 'UTF-8']], | |
], | |
'maxlength' => 255, | |
'placeholder' => 'placeholder', | |
]); | |
$this->addCheckbox('checkbox_field_name', [ | |
'label' => 'Field name', | |
'filters' => ['Int'], | |
]); | |
$this->addSelect('type', [ | |
'required' => true, | |
'label' => 'Robot type', | |
'multiOptions' => ['' => '(all)'] + RobotsTable::getTypeNames(), | |
]); | |
$this->addText('priority', [ | |
'label' => 'Priority', | |
'description' => '', | |
'maxlength' => 5, | |
'filters' => ['Int'], | |
'validators' => [ | |
['Between', false, [0, 99999]], | |
], | |
]); | |
// Настройка декоратора Label: отключение экранирования HTML тегов | |
$this->getElement('text_field_name')->getDecorator('Label')->setOption('escape', false); | |
// Настройка декоратора Label | |
$this->getElement('checkbox_field_name')->getDecorator('label')->setOptions([ | |
'placement' => 'append', | |
'escape' => false, | |
]); | |
// DisplayGroup | |
$this->addDisplayGroup($elements, 'settings', ['legend' => 'Robot settings']); | |
// DisplayGroup | |
$this->addDisplayGroup( | |
[ | |
'is_checkbox_field_name_1', | |
'is_checkbox_field_name_2', | |
], | |
'show-to', | |
[ | |
'legend' => 'Show to:', | |
'class' => 'form-element' | |
] | |
); | |
$this->addSubmit('Save'); | |
} | |
public function isValid($data) { | |
// Добавляем ручную проверку того, что хотя бы один чекбокс выбран | |
$isValid = parent::isValid($data); | |
if ($data['is_checkbox_field_name_1'] || $data['is_checkbox_field_name_2']) { | |
return $isValid; | |
} | |
$this->getElement('is_checkbox_field_name_1')->addError("You should choose at least one of the client types"); | |
$this->getElement('is_checkbox_field_name_2')->addError("You should choose at least one of the client types"); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment