Yii2 Using Multiple Checkbox Fields - checkbox

I have a Yii2 app in which I want to use multiple checkbox as array. The checkbox is part of my Model, but it's not a DB column. It also has a custom value.
I loop through a list of days, set a custom value for the checkboxes and print the checkboxes to my view like this:
echo $form->field($model, 'dayIds[]')->checkbox(
['value' => $day->id.'_'.$person->id])->label(false);
On my Model side I have this:
public $dayIds;
foreach ($this->dayIds as $dayId) {
//do something
}
I have tried many different scenarios but always end up with error on my Model that $this->dayIds is null.
Any ideas please?

Related

why the combo box displays the objects?

I have a combobox which I would like to populate with some info. Below is the code:
cmbSelectProject.Items.Clear();
cmbSelectProject.ValueMember = "Sid";
cmbSelectProject.DisplayMember = "Name";
cmbSelectProject.DataSource = new BindingSource(ApiCaller.LstProjects, null);
So, there is a class Project with the existing properties "Sid" and "Name". ApiCaller.LstProjects call returns a list of Projects. So I expect the "Name" to de displayed and "Sid" is used as value.. Instead I see the bunch of Project objects displayed in the dropdown.
What am I doing wrong in here? Thanks for the suggestions.
Try This sequence.
Set DataSource property.
Set ValueMamber and DisplayMember.
Refresh Combo. using Refresh() method.

Initializing ngModel with data - AngularJS Bootstrap bs-select

I am maintaining a site that allows users to create a profile of sorts that will allow them to broadcast activities to a feed. I implement ng-grid to keep track of all the profiles that are created, and have created two buttons that allow users to create/edit these profiles. My only problem right now is, when users select a row on the grid and attempt to edit that specific row, the drop-down menu is not auto-populated with the data from ngModel.
This is the part of the form I am having trouble with:
<select ng-model="source.canSendTo" ng-options="value.name for value in sourceCanSendTo" data-style="btn" bs-select></select>
And within the controller, I have sourceCanSendTo defined as:
$scope.sourceCanSendTo = [ {"id":"abc", "name": "ABC"}, {"id":bcd", "name": "BCD"} ... ];
On row selection, I simply set source = the selected item, and console.logs show that all the data is there. The other parts of the form are being populated properly (mainly s), and console.log($scope.source.canSendTo) shows that the original data is there, it's just that select is defaulted to being blank...how would I go about trying to pre-select certain elements on the drop-down select I currently have?
For example, if the profile has 'abc', 'bcd' selected, how can I make it so that when I edit that profile, the drop down box shows 'abc,bcd' instead of just "Nothing Selected"?
Edit: I previously responded to a comment inquiring about bs-select, saying that it simply controlled some CSS elements of the drop down box - seems like this is completely incorrect after a quick google search when everything else led to dead ends. Does anyone have any idea how to properly initialize the model with data so that when I preload my form, the 'can send to' drop down select actually has the selected options selected, as opposed to saying "Nothing Selected"? Thanks in advance for all help!
As you are binding source.canSendTo to the name (value.name) of sourceCanSendTo then you just need to initially have an structure binding the names which had been saved, something like this:
source.canSendTo = ['abc', 'bcd']; //And all the selected values
So you need to construct your source.canSendTo property to this structure.
PS: If you show how you bring your data from the server, I can help you to construct the source.canSendTo property.
$scope.canSendTo must be initialized with a reference to the selected option.
var initialSelection = 0;
$scope.source = { canSendTo : [ {"id":"abc", "name": "ABC"}, {"id":bcd", "name": "BCD"} ... ] };
$scope.canSendTo = $scope.source.canSendTo[initialSelection];
Finally found out what was wrong with my code - seems like the data being stored in the model wasn't the same as what was in ngOptions, played around a bit with ngOptions and managed to get something that works. Working snippet of code:
<select ng-model="sendTo.name" ng-option="value.name as value.name for value in sourceCanSendTo" data-style="btn" multiple bs-select>
(Realized that the variable being used for ngModel was a fairly ambiguous in terms of naming convention, changed it)

Best way to handle checkbox in Laravel4?

I'm starting to work with Laravel4, I was struggling with some basic stuff like this one below. There's any other way to handle the value for an unmarked checkbox than adding a hidden checkbox for the default value like below?
<td class="text-center">
{{Form::hidden('status', 0)}}
{{Form::checkbox('status')}}
</td>
This is working fine for me right now, but I would like to know if there's any better way like handling the value in the Controller#update.
EDIT: The values of the checkbox on the form are being handle by Input::all() at the Update action.
Thanks for the feedback.
This is default behavior of HTML actually, not related to how Laravel handle inputs. From HTML spec:
When a form is submitted, only "on" checkbox controls can become successful.
...
Every successful control has its control name paired with its current value as part of the submitted form data set.
So, a more generic approach would be not to change your HTML form, but change the behavior when you are retrieving the input, e.g.:
$status = Input::get('status', false);
In the FormBuilder class, the checkbox method has these parameters.
checkbox(string $name, mixed $value = 1, bool $checked = null, array $options = array())
So you want:
Form::checkbox('status', 0, false, ['class' => 'form-control'])
Happy coding!

Ext JS form.getValues() retrieves bad values for checkboxes

So I have a form with two checkboxes. When I call form.getForm().getValues() and I log the values, it retrieves checkbox values like this: if it is checked then the value looks like this - SomeCheckbox: "on", and if it's not checked than the value is not there at all. And then I encode those values into JSON and send it to ASP.NET MVC and the values ASP retrieves for these checkboxes are always false. The solution I came up with is to set the SomeCheckbox properties of the values I retrieve from form after getting form values and it works just fine, but is not there any other way ?
SOLUTION:
call form's getFieldValues() method instead of getValues()
call form's getFieldValues() method instead of getValues()
You need to add an inputValue config to the checkbox.
{
boxLabel : 'Anchovies',
name : 'topping',
inputValue: 'anchovies'
}
http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.form.field.Checkbox-cfg-inputValue

Cakephp - keep selected value for dropdownlist after submit

How can I keep the selected value for a dropdownlist after form submission in Cakephp?
If more info (or some code) is needed just tell me please.
UPDATE
Here is part of the code in my view:
echo $this->Form->create('Chart');
echo $this->Form->input('username',
array('label'=>('Usernames List'),
'default'=>('Select username'),
'options'=>$usernames, 'selected'=>false));
echo $this->Form->end('Create Chart');
So, when I press 'Create Chart', the dropdownlist doesn't keep the username that I selected, but it goes back to the first one.
The Form helper uses the data stored in $this->data to prepopulate fields. Make sure that when you are submitting the form, the view that is rendered after has the appropriate model/key data stored in $this->data in order for the Form helper to correctly fill in the appropriate values.
Can we see your controller action possibly? That may help draw a more accurate conclusion.
you should never use the view to set defaults or values (especially selected/value is wrong as it - like your code - destroys the idea of persistent forms).
use the controller instead
#see http://www.dereuromark.de/2010/06/23/working-with-forms/ (Default Values)
add value in dropdown like this:
<?php echo $this->form->select('Schedule.showsid', array('0'=>'title', '1'=>'description'));?>

Resources