Cake PHP date validation - javascript

I have the following code which allow users to add order details. And I'm using the datapicker jQuery to display a calendar for dates. For example, the order date field, I want to prevent users from choosing a future date. How can I do that?
My code is as follows:
edit.ctp:
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$("#datepicker").datepicker({
changeMonth: true,
changeYear: true,
yearRange: '1950:2020',
dateFormat: "yy-mm-dd"
});
$("#datepicker2").datepicker({
changeMonth: true,
changeYear: true,
yearRange: '1950:2020',
dateFormat: "yy-mm-dd"
});
});
</script>
</head>
<?php
$usertype = $this->SESSION->read('User.usertype');
?>
<body>
<div class="orders form">
<?php echo $this->Form->create('Order'); ?>
<fieldset>
<legend><?php echo __('Edit Order Details'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('invoice_number', array('required' => false));
echo $this->Form->input('order_date', array('required' => false, 'id' => 'datepicker', 'type' => 'text'));
echo $this->Form->input('payment_type', array('required' => false, 'options' => array('Cash' => 'Cash', 'EFTPOS' => 'EFTPOS', 'CC' => 'CC', 'Cheque' => 'Cheque', 'PayPal' => 'PayPal'), 'empty' => '(choose one)'));
echo $this->Form->input('order_type', array('required' => false, 'options' => array('Email' => 'Email', 'Telephone' => 'Telephone', 'Web' => 'Web'), 'empty' => '(choose one)'));
echo $this->Form->input('date_promised', array('required' => false, 'id' => 'datepicker2', 'type' => 'text'));
echo $this->Form->input('order_description', array('required' => false));
echo $this->Form->input('order_comments', array('required' => false));
echo $this->Form->input('order_total', array('required' => false));
echo $this->Form->input('amount_deposited', array('required' => false));
echo $this->Form->input('balance_due', array('required' => false));
/* echo $this->Form->input('customers_id', array('required'=>false));
echo $this->Form->input('employees_id', array('required'=>false));
*/
echo $this->Form->input('customers_id', array('label' => 'Customer Name', 'options' => $customers, 'label' => 'Customer Name', 'required' => false));
echo $this->Form->input('employees_id', array('label' => 'Employee name', 'options' => $employees, 'label' => 'Employee name', 'required' => false));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div
</body>
</html>
Can someone please help?

try this
$(function() { $("#datepicker").datepicker({
maxDate: '0'});
});
This will set the maxDate to +0 days from the current date (i.e. today).
Here is the reference

fiddle:
http://jsfiddle.net/pvXzb/
jquery:
$("#datepicker").datepicker({
changeMonth: true,
changeYear: true,
yearRange: '1950:2020',
dateFormat: "yy-mm-dd",
maxDate: 0
});

Related

lib fullCalendar, calendar isn't rendering in the page... i'm using contats and events in my calendar

my homepage is the callendar and look like this:
<?php
include_once("banco/conexao.php");
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<script src='https://cdn.jsdelivr.net/npm/fullcalendar#6.1.4/index.global.min.js'></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
header: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay,listYear'
},
views: {
listYear: {
type: 'listYear',
buttonText: 'Year'
}
},
initialView: 'dayGridMonth',
locale: 'pt-br',
events: [
<?php include 'eventos_json.php'; ?>
<?php include 'contatos_json.php'; ?>
]
});
calendar.render();
});
</script>
</head>
<body>
<div id='calendar'></div>
</body>
</html>
I created 2 files to prepare the event and contact data
my event_json.php file:
<?php
include_once 'banco/conexao.php';
// database connection
$conn = getConexao();
// recover events from database
$queryEventos = "SELECT * FROM eventos";
$resultEventos = mysqli_query($conn, $queryEventos);
// Convert event information into a format that FullCalendar understands
$eventos = array();
while ($evento = mysqli_fetch_assoc($resultEventos)) {
$nomeEvento = $evento['nomeEvento'];
$dataEvento = $evento['dataEvento'];
$horaEvento = $evento['horaEvento'];
$tipoEvento = $evento['tipoEvento'];
$organizadorEvento = $evento['organizadorEvento'];
$telefoneEvento = $evento['telefoneEvento'];
$localEvento = $evento['localEvento'];
$eventos[] = array(
'title' => $nomeEvento,
'start' => $dataEvento . 'T' . $horaEvento,
'extendedProps' => array(
'tipoEvento' => $tipoEvento,
'organizadorEvento' => $organizadorEvento,
'telefoneEvento' => $telefoneEvento,
'localEvento' => $localEvento,
),
);
}
mysqli_close($conn);
echo json_encode($eventos);
?>
my contact_json.php file
<?php
include_once 'banco/conexao.php';
$conn = getConexao();
$queryContatos = "SELECT * FROM contatos";
$resultContatos = mysqli_query($conn, $queryContatos);
$contatos = array();
while ($contato = mysqli_fetch_assoc($resultContatos)) {
$nomeContato = $contato['nomeContato'];
$dataNascContato = $contato['dataNascContato'];
$emailContato = $contato['emailContato'];
$telefoneContato = $contato['telefoneContato'];
$sexoContato = $contato['sexoContato'];
$contatos[] = array(
'title' => $nomeContato,
'start' => $dataNascContato,
'allDay' => true,
'extendedProps' => array(
'emailContato' => $emailContato,
'telefoneContato' => $telefoneContato,
'sexoContato' => $sexoContato,
),
);
}
// Retorna os contatos no formato JSON
mysqli_close($conn);
echo json_encode($contatos);
?>
i'm not from a english country, so my code is in another language (portuguese)
my localhost is like this,
the calendar is not showing :
im here because not even chatgbt can help me anymore

datetimepicker error submitting form

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?

How to insert time from time picker in zf2

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 ',
),
));

Executing nested <script> tags in PHP

I am working on a side project for my website to make the footer more easily editable in the Wordpress theme customizer, and use a dynamic copyright date, something I had in my previous version, but I wanted the values to be more easily editable.
I have created all of this and it works fine, but when I try to add in the dynamic date, it falls over as when I do this, I am trying to have a within a .
I have looked around quite a bit for an answer to this, but not knowing what to call it doesn't help.
I decided asking my question directly is probably the best thing to do.
So, inside a php script, I have all the values I need which work fine, and a at the bottom which uses jQuery to append HTML code to a certain area (after the #footer-info area).
I need to be able to use the small amount of code posted below in the area indicated, but have no idea how to do this as it is technically inside a jQuery string.
This is the main area which I need:
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#footer-info").text(' ');
jQuery('<p id="footer-info"><?php if( !empty($footer_one)) : ?><?php echo $footer_one; ?><?php endif; ?> <?php if( !empty($footer_two)) : ?><?php echo $footer_two; ?><?php endif; ?> | © Copyright <?php if( !empty($footer_three)) : ?><?php echo $footer_three; ?><?php endif; ?>**DYNAMIC DATE** <?php if( !empty($footer_three)) : ?><?php echo $footer_three; ?><?php endif; ?>. All Rights Reserved.</p>').insertAfter("#footer-info");
});
</script>
The dynamic date code:
<script>new Date().getFullYear()><?php if( !empty($footer_three)) : ?><?php echo $footer_three; ?><?php endif; ?>&&document.write(" - "+new Date().getFullYear());</script>
I need a way to put the code above inside the area marked DYNAMIC DATE in the first code segment (just past half-way through scrolling).
Is this possible, or am I doing this all wrong?
Here is the whole php file if you need it:
<?php
// ====================== Footer Editor ======================
function ds_footer_links_editor($wp_customize) {
$wp_customize->add_panel( 'footer_links_option', array(
'priority' => 30,
'capability' => 'edit_theme_options',
'title' => __('Edit Footer Links', footer_links_title),
'description' => __('Customize the login of your website.', footer_links_title),
));
$wp_customize->add_section('ds_footer_links_section', array(
'priority' => 5,
'title' => __('Footer Links Editor', footer_links_title),
'panel' => 'footer_links_option',
));
// Before Link One
$wp_customize->add_setting('ds_footer_links_before_link_one', array(
'default' => 'Designed By',
'type' => 'option',
'capability' => 'edit_theme_options',
));
$wp_customize->add_control('ds_footer_links_before_link_one', array(
'label' => __('Text Before First Link', footer_links_title),
'section' => 'ds_footer_links_section',
'type' => 'option',
'priority' => 5,
'settings' => 'ds_footer_links_before_link_one'
));
// Link One
$wp_customize->add_setting('ds_footer_links_link_one', array(
'default' => 'Kyle Briggs',
'type' => 'option',
'capability' => 'edit_theme_options',
));
$wp_customize->add_control('ds_footer_links_link_one', array(
'label' => __('First Link Text', footer_links_title),
'section' => 'ds_footer_links_section',
'type' => 'option',
'priority' => 10,
'settings' => 'ds_footer_links_link_one'
));
// Link One URL
$wp_customize->add_setting('ds_footer_link_one_url', array(
'default' => 'http://kylebriggs.co.uk/',
'type' => 'option',
'capability' => 'edit_theme_options',
));
$wp_customize->add_control('ds_footer_link_one_url', array(
'label' => __('First Link URL', footer_links_title),
'section' => 'ds_footer_links_section',
'type' => 'option',
'priority' => 15,
'settings' => 'ds_footer_link_one_url'
));
// Company Name
$wp_customize->add_setting('ds_footer_links_company_name', array(
'default' => 'Kyle Briggs',
'type' => 'option',
'capability' => 'edit_theme_options',
));
$wp_customize->add_control('ds_footer_links_company_name', array(
'label' => __('Text Before First Link', footer_links_title),
'section' => 'ds_footer_links_section',
'type' => 'option',
'priority' => 5,
'settings' => 'ds_footer_links_company_name'
));
//Company URL
$wp_customize->add_setting('ds_footer_link_company_url', array(
'default' => 'http://kylebriggs.co.uk/',
'type' => 'option',
'capability' => 'edit_theme_options',
));
$wp_customize->add_control('ds_footer_link_company_url', array(
'label' => __('First Link URL', footer_links_title),
'section' => 'ds_footer_links_section',
'type' => 'option',
'priority' => 15,
'settings' => 'ds_footer_link_company_url'
));
//Start copyright year
$wp_customize->add_setting('ds_footer_link_start_year', array(
'default' => '2015',
'type' => 'option',
'capability' => 'edit_theme_options',
));
$wp_customize->add_control('ds_footer_link_start_year', array(
'label' => __('First Link URL', footer_links_title),
'section' => 'ds_footer_links_section',
'type' => 'option',
'priority' => 15,
'settings' => 'ds_footer_link_start_year'
));
}
add_action('customize_register', 'ds_footer_links_editor');
function ds_new_bottom_footer() {
$footer_one = get_option('ds_footer_links_before_link_one','Designed By');
$footer_two = get_option('ds_footer_links_link_one','Kyle Briggs');
$footer_link_one = get_option('ds_footer_link_one_url','http://kylebriggs.co.uk/');
$footer_three = get_option('ds_footer_links_company_name','Kyle Briggs');
$footer_link_two = get_option('ds_footer_link_company_url','http://kylebriggs.co.uk/');
$footer_four = get_option('ds_footer_link_start_year','2015');
?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#footer-info").text(' ');
jQuery('<p id="footer-info"><?php if( !empty($footer_one)) : ?><?php echo $footer_one; ?><?php endif; ?> <?php if( !empty($footer_two)) : ?><?php echo $footer_two; ?><?php endif; ?> | © Copyright <?php if( !empty($footer_three)) : ?><?php echo $footer_three; ?><?php endif; ?><script>new Date().getFullYear()><?php if( !empty($footer_four)) : ?><?php echo $footer_four; ?><?php endif; ?>&&document.write(" - "+new Date().getFullYear());</script> <?php if( !empty($footer_three)) : ?><?php echo $footer_three; ?><?php endif; ?>. All Rights Reserved.</p>').insertAfter("#footer-info");
});
</script>
<?php
}
add_action( 'wp_head', 'ds_new_bottom_footer' );
?>
You have to use eval() to execute any script code that you've inserted as DOM text.
MooTools will do this for you automatically, and I'm sure jQuery would as well (depending on the version. jQuery version 1.6+ uses eval). This saves a lot of hassle of parsing out tags and escaping your content, as well as a bunch of other "gotchas".
Generally if you're going to eval() it yourself, you want to create/send the script code without any HTML markup such as , as these will not eval() properly.
Let we assume, we have following values for those variables.
<?php
$footer_one= "Footer one";
$footer_link_one= "#";
$footer_two = " link";
$footer_three = "Footer three";
?>
<script type="text/javascript">
$(document).ready(function(){
$("#footer-info").html('<?php if( !empty($footer_one)) : ?><?php echo $footer_one; ?><?php endif; ?>'+'<?php if( !empty($footer_two)) : ?><?php echo $footer_two; ?><?php endif; ?> | © Copyright <?php if( !empty($footer_three)) : ?><?php echo $footer_three; ?><?php endif; ?>**DYNAMIC DATE** <?php if( !empty($footer_three)) : ?><?php echo $footer_three; ?><?php endif; ?>. All Rights Reserved. ');
});
</script>
I don't know what below part is doing. Can you explain.
<script>new Date().getFullYear()><?php if( !empty($footer_three)) : ?><?php echo $footer_three; ?><?php endif; ?>&&document.write(" - "+new Date().getFullYear());</script>

Daterangepicker date defaults to zero in CakePHP

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));
}

Categories

Resources