CakePhp: Dropdowns repeating options - php

I'm using CakePHP 2.1.1, and upgraded to 2.2.0 and the issue exists in both. I am using the FormHelper to generate a Select dropdown with the options defined in an array. When it generates the options, it repeats some of them. Which ones and how many repeat changes depending on which record I am editing.
Using the following code in my view:
debug($advisors);
echo $this->Form->input('advisor',array('options'=>$advisors));
I see:
/app/View/Students/edit.ctp (line 38)
array(
'K-1' => 'K-1',
'K-2' => 'K-2',
'2-3' => '2-3',
'3-5n' => '3-5n',
'3-5s' => '3-5s',
'4-5' => '4-5',
'6-8' => '6-8'
)
and then a dropdown with the following options:
<option selected="selected" value="K-1">K-1</option>
<option value="K-2">K-2</option>
<option value="2-3">2-3</option>
<option value="3-5n">3-5n</option>
<option value="3-5s">3-5s</option>
<option value="4-5">4-5</option>
<option value="6-8">6-8</option>
<option value="K-1">K-1</option>
<option value="K-2">K-2</option>
<option value="2-3">2-3</option>
What else should I be checking?

Ok, never mind. It was a javascript function I wrote a while back that was messing with the generated options.

Related

laravelcollective: add extra attribute to each options(Form::Select)

<select class="form-control" id="user" name="car">
<option value="0" selected="selected">Select Cars</option>
<option dta-img='1.jpg' value="134">Volvo</option>
<option dta-img='2.jpg' value="135">Noah</option>
<option dta-img='3.jpg' value="136">Marcidis</option>
<option dta-img='4.jpg' value="143">London Express</option>
</select>
My array for options :
public function guardianList()
{
$list = ["Select Cars"];
foreach ($carss->all() as $car) {
$list[$car->id] = $car->name;
}
return $list;
}
Form::select('car',$list, null, null);
How can i add extra dynamic html attribute to all options?
I want to show image infront of every option in jquery select2. For that i need to send image from Form::Select(). I there any easy way to do that without using marcos?
Solved the issue:
Pass array in the 4th param.
$opt_attributes = collect($guardians->all())
->mapWithKeys(function ($item) {
return [$item->id => ['data-img' => $item->photos->last()->name]];
})->all();
make a array linke this and pass it as 4th parameter.
I don't know exactly since which version, but laravelcollective/html 5.8 version takes optionParameters as fifth parameter, not 4th

Laravel input array why do I get null in $request->quantity

After searching similar questions I still didn't get the error
The thing is that I have a form that usually works, but the 10% of the users have this error
array_sum() expects parameter 1 to be array, null given
Form
<select name="quantity[]" class="form-control option-quantity" data-price="100">
<option data-price="100" value="0">0</option>
<option data-price="101" value="1" selected="selected">1</option>
<option data-price="102" value="2">2</option>
</select>
Controller
$validator = Validator::make($request->all(), [
'quantity.*' => 'required|integer',
]);
if ($validator->fails()) {
abort(500);
}
if(count($request->quantity) == 0 || array_sum($request->quantity) < 1)){
//message to select at least a quatity
}
They always put an amount because I did't restrictions, js to avoid nulls or 0 and from the server side not to allow 0
So, as it happens just sometimes, I'm wondering if some explorers do something weird changing the quantity[] to quantity or null or something, I'm driving a bit crazy here and I can't reproduce the error myself and it wouldn't be a good idea to wait the error to happen to a user (:
BTW of course I can add if($request->quantity) to avoid the error, but that not solves the question of why I get a null just sometimes, rest is fine.
As per suggestion after doing dd($request->quantity); I get:
array:1 [
0 => "1"
]
But again, for me its working good, not for the 10%.
You can simply avoid this error by checking quantity.It
if($request->quantity){
if(count($request->quantity) == 0 || array_sum($request->quantity) < 1)){
//message to select at least a quatity
}
}
It is throwing this error because $request->quantity is null;
Hope this helps
Since you have not tagged the version of Laravel, I'll make an assumption that you are using latest version.
why I get a null
Check your validation rule..
When a user submits the data with empty strings, Laravel makes it nullable. Checkout the middleware ConvertEmptyStringsToNull which is located inside Illuminate\Foundation\Http\Middleware folder.
So, if you want it to be always present at the time of submitting the user data, make it required like so.
return [
'quantity' => 'required',
// .. other validation rules..
];
Try removing "[]" from the name on the select, so you will have this:
<select name="quantity" class="form-control option-quantity" data-price="100">
<option data-price="100" value="0">0</option>
<option data-price="101" value="1" selected="selected">1</option>
<option data-price="102" value="2">2</option>
</select>

Clone form select box with option .CakePHP

Usually, I always used CakePHP form helper to list down select box with options.
I would like to know,if I can create it manually(I mean without CakePHP form helper) ?
Example:
Using cakephp form helper to create select box
<?php echo $this->Form->input('product_name',array('options'=>$myoptions));?>
But I want to do something like below
<select name="data[Product][product_name]" id="product">
<options value="1">One</options>
<options value="2">Two</options>
<options value="3">Three</options>
</select>
Any ideas?
Thanks
CakePHP 2.5.7
--EDIT--
I want to clone multiple select box that will follow CakePHP form structure.
Okay the problem start here.
By default I'd use CakePHP form helper to draw the select box and their options.
I wrote this kind of code:
<?php echo $this->Form->input('Redemption.0.type_id',array('class'=>'form-control','id'=>'selectProduct','label'=>false,'empty'=>'Choose Type','options'=>$types));?>
It will return in HTML structure like below:
<select name="data[Redemption][0][type_id]" class="form-control" id="selectProduct">
<option value="">Choose Type</option>
<option value="1">Toner</option>
<option value="2">Ink Catridge</option>
</select>
I want to clone the existing select box above to new select box but with different name value like this:
<select name="data[Redemption][1][type_id]" class="form-control" id="selectProduct">
<option value="">Choose Type</option>
<option value="1">Toner</option>
<option value="2">Ink Catridge</option>
</select>
Notice that integer value on name="data[Redemption][1][type_id]" has increased.That is what I wanted.
But now,I've no idea to do cloning with data changing as stated.
I have try several jquery method such as .clone() .append() but it's not work as the options data inside the select box become redundant.
Can you please enlighten me how to duplicate/clone select box in CakePHP?
Thanks.
CakePHP 2.5.7
<?php
echo $this->Form->input( 'product_name', array(
'type' => 'select',
'options' => array(
'key1' => 'val1',
'key2' => 'val2',
),
'empty' => true,
));
?>
You can add other conditional values to your select options just by adding values to the variable array like this:
$myOptions = array(
'1' => 'One',
'2' => 'Two',
'3' => 'Three'
);
// If user make some action
if($someAjaxAction == true) {
$myOptions[4] = 'Four';
}
echo $this->Form->input('product_name', array('options' => $myOptions));

Show title or span in select box (Cakephp 1.3)?

Problem
Show lengthy options on mouse hover in select box with a fixed width.
Names are hidden here:
Rendered HTML in browser
<select id="prim" size="5" multiple="multiple" scroabble="1" name="prim[]">
<option value="Applied Research Associates Inc.">Applied Research Associates Inc.</option>
</select>
Required Output
Show span or title as below
Expected HTML
<select id="prim" size="5" multiple="multiple" scroabble="1" name="prim[]">
<option value="Applied Research Associates Inc." title="Applied Research Associates Inc.">Applied Research Associates Inc.</option>
</select>
My CakePHP Code
echo $form->input('prim',
array('options'=>$refined_list,
'type'=>'select',
'scrollable'=>true,
'multiple'=>true,
'name'=>'prim',
'label'=>false,
'size'=>'5'));
What attribute should I add to make desired changes?
EDIT/UPDATE: One way might be to update my $refined_list array with title field as suggested by #ammu. I must wait for a better solution.
There is no such way to add attribute to select option in cakephp documentation.Have a look at this, it might help you for adding attributes to select option.
http://www.dereuromark.de/2012/03/01/some-new-crazy-cakephp-tricks/
Well use it like this and you'll get what you want:
$options = array(
...
array('name' => 'United states', 'value' => 'USA', 'title' => 'the title that you want'),
array('name' => 'USA', 'value' => 'USA', 'title' => 'the other title that you want'),
);
echo $this->Form->input('test', array('type'=>'select', 'options'=>$options));
you can also add any attribute that you want in the form of array for each option (class, placeholder, etc).

How to set any attribute of Select lke disabled='disabled' by using ViewHelper in Zend Framework

I am using Zend Frameworks ViewHelpers.
I am trying to pass something to set disabled attribute in SELECT. For example if
$countries = array(1=>'Select Option', 2=>'us', 3=>'uk')
and
formSelect('country','us',null,$this->countries)
I need to diable first option i.e. 'Select Option'
Do you have any idea ?
Thanks in addvance
I don't think you can disable one element? If you disable it then why have it at all?
You can only disable the whole <select> input.
Suggest you write validation to not accept the first element.
Edit after OP's comment about being able to do this
Here is another answer
// Get the countries element (do this after adding your options), then set the
// attribute disable for option '1'
$form->getElement("countries")->setAttrib("disable", array(1));
This is suggested here
There IS a way of doing it through Zend_Form (at least on my current ve 1.11):
$this->addElement
(
"select","selectName",
array("multiOptions"=>array("one","two","three"), "disable"=>array(0,1))
);
That one will disable first two options.
Credit goes to jakenoble.
Just reformatted the code to use the formSelect-viewhelper instead of a form-element.
<?php
$countries = array(1 => 'Select Option', 2 => 'us', 3 =>'uk');
echo $this->formSelect('country', 2, array('disable' => array(1)), $countries)
This will result in:
<select name="country" id="country">
<option value="1" label="Select Option" disabled="disabled">Select Option</option>
<option value="2" label="us" selected="selected">us</option>
<option value="3" label="uk">uk</option>
</select>

Categories