drupal 7 date_popup module (update minDate onSelect event) - date

I'm using drupal 7 module date (date_popup). I have 2 datepickers. I need to update minDate on the second datepicker on "onSelect" event by click on the first one. It works but only for the second click. If I click on the first datepicker and select date nothing happens and minDate is not updated. When I select the second time it works well. Why doesn't it work from my first click?
Here is my datepicker init php code for my module.
<?php
$form['leave'] = array(
'#type' => 'date_popup', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
'#title' => t('Leave'),
'#default_value' => date('Y-m-d'),
'#date_format' => $format,
'#required' => TRUE, // Added
'#date_label_position' => 'within', // See other available attributes and what they do in date_api_elements.inc
'#date_increment' => 15, // Optional, used by the date_select and date_popup elements to increment minutes and seconds.
'#date_year_range' => '-3:+3', // Optional, used to set the year range (back 3 years and forward 3 years is the default).
'#datepicker_options' => array(
'changeMonth' => false,
'changeYear' => false,
'altField' => '#edit-leave-alt',
'showOtherMonths' =>true,
'selectOtherMonths' =>false,
'altFormat' => 'D, M d, yy',
'minDate' => 0,
), // Optional, as of 7.x-2.6+, used to pass in additional parameters from the jQuery Datepicker widget.
'#attributes' => array('readonly' => 'readonly')
);
$form['leave'] = array(
'#type' => 'date_popup', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
'#title' => t('Leave'),
'#default_value' => date('Y-m-d'),
'#date_format' => $format,
'#required' => TRUE, // Added
'#date_label_position' => 'within', // See other available attributes and what they do in date_api_elements.inc
'#date_increment' => 15, // Optional, used by the date_select and date_popup elements to increment minutes and seconds.
'#date_year_range' => '-3:+3', // Optional, used to set the year range (back 3 years and forward 3 years is the default).
'#datepicker_options' => array(
'changeMonth' => false,
'changeYear' => false,
'altField' => '#edit-leave-alt',
'showOtherMonths' =>true,
'selectOtherMonths' =>false,
'altFormat' => 'D, M d, yy',
'minDate' => 0,
), // Optional, as of 7.x-2.6+, used to pass in additional parameters from the jQuery Datepicker widget.
'#attributes' => array('readonly' => 'readonly')
);
?>
Here is my js code
(function($) {
Drupal.behaviors.ifly_search = {
attach: function (context, settings) {
$("#edit-leave-datepicker-popup-0").datepicker({
onSelect: function(date,param) {
var data = new Date(param.currentYear, param.currentMonth, param.currentDay);
$("#edit-return-datepicker-popup-0").datepicker("option", "minDate", data).val(date);
},
});
}
};
})(jQuery);

Just to flesh out my comment. Try this instead:
(function($) {
Drupal.behaviors.ifly_search = {
attach: function (context, settings) {
$("#edit-return-datepicker-popup-0").datepicker({minDate: new Date()});
$("#edit-leave-datepicker-popup-0").datepicker({
onSelect: function(date,param) {
var data = new Date(param.selectedYear, param.selectedMonth, param.selectedDay);
$("#edit-return-datepicker-popup-0").datepicker("option", "minDate", data).val(date);
},
});
}
};
})(jQuery);

Skarist, thank you a lot!
Here is the working code:
Drupal.behaviors.ifly_search = {
attach: function (context, settings) {
$("#edit-leave-datepicker-popup-0").datepicker({
onSelect: function(date,param) {
var data = new Date(param.selectedYear, param.selectedMonth, param.selectedDay);
$("#edit-return-datepicker-popup-0").datepicker({minDate: data}).val(date);
$("#edit-return-datepicker-popup-0").datepicker("option", "minDate", data).val(date);
},
});
}
};

Related

Restrict date past 60 days

I was working in Drupal 8 custom forms. I like to restrict or block user purchase date not more than 60 days
I tried with three methods. it wont work. Please help for me
I tried with jquery but style are afftected and format are also changed.
$form['product_info']['install_date'] = [
'#type' => 'date',
'#title' => $this->t('Date of Install *'),
'#default_value' => $this->store->get('install_date') ? $this->store->get(
'install_date') : "",
'#date_format' => "m/d/Y",
'#size' => 10,
//First method
'#minDate'=> '-60',
//second Method
'#datepicker_options' => array('minDate' => 0,),
//Third Method
' #attributes' => ('min' => \Drupal::service('date.formatter')->format(REQUEST_TIME, '-60', 'Y-m-d'),
),
// '#required' => TRUE,.
'#prefix' => '<div class="field-wrapper">',
'#suffix' => '</div>',
'#error_no_message' => TRUE,
];

How to highlight a date(Some other date not the today's date) in Kartik Datepicker

I'm able to highlight current date with 'todayHighlight' => true, but I want to highlight some other date.
You can use this function beforeShowDay as pluginOptions array key.
Here is simple example:
<?= $form->field($model, 'myDate', ['showLabels' => false])->widget(DatePicker::className(), [
'size' => 'sm',
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'autoclose' => true,
'format' => 'Y-m-d',
'startDate' => date('Y-m-d'),
'endDate' => date('Y-m-d', strtotime('-1 day')),
'weekStart' => 1,
'beforeShowDay' => new \yii\web\JsExpression("function (dates) {
console.log(dates); // List with available dates as date object defined with startDate/endDate (for mor customisations if needed)
return {classes: 'highlight', tooltip: 'Pick this day'};
}
}"),
]
]); ?>
Don't forget to define class (.highlight in this case) in your css.
.highlight{background: #ebf4f8}

fire an onchange event in select form control in drupal 7 forms api

I have a select form control in Drupal 7. How to call a function to whenever onchange event fires.
This is my select form control code:
$form['id']['signature'] = array(
'#type' => 'select',
'#options' => array(
0 => t('Browse...'),
1 => t('Sign...'),
2 => t('Clear...'),
),
);
You could probably do it with '#attibutes'.
eg.
$form['id']['signature']['#attributes'] = array('onchange' => array('myFunction()'))
Though I never like adding event attributes to html tags, so you could also probably do it by adding inline (or extenal) javascript to the form with a behavior like this:
$form['id']['signature'] = array(
'#type' => 'select',
'#options' => array(
0 => t('Browse...'),
1 => t('Sign...'),
2 => t('Clear...'),
),
'#attributes' => array(
'id' => array('signature-goes-here'),
),
);
$form['id']['signature']['#attached']['js'][] = array(
'data' => "
Drupal.behaviors.signature = function (context) {
$('#signature-goes-here', context).change(function () {
// Do stuff here.
});
}
",
'type' => 'inline',
);
But if you wanted to fire ajax when this form item changes, you would probably use the Drupal form api functionality.

How to edit cell Zfdatagrid

how I can make editing row using ajax with zfdatagrid, thanks
ZF 1.11 -
My Bootstrap
protected function _initZfdatagrid()
{
$this->_config = new Zend_Config_Ini(APPLICATION_PATH .'/configs/grid.ini', 'production');
Zend_Registry::set('config', $this->_config);
if ( #isset(Zend_Registry::get('config')->site->jqGridUrl) ) {
Bvb_Grid_Deploy_JqGrid::$defaultJqGridLibPath = Zend_Registry::get('config')->site->jqGridUrl;
}
}
My Controller
public function indexAction()
{
$grid1 = new Bvb_Grid_Deploy_JqGrid(Zend_Registry::get('config'));
$this->configG1($grid1);
$grid1->setDeployOptions(array
('title'=>'Grado 10A',
'subtitle'=>'School2.0 : Matematicas:'.date('Y-m-d'),
'logo'=>$this->view->baseUrl.'/zfdatagrid/public/images/logotipo.jpg',
));
$this->view->grid = $grid1->deploy();
$this->render('index');
}
public function configG1 ($grid)
{
$select = $this->_db->select()->from('Articulos', array('id', 'titulo', 'fecha','nota', 'publicar', 'dependencia'));
$grid->setSource(new Bvb_Grid_Source_Zend_Select($select));
$grid->updateColumn('id',
array('title' => '#ID', 'hide' => true));
$grid->updateColumn('_action',
array('search' => false, // this will disable search on this field
'order' => 1, 'title' => 'Action', 'width' => 100, 'class' => 'bvb_action bvb_first', 'callback' =>
array('function' => array($this, 'g1ActionBar'), 'params' =>
array('{{ID}}')), 'jqg' =>
array('fixed' => true, 'search' => false)));
$grid->updateColumn('titulo', array('title' => 'Titulo Articulo', 'width' => 260));
$grid->updateColumn('fecha', array('title' => 'Fecha', 'searchType' => "="));
$grid->updateColumn('Nota', array('title' => 'Calificacion (ucase)', 'callback' => array('function' => create_function('$text', 'return strtoupper($text);'), 'params' => array('{{District}}'))));
$grid->setDefaultFilters(array('titulo' => '1'));
$grid->setExport(array(// define parameters for csv export, see Bvb_Grid::getExports
'csv' => array('caption' => 'Csv'),
'pdf' => array('caption' => 'Pdf')));
$grid->setJqgParams(array('caption' => 'jqGrid Example', 'forceFit' => true, 'viewrecords' => false, // show/hide record count right bottom in navigation bar
'rowList' => array(10, 20, 30, 40, 50), // show row number per page control in navigation bar
'altRows' => true)// rows will alternate color
);
$grid->setJqgParam('viewrecords', true);
$grid->jqgViewrecords = true;
$grid->setBvbParams(array('id' => 'ID'));
$grid->setBvbParam('id', 'ID');
$grid->bvbId = 'ID';
$grid->bvbOnInit = 'console.log("this message will not be logged because of call to bvbClearOnInit().");';
$grid->bvbClearOnInit();
$grid->bvbSetOnInit('console.log("jqGrid initiated ! If data are remote they are not loaded at this point.");');
$grid->setAjax(get_class($grid));
}
If you haven't gotten anywhere on this, you might want to look at the jqgrid wiki. They provide information on adding inline cell editing:
http://www.trirand.com/jqgridwiki/doku.php?id=wiki:cell_editing
as well as popup form editing:
http://www.trirand.com/jqgridwiki/doku.php?id=wiki:form_editing
According to the inline cell editing page, adding the cellEdit parameter should enable cell editing. You can provide the url the data is submit to with the cellurl parameter.

Drupal Form:want to show previous form value as default_value on page

My Goal is if user is submitting this form with "Product Name" value as "YYY". On submit page should reload but this time "Product Name" should show previous valye as default as in this case "YYY".
Here is my code...
function addnewproduct_page () {
return drupal_get_form('addnewproduct_form',&$form_state);
}
function addnewproduct_form(&$form_state) {
$form = array();
$formproductname['productname'] = array (
'#type' => 'textfield',
'#title' => t('Product Name'),
'#required' => TRUE,
'#size' => '20',
);
if (isset($form_state['values']['productname']))
{
$form['productname']['#default_value'] = $form_state['values']['productname'];
}
//a "submit" button
$form['submit'] = array (
'#type' => 'submit',
'#value' => t('Add new Product'),
);
return $form;
}
You can use $form_state['storage'] in your submit handler to hoard values between steps. So add a submit function like so:
function addnewproduct_form_submit ($form, &$form_state) {
// Store values
$form_state['storage']['addnewproduct_productname'] = $form_state['values']['productname'];
// Rebuild the form
$form_state['rebuild'] = TRUE;
}
Then your form builder function would become:
function addnewproduct_form(&$form_state) {
$form = array();
$form['productname'] = array (
'#type' => 'textfield',
'#title' => t('Product Name'),
'#required' => TRUE,
'#size' => '20',
);
if (isset($form_state['storage']['addnewproduct_productname'])) {
$form['productname']['#default_value'] = $form_state['storage']['addnewproduct_productname'];
}
return $form;
}
Just remember that your form will keep being generated as long as your $form_state['storage'] is stuffed. So you will need to adjust your submit handler and unset($form_state['storage']) when are ready to save values to the database etc.
If your form is more like a filter ie. used for displaying rather than storing info, then you can get away with just
function addnewproduct_form_submit ($form, &$form_state) {
// Rebuild the form
$form_state['rebuild'] = TRUE;
}
When the form rebuilds it will have access to $form_state['values'] from the previous iteration.
Drupal will do this by default if you include:
$formproductname['#redirect'] = FALSE;
In your $formproductname array.
I prefer to save all values in one time when we are speaking about complex forms :
foreach ($form_state['values'] as $form_state_key => $form_state_value) {
$form_state['storage'][$form_state_key] = $form_state['values'][$form_state_value];
}
simply adding rebuilt = true will do the job:
$form_state['rebuild'] = TRUE;
version: Drupal 7
For anyone looking for an answer here while using webform (which I just struggled through), here's what I ended up doing:
//in hook_form_alter
$form['#submit'][] = custom_booking_form_submit;
function custom_booking_form_submit($form, &$form_state) {
// drupal_set_message("in form submit");
// dpm($form_state, 'form_state');
foreach ($form_state['values']['submitted_tree'] as $form_state_key => $form_state_value) {
$form_state['storage'][$form_state_key] = $form_state_value;
}
}
Note: added the ' as it was lost
In my case I had a couple of drop-downs. Submitting the form posted back to the same page, where I could filter a view and I wanted to show the previously selected options. On submitting the form I built a query string in the submit hook:
function myform_submit($form, &$form_state) {
$CourseCat = $form_state['values']['coursecat'];
drupal_goto("courses" , array('query' =>
array('cat'=>$CourseCat))
);
}
In the form build hook, all I did was get the current query string and used those as default values, like so:
function myform_form($form, &$form_state) {
$Params = drupal_get_query_parameters ();
$CatTree = taxonomy_get_tree(taxonomy_vocabulary_machine_name_load ('category')->vid);
$Options = array ();
$Options ['all'] = 'Select Category';
foreach ($CatTree as $term) {
$Options [$term->tid] = $term->name;
}
$form['cat'] = array(
'#type' => 'select',
'#default_value' => $Params['cat'],
'#options' => $Options
);
$form['submit'] = array(
'#type' => 'submit',
'#default_value' => 'all',
'#value' => t('Filter'),
);
return $form;
}
I usually solve this by putting the submitted value in the $_SESSION variable in the submit hook. Then the next time the form is loaded, I check the $_SESSION variable for the appropriate key, and put the value into the #default_value slot if there's anything present.
Not sure if this would work for you, but you could try adding the #default_value key to the form array
$form['productname'] = array (
'#type' => 'textfield',
'#title' => t('Product Name'),
'#required' => TRUE,
'#size' => '20',
'#default_value' => variable_get('productname', ''),
);
That way if the variable is set it will grab whatever it is, but if not you can use a default value.