CakePHP Drop Down Form Value Not Being Sent to Controller - javascript

I have a form with four fields - three of which are properly being sent up to the controller. One field is a drop-down selector field and it is not being sent. Here is sample form code from my schedule_date field which is being sent properly:
<?php
echo $this->Form->input("schedule_date"
,array('label' => false
,'type' => 'text'
,'class' => 'step_dates'
,'readOnly' => 'readOnly'
)
);
?>
And here is the code for the drop-down box which is NOT being sent properly:
<?php
echo $this->Form->input("project_step_type_id"
,array('label' => false
,'id' => 'project_step_type_id'
,'class' => 'project_step_type_id'
,'empty' => '( select step )'
,'options' => $project_step_types
)
);?>
Here is what my debug looks like on the form data I'm sending back (notice project_step_type_id is missing):
<pre class="cake-debug">
array(
'user_id' => '402',
'trainer_user_id' => '524',
'schedule_date' => '2014-11-26'
)
</pre>
Can anyone help? All fields are inside the form tag. I believe the issue is with this line:
request_data=$('#my_form input').serializeCakeArray();
It's not getting the "select" fields, only the "input" fields. Does anyone know how to fix that? Thank you.

The answer is:
request_data=$('#my_form input, #my_form select').serializeCakeArray();

Related

Symfony Forms EntityType Select All using JS

On my webpage, I am using a Symfony Form with an EntityType class. I want to create an extra button that selects all items from this dropdown on click. Is this possible using JavaScript / JQuery? Simply changing the automatic generated HTML does not work.
FormType.php:
->add('item', EntityType::class, [
'class' => Item::class,
'choice_label' => function(Item $item) {
return sprintf('%s', $item->getName());
},
'label' => 'Staff',
'multiple' => true,
I think something like that would do the trick :
<!-- Somewhere in your Twig file : -->
<button id="check-all-options">Check all !</button>
document.querySelector('#check-all-options').addEventListener('click', () => {
// Select all the options of your <select> tag
const options = document.querySelectorAll('#form_item option');
// Loop on all the options and pass them to selected ✅
options.forEach(option => option.selected = true);
});
Let me know if it helps :)

How to use multiple filtering options that will affect the price in Woocommerce simple products?

I am using "Add a select field that will change price in Woocommerce simple products" answer code. It only uses a single selection field for filtering. I want to extend this code using jQuery to be able to use multiple fields for filtering.
Example: 2 select fields, one text, a checkbox and some radio buttons that will all determine the final calculated price.
// Select field
woocommerce_form_field('materials_pack', array(
'type' => 'select',
'class' => array('material-field form-row-wide'),
'label' => __('Select Materials:', $domain),
'required' => true,
'options' => $options,
),'');
<script>
jQuery(function($){
var a = <?php echo json_encode($prices); ?>,
b = 'p.price',
c = 'select[name="materials_pack"]';
$(c).on( 'change', function(){
$.each( a, function( key, value ){
if( $(c).val() == key )
$(b).html(value);
});
});
});
</script>
How to use it for multi fields? and how do the jquery script?
Ok i found my answer here now i just need to how to do it in this code?
add_action('woocommerce_before_calculate_totals', 'set_cutom_cart_item_price', 20, 1);
foreach ( $cart->get_cart() as $cart_item ) {
if ( isset( $cart_item['material_data']['new_price'] ) ){
$cart_item['data']->set_price( $cart_item['material_data']['new_price'] );
}
}

laravel getting error on empty request

I get error below while trying to add item to my cart:
Darryldecode \ Cart \ Exceptions \ InvalidItemException
validation.numeric
The error comes from this part of my code:
$customAttributes = [];
if(!empty($request->attr)){
foreach($request->attr as $sub) {
// find the suboption
$sub = Suboption::find($sub);
if (!empty($sub->id)) {
$itemCondition1 = new \Darryldecode\Cart\CartCondition(array(
'name' => $sub->title,
'value' => $sub->price,
'type' => 'additional',
'target' => 'item',
));
array_push($customAttributes, $itemCondition1);
}
}
}
and it take place in here:
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $price,
'quantity' => $request->input('quantity'),
'attributes' => $weightArray,
'conditions' => $customAttributes, //here
));
The $customAttributes code supposed to get data IF product does have those information And user chose any of it and suppose to ignore if product doesn't have any of those info or user didn't chose any of it.
Issue is
The code expect data no matter what, product does have that info or not, user selected any or not, even if user select that data still i get error above.
Demo
https://imgur.com/a/KHJqp
any idea why is that?
UPDATE
I figured my issue comes from 'price' => $price, in my add method where i get my $price like:
$price = $product->discounts;
if($price->count() > 0 ) {
foreach($discounts as $disc){
if($disc->value_to >= $mytime) {
$price = $product->price - $disc->amount;
}
}
}else{
$price = $product->price;
}
this part supposed to get product price if there is no discount, and get discounted price if there is.
How I get to this line of code? here is it
SOLVED
I used hidden field and got my data from front-end instead of controller method.

How to get .on("change") to work with gldatepicker?

I have a date select box that when a date is selected, the date will be used in a calculation.
<? echo $this->Form->input( 'start_date', array( 'label' => __('Show only accounts made since:'), 'value' => '', 'readonly' => 'readonly' ) ); ?>
The Javascript I tried:
$('#start_date').glDatePicker({ cssName: 'darkneon'});
$("#start_date").on("change",function() {
var date = this.value;
console.log (date);
do_stuff(date);
});
The console shows nothing when I select a date. I am clearly missing something. Is on-change even the right way to handle this?
Try this:
$('#start_date').glDatePicker({
cssName: 'darkneon',
onClick: function(target, cell, date, data) {
console.log (date);
do_stuff(date);
}
);
From the docs (find with Ctrl+f):
Callback that will trigger when the user clicks a selectable date.
Example (check example #3)

Yii- How can I make the second dropdownlist values displayed

I'm new to Yii framework and Javascript. Now, I'm using two dropdownlists for min_cost and max_cost. I'm using the below code to list the values in the dropdownlist.
<?php
$this->widget('ext.combobox.EJuiComboBox', array(
'model' => $model,
'attribute' => 'min_cost',
'data' => Yii::app()->params['cost_min_resales'],
'options' => array(
'onSelect' => 'cost_change(item.value);',
'allowText' => false,
),
'htmlOptions' => array('placeholder' => 'Min Cost', 'style'=>'width:70px'),
));
?>
</br>
<?php
$this->widget('ext.combobox.EJuiComboBox', array(
'model' => $model,
'attribute' => 'max_cost',
'data' =>Yii::app()->params['cost_max_resales'],
'options' => array(
'allowText' => false,
),
'htmlOptions' => array('placeholder' => 'Max Cost', 'style'=>'width:70px'),
));
?>
For this I'm using the below script:
<script>
function cost_change(price) {
var removed;
console.log("removed", removed);
select = "#SearchForm_max_cost_select";
var value = price;
console.log("value", value);
jQuery('#SearchForm_max_cost_select').prepend(removed);
var toKeep = jQuery('#SearchForm_max_cost_select option').filter(function() {
return parseInt(this.value) > parseInt(value);
});
console.log("to keep",toKeep);
removed = jQuery('#SearchForm_max_cost_select option').filter(function() {
return parseInt(this.value) < parseInt(value);
});
}
</script>
Now, I want to populate the second dropdownlist with values greater than the value selected in first one. Suppose there are values hardcoded in an array as (1,2,3,4,5) I select 2 from first dropdownlist then all values greater than 2 (i.e 3,4,5) should be listed in second dropdownlist.
The above script works fine. I can see the values of second dropdownlist printed in the logs . But, I'm not able to pass this values to second dropdownlist. How can I do this.
Edit :
Below is the image of the object I'm getting when I select value .
this will work, actually i was also facing the same problem but somehow figured it out.
In your controller just check for this condition..
if ($model->"yourdbid" == null) {
$model->"yourdbid" = $_POST['yourdbid'];
}

Categories

Resources