<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
Related
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>
My page has two columns. Column1 for player1, column2 for player2.
For each column I have a dropdown to choose one of the players.
With some ajax en php codes I return the folowing info of each player. For example:
Col1: Tom, Football: 8 points Basketball: 5 points
Col2: Jonathan, Football: 4 points Basketball: 9 points
Now I want to let the users sort the results: So I add another dropdown in each column:
PHP
echo "<select id='filter_class' name=\"filter\"
data-userid=\"".$user_id."\" onchange=\"getFilter(this.value)\">
<option value=\"all\">All</option>
<option value=\"asc\">Asc</option>
<option value=\"desc\">Desc</option>
</select>";
AJAX
function getFilter(filter)
{
var user_id = document.getElementById('filter_class').getAttribute('data-userid');
console.log(user_id);
$.ajax({
type: "GET",
url: 'http://website.com',
data: {action: filter, user_id: user_id},
success: function (result) {
$("#Target").html(result);
}
});
}
As you can see, the dropdown use the user_id and the option value to sort the results.
this code works only for 1 column, because he keeps (Ofcourse) using the first user_id. For example when I try to sort the second results, he sort the results of the first user_id (Normal).
I want that the SORT DROPDOWN use the user_id of the player is selected in the same column.
IMPORTANT
I want to use the sort option after showing both results on the page.
You need to create your SORT dropdowns with different ids. Let's say you have the id's id='filter_class1' and id='filter_class2'.
Now you need to pass the id of the calling dropdown to the getFilter() function like this.
onchange=\"getFilter(this.value, this.id)\"
First few lines of your getFilter() function should look like this:
function getFilter(filter, eleId)
{
var user_id = document.getElementById(eleId).getAttribute('data-userid');
console.log(user_id);
...
There can only be one element with one id.
You might wanna use the class attribute alongside the data attribute.
Using jquery's class selector.
Take a look at this example.
<select class='filter_class' name="filter"
data-userid="29" >
<option value="all">All</option>
<option value="asc">Asc</option>
<option value="desc">Desc</option>
</select>
<select class='filter_class' name="filter"
data-userid="30" >
<option value="all">All</option>
<option value="asc">Asc</option>
<option value="desc">Desc</option>
</select>
<script>
$(".filter_class").change(function(){
var user_id = $(this).data('userid');
alert(user_id);
})
</script>
This is my category list
$scope.categories= [{"category_id":"1","category_name":"sports"},{"category_id":"2","category_name":"casual"},{"category_id"
:"3","category_name":"formal"},{"category_id":"4","category_name":"party wear"},{"category_id":"5","category_name"
:"winter"},{"category_id":"9","category_name":"summer"}]
The product can have multiple categories.
$scope.product_categories=[{"category_id":"3"},{"category_id":"4"},{"category_id":"5"}]
I have these two array first array categories holds all the category.
and second array holds the category which that product has.
I have select tag where all the categories are listed at the time of adding the product.user can select multiple product.
{{category.category_name}}
Now suppose I have added one product with 3 category 3,4,5 respectively.
When I trying to edit that product these 3,4,5 category must be selected because this product is related with these category. So this is my code which is not working.
<select multiple="" class="form-control" name="category_list[]" ng-model="categories" >
<option ng-selected="{{category.category_id == product_categories}}"
ng-repeat="category in categories"
value="{{category.category_id}}">
{{category.category_name}}
</option>
I am confused here how to do the multiple selection when I have array with array.Categories 3,4,5 must be selected among the category.if id do
ng-selected="{{category.category_id ==5}}" like this only 5 category is get selected.how to do the multiple selection with array or multiple values?
I have a solution, use $scope.$watch
$scope.$watch('product_categories', function (nowSelected) {
$scope.selectedValues = [];
if (!nowSelected) {
// here we've initialized selected already
// but sometimes that's not the case
// then we get null or undefined
return;
}
angular.forEach(nowSelected, function (val) {
$scope.selectedValues.push(val.category_id.toString());
});
});
And apply selectedValues via following:
<select multiple ng-model="selectedValues" style="width: 50%;" size="7">
<option ng-repeat="category in categories" value="{{category.category_id}}" ng-selected="{{selectedValues.indexOf(category.category_id.toString())!=-1}}">{{category.category_name}}</option>
</select>
==> Full code at Plunker multi selection ng-selected
Hope it helps!
you forgot to write the ng-app="" in your code ...
something like this !
<div ng-app="">
<select multiple="" class="form-control" name="category_list[]" ng-model="categories" >
<option ng-selected="{{category.category_id == product_categories}}"
ng-repeat="category in categories"
value="{{category.category_id}}">
{{category.category_name}}
</option>
</div>
I have added
$watch for the multiple selection.
angular.module("myApp.controllers",[])
.controller("product",function($scope){
$scope.categories= [{"category_id":"1","category_name":"sports"},{"category_id":"2","category_name":"casual"},{"category_id"
:"3","category_name":"formal"},{"category_id":"4","category_name":"party wear"},{"category_id":"5","category_name"
:"winter"},{"category_id":"9","category_name":"summer"}]
$scope.product_categories=[{"category_id":"3"},{"category_id":"4"},{"category_id":"5"}]
$scope.$watch('product_categories', function(nowSelected){
$scope.selectedValues = [];
if( ! nowSelected ){
return;
}
angular.forEach(nowSelected, function(val){
$scope.selectedValues.push( val.category_id.toString() );
});
});
});
});
and I made some changes in my view part
<select class="form-control" name="category_list[]" multiple ng-model="selectedValues" >
<option ng-repeat="category in categories"
value="{{category.category_id}}">
{{category.category_name}}
</option>
</select>
I put all the categories in selectedValues and assigned to select tag as ng model with multiple selection. After this change It works for me.
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.
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>