I'm developing a small app with CakePHP and I need to save a startdate and an enddate for an event. I went with the Bootstrap Daterangepicker to let users visually input the dates.
In my add.ctp view I have two fields:
<div class="form-group">
<?php echo $this->Form->input('start', array(
'class' => 'form-control',
'type' => 'text',
'id' => 'start',
'name' => 'start',
'data-mask')); ?>
</div><!-- .form-group -->
<div class="form-group">
<?php echo $this->Form->input('end', array(
'class' => 'form-control',
'type' => 'text',
'id' => 'end',
'name' => 'end',
'data-mask')); ?>
</div>
And the Javascript that controls the inputs:
<script type="text/javascript">
$(function() {
$('input[name="start"]').daterangepicker({
timePicker24Hour: true,
singleDatePicker: true,
timePicker: false,
timePickerIncrement: 30,
locale: {
format: 'YYYY-MM-DD HH:mm:ss'
}
});
$('input[name="end"]').daterangepicker({
timePicker24Hour: true,
singleDatePicker: true,
timePicker: false,
timePickerIncrement: 30,
locale: {
format: 'YYYY-MM-DD HH:mm:ss'
}
});
});
</script>
The picker works just fine, it updates the field values properly, but when I attempt to save the form, the start field gets written to the database as follows:
start 0000-00-00 00:00:00
end (null)
The form data for the start and end fields saves properly if I'm reverting to the default configuration of the fields.
Any clue as to where I'm failing would be highly appreciated.
try something like this
$correctdate = date("Y-m-d j:h:m",strtotime($yourdate));
It's not a great peace of code, but it should help to go further in the right direction.
We take this string and make a timestamp out of it. Then we change it to a proper datetime value.
Okay, so I was passing text to a datetime field and the save operation failed. In order to fix this I had to convert the strings to the proper type by processing it in the beforeSave() function of the model.
Here is the code that worked for me:
public function beforeSave($options = array()) {
if (!empty($this->data['Task']['start']) && !empty($this->data['Task']['end'])) {
$this->data['Task']['start'] = $this->dateFormatBeforeSave($this->data['Task']['start']);
$this->data['Task']['end'] = $this->dateFormatBeforeSave($this->data['Task']['end']);
}
return true;
}
public function dateFormatBeforeSave($dateString) {
return date('Y-m-d h:m:s', strtotime($dateString));
}
Related
I am using pikaday.js date picker.
I have moment.js loaded as well.
If i select 02/02/2022 it stored fine but when the page loads it changes changes the date to a random date.
var qualExpiry = new Pikaday({
field: document.getElementById('qualExpiry'),
firstDay: 1,
format: 'DD-MM-YYYY',
yearRange: [2010, 2040]
});
{!! Form::text('expiry_date', null, ['id' => 'qualExpiry', 'class' => 'form-control form-control-lg form-control-solid mb-3 mb-lg-0', 'placeholder' => 'Select expiry date']) !!}
if i remove format: 'DD-MM-YYYY' and use the default ISO format it works fine
Hi everyone i need your help.
I put dates in Jquery in my form with the id:
twig :
{{ form_label(Form.date, 'Date') }}
{{ form_widget(Form.date, {'id': 'datejquery', 'attr': {'class' :'form-control'}}) }}
formType :
$builder->add('date', DateType::class, ['label' => 'Date', 'widget' => 'single_text', 'html5' => false, 'format' => 'dd/MM/yyyy']);
The code works with 'format' => 'dd/MM/yyyy'
But the display of the datepicker no longer works when I click on the form fields.
I just have the date that appears in the input but I don't have the calendar display...
When : {'class' :'js-datepicker'}})
display empty calendar
I'm trying to use a jquery datetimepicker.
And I have some trouble when I submit the form:
This value is not valid. : Unable to reverse value for property path "debut": Date parsing failed: U_PARSE_ERROR
Here is my form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('debut', 'datetime', array(
'widget' => 'single_text',
'label' => 'Debut',
'html5' => false,
'format' => 'dd/MM/yyyy H:m'
))
//...
And the javascript is:
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.4/build/jquery.datetimepicker.full.min.js"></script>
<script>
$(document).ready(function () {
$( '#mybundle_evenement_debut' ).datetimepicker({
startDate: new Date(),
format: 'd/m/y H:i',
autoclose: true,
});
});
</script>
How would you fix this?
I have following code in my view file:
<?= $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $model,
'attribute' => 'time',
'language' => 'ru',
'htmlOptions' => array(
'size' => '10',
'class' => 'form-control form_empty centered date',
),
'options' => array(
'dateFormat' => 'dd/mm/yy',
'showOn' => 'focus',
'showOtherMonths' => true,
'selectOtherMonths' => true,
'changeMonth' => true,
'changeYear' => true,
'showButtonPanel' => true,
'closeText' => Yii::t('ablock', 'Очистить'),
'beforeShow' => 'js:
function( input ) {
setTimeout(function() {
var clearButton = $(input ).datepicker( "widget" ).find( ".ui-datepicker-close" );
clearButton.unbind("click").bind("click",function(){ $.datepicker._clearDate( input );});
}, 1 )}',
),
),
true) ?>
I used this code in order to allow user to enter date. However, it is accepting 0 values. For example, if user enters incomplete date such as 0d/mm/yyyy (which is incorret date), it will accept it as true. I need to not allow, if user enters wrong value. How can I do it?
I haven't found any params for accepting only complete dates. Suggested workarounds:
AJAX validation for the date field in your model
disable field input, so the user is limited to the datepicker
perform validation via overriding date method (see example: https://stackoverflow.com/a/21898124/5992898)
I am new to zendframework2. I wanna to insert time from time picker. I did a lot of practice, but time picker is not displayed when clicking on form field for selecting time..
form:
$this->add(array(
'type' => 'Zend\Form\Element\Time',
'name' => 'departure_time',
'attributes' => array(
'id' => 'departure_time',
'required' => 'required',
'type' => 'text',
),
'options' => array(
'label' => 'Departure Time ',
),
));
view :
echo $this->form()->openTag($form);
echo $this->formRow($form->get('departure_time'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
<script type="text/javascript">
$(document).ready(function() {
$( ".departure_time" ).datepicker({
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true
});
});
</script>
Is there need to call jquery function through onclick event from form.
Thanks in advance. Please
It looks like you are passing the wrong selector string into your jQuery function.
Since you have
'id' => 'departure_time',
It should be:
<script type="text/javascript">
$(document).ready(function() {
$( "#departure_time" ).datepicker({
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true
});
});
</script>
Or you need to add class of departure_time to your ZF2 form config
**** UPDATE ****
You can add a class the same way as an id, for your code it would be:
$this->add(array(
'type' => 'Zend\Form\Element\Time',
'name' => 'departure_time',
'attributes' => array(
'class' => 'departure_time',
'required' => 'required',
'type' => 'text',
),
'options' => array(
'label' => 'Departure Time ',
),
));