Yii activeRadioButton does not get activerecord's real value - yii

What might be the problem? I have a activerecord in $Row. The model has a property 'id'. Then I want to show radio buttons on interface like this:
echo CHtml::activeRadioButton($Row, 'id');
and when rendered on the browser, I can see that the radio button's value is '1'. But when I do like this:
echo $Row['id'];
that shows the correct value (128).
What might be the problem?

The 'id' argument specifies what property the radio button will write to when the form is submitted. However, you need to also specify what the radio button's value will be when selected. Do this through the function's third parameter:
echo CHtml::activeRadioButton($Row, 'id', array('value' => $Row['id']));

Related

Retrieve s:hidden value and add as a param to an url

I have an hidden input field which value changes according to the option selected in a s:select in Struts 2. Then, there's a button that points to an action like loadData.action.
I would like to add to this action a parameter named item.id with the value of the hidden field as its value, a value changing each time I select a new option.
I've tried to use an s:param with an s:property inside and the name or the id of the s:hidden, but it doesn't print the value after the = sign.
How can I do to achieve this result? loadData.action?item.id=12 where 12 is the value of s:hidden name="item" id="item" ?
Please Provide complete action link and which value to be change also your HTML structure, so it will be easy to answer your question
Assuming your HTML structure's elements, I have tried to answer your question
You can change action using jQuery on change event of your <s:select>
Have a look at https://jsfiddle.net/shantaram/qy9rew4v/
$('#id-select').change( function () {
var newAction = $('#id-form').prop('action');
newAction = newAction.substring(0, newAction.lastIndexOf('='));
$('#id-form').prop('action', newAction+"="+varItemId);
//varItemId is value of your hidden field
//which you want to change Dynamically
});
provided:
id-select -> Id of your <select> element
id-form -> Id of your <form> element

getQueryParams() from gridview's url after search and create another URL with same params for anchor tag inside GridView

I'm using GridView::widget, when I enter anything on search box inside gridview, it's creating url as below:
/v2/backend/web/index.php/agency/index?AgencySearch%5Bgovt_name%5D=&AgencySearch%5Bcity%5D=&AgencySearch%5Bmember_id%5D=22
Now I need to pass those paramters to url inside status icons present under GridView's Status column.
echo Url::to(['agency/change-status', 'id' => 33, 'set' => 11, Yii::$app->request->getQueryParams()]);
Above code is creating url as below:
/v2/backend/web/index.php/agency/33/change-status/11?1%5BAgencySearch%5D%5Bgovt_name%5D=&1%5BAgencySearch%5D%5Bcity%5D=&1%5BAgencySearch%5D%5Bmember_id%5D=22
And it makes sense because Yii::$app->request->getQueryParams() is an array. Note 1 inside url change-status/11?1%5BAgencySearch
Is there any simplest Yii way of achieving what I need instead of modifying Yii::$app->request->getQueryParams() as per Url::to() that is into string.
I'm trying to pass parameters in order to persist the state of gridview while loading contents again on grid after status change pjax request.
Use array_merge to combine your route and parameters and the search params.
$params = array_merge(['agency/change-status', 'id' => 33, 'set' => 11], Yii::$app->request->getQueryParams());
echo Url::to($params);

Yii, set radio button value fetch from database

In Yii, I listed my table which is fetch from database in grid view.
'value'=> 'CHtml::radioButton("set_default",false,array(
"value"=>"$data->id",
"set"=>"1",
"disable"=>"disable",
"uncheckValue"=>null
))',
the above code I entered in normal radio button view, how do I fetch from database? Anybody help me?
CGridView lets you write any value inside row's column instead of normal database values. For modify column values, you need to add a function for value attribute. Inside of this function, you can access each data's attributes. In your case, You need to echo a radio button like this:
//$data refres to each data row in the CGridView
'value' => function ($data, $row) {
echo
CHtml::radioButton("set_default",false,array(
"value"=>$data->id,
"set"=>"1",
"disable"=>"disable",
"uncheckValue"=>null
)),
},

Yii populate a dropdown on select of another

I have a dropdown that I want to populate when an item in another dropdown is selected. Both the dropdown are tied to data/model passed on from controller. and the first dropdown is populated from DB by calling a function in the model. Heres' the form,
echo $form->dropDownListRow($modelunit,
'superunit',
$model->getSunits(),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('user/getunits'),
'update'=>'#unit_id',
)));
echo CHtml::dropDownList('unit_id','', array());
Here's the action user/getunits called by Ajax.
$data=Unit::model()->findAll('sid=:sid',
array(':sid'=>(int) $_POST['superunit']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
I keep getting an error "Undefined index: superunit" when first dropdown is selected. Also, you may notice I am using form->dropDownListRow for the first dropdown while using CHtml::dropDownList for the second. That's cause I am clueless on the syntax of how exactly to make sure the dropdown is populated correctly with ajax and at also properly bind to the model.
You use $form->dropDownListRow that's why you will get $_POST['MyModelName']['superunit'] on your server side
Change you code like
$data=Unit::model()->findAll('sid=:sid',
array(':sid'=>(int) $_POST['MyModelName']['superunit']));
Where MyModelName is a model that you use)
Or like
echo CHtml::dropDownList('superunit'.....
For others - this wiki may help.

How can I determine if a radio button is checked already?

I'm using webrat with cucumber and I would like to test if a radio button is checked already when I am on a page.
How can I do that ? I didn't find any step in webrat which can do that.
expect(find_field("radio_button_name")).to be_checked
input("#my_box").should be_checked
There are cases when you cannot rely on checkboxes having ids or labels or where label text changes. In this case you can use the have_selector method from webrat.
From my working code (where I do not have ids on checkboxes).
response_body.should have_selector 'input[type=radio][checked=checked][value=information]'
Explanation: test will return true if body of document contains an radio button (input[type=radio]) which is checked and that has the value "information"
Just changed a web_step checkbox to radio button
Add the following step to web_steps.rb
Then /^the "([^"]*)" radio_button(?: within "([^"]*)")? should be checked$/ do |label, selector|
with_scope(selector) do
field_checked = find_field(label)['checked']
if field_checked.respond_to? :should
field_checked.should be_true
else
assert field_checked
end
end
end
And you can write the following to check whether the given raido button is checked or not
And the "Bacon" radio_button within "div.radio_container" should be checked
You can use the built-in checkbox matcher in web_steps.rb:
And the "Bacon" checkbox should be checked
However, you'll need to have a label on your checkbox that matches the ID of the corresponding checkbox input field. The f.label helper in Rails takes a string to use as the ID in the first argument. You may have to build a string that includes the field name and the checkbox name:
f.label "lunch_#{food_name}, food_name
f.radio_button :lunch, food_name
In any case, use this directive to see that you've got the HTML correct:
Then show me the page
Wrapped Jesper Rønn-Jensen his function + added name which is used by rails:
Then /^I should see that "([^"]*)" is checked from "([^"]*)"$/ do |value, name|
page.should have_selector "input[type='radio'][checked='checked'][value='#{value}'][name='#{name}']"
end
And the "Obvious choice" checkbox should be checked
Although it might be a radio button, but the code will work. It is just checking for a fields labelled with that text.
You can use method checked? on your field
expect(find_field("radio_button_id").checked?).to eq(true)