I try to make a \Phalcon\Forms\Element with a date-range picker. How do I bind it to several fields in the model?
In my model I have:
$model->time_start (datetime)
$model->time_end (datetime)
I want to create a DateTimeInput element with the ID "time", that is going to load the default values from model with fields "time_start" and "time_end". The problem is also with $form->bind($_POST, $entity);
class DateTimeInput extends \Phalcon\Forms\Element\Text {
public function __construct($elementName) {
$this->setName($elementName);
}
public function render($attributes = null) {
$attributes = $this->prepareAttributes($attributes);
if(!empty($attributes)) {
foreach($attributes as $attrName => $attrValue) {
$this->setAttribute($attrName, $attrValue);
}
}
$attributes = $this->getAttributes();
$date_format = (!empty($attributes) && array_key_exists('data-date-format', $attributes) ? $attributes['data-date-format'] : 'dd.mm.yyyy');
$html = '<div class="input-group input-large date-picker input-daterange" data-date-format="'.$date_format.'"><input type="text" name="'.$this->getName().'_start"'; // data-date="10/11/2012"
// write every attributes of select element in DOM
$input_id = false;
if(!empty($attributes)) {
foreach($attributes as $attrName => $attrValue) {
if($attrName == '0') { $attrName = 'id'; $input_id = $attrValue.'_start'; }
if($attrName == 'class') $attrValue .= ' input_postcode';
if($attrName == 'data-date-format') continue;
$html .= ' '.$attrName.'="'.$attrValue.'"';
}
}
$html .= ' />';
$html .= '<span class="input-group-addon"> '._('to').' </span><input type="text" name="'.$this->getName().'_end"'; // data-date="10/11/2012"
// write every attributes of select element in DOM
$attributes = $this->getAttributes();
$input_id = false;
if(!empty($attributes)) {
foreach($attributes as $attrName => $attrValue) {
if($attrName == '0') { $attrName = 'id'; $input_id = $attrValue.'_end'; }
if($attrName == 'class') $attrValue .= ' input_postcode';
$html .= ' '.$attrName.'="'.$attrValue.'"';
}
}
$html .= ' />';
$html .= '</div>';
\Phalcon\Di::getDefault()->get('assets')->collection('css_extra')
->addCss('assets/global/plugins/bootstrap-datepicker/css/datepicker3.css')
->addCss('assets/global/plugins/bootstrap-daterangepicker/daterangepicker-bs3.css')
->join(false);
\Phalcon\Di::getDefault()->get('assets')->collection('js_extra')
->addJs('assets/global/plugins/bootstrap-datepicker/js/bootstrap-datepicker.js')
->addJs('assets/global/plugins/bootstrap-daterangepicker/daterangepicker.js')
->join(false);
//&callback=?
$html .= '<script type="text/javascript">
$(function() {
if (jQuery().datepicker) {
$(\'.date-picker\').datepicker({
rtl: Metronic.isRTL(),
orientation: "left",
autoclose: true
});
}
});
</script>';
return $html;
}
}
In my Form:
class ProjectForm extends \Phalcon\Forms\Form {
public function initialize() {
$timespan = new DateTimeInput("time");
$timespan->setLabel(_('Timespan'));
$timespan->setAttribute('data-date-format', 'dd.mm.yyyy');
$this->add($timespan);
}
}