Radio Button group separately in yii2 without using radiolist - html

I need Radio Button group in separate place in yii2 like in this question . I can customise the radio button like in this picture
but when I submit the form only the value of last radio is always submitted if last radio button is clicked respective value else null . I am not able to submit previous checked value.
mine form looks like
<div class="cart-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'product_id')->hiddenInput()->label(false) ?>
<?= $form->field($model, 'size_id')->dropDownList(
ArrayHelper::map(Productsize::find()->where(['product_id'=>$id])->all (), 'product_size_id', 'product_size' ),
[ 'prompt'=>'Select size',
'onchange'=>'
$.get( "'.Yii::$app->urlManager->createUrl('cart/lists?id=').'"+$(this).val(),
function( data ) {
$( "#pricetag11" ).val(data);
$( "#pricetaghtml" ).html(data);
});
']); ?>
<?php $colors = Productcolor::find()->where(['product_id'=>$id])->all(); ?>
<h4>Price:</h4> <p id="pricetaghtml" style="color:red;"></p>
Select size to see the price
<?php echo $form->field($model, 'price_id')->hiddenInput(['id'=>'pricetag11'])->label(false)?>
<!-- here is my problem -->
<h4>Color:</h4>
<?php foreach($colors as $color):?>
<div style="background-color:<?= $color->product_color;?>;width:20px;height:20px;padding-left:3px; display: inline-block"> <?= $form->field($model, 'color_id')->radio(['value' =>$color->product_color, 'label'=>'','uncheck'=>'null']); ?></div>
<?php endforeach;?>
<!-- -->
<?= $form->field($model, 'quantity')->textInput(['type' => 'number','min'=>'0','max'=>'9999' , 'placeholder'=>'0']) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
In my scenario, I have to load color (hexcode) from database table and display in form and need to save the respective code in another table using radio button.
I have tried so many ways to accomplish this task in yii2 but i am not able. i hope someone will point me to the right direction.

I guess this will work,
<?= $form->field($model, 'color_id')->radioList(
$colors,
[
'item' => function($index, $label, $name, $checked, $value) {
$return = '<div style="background-color:'.$value.';width:20px;height:20px;padding-left:3px; display: inline-block">';
$return .= '<input type="radio" name="' . $name . '" value="' . $value . '">';
$return .= '</div>';
return $return;
}
]
)
->label(false); ?>

Related

How to write the material design radio button in yii2?

I am facing issue with material design radio button using in yii2.
this is the material design radio button which is working perfectly
<div class="radio">
<label>
<input type="radio" name="q_option" value="Recent advances in MS">
Recent advances in MS
</label>
</div>
but when I write the same radio button in yii2 framework, the radio button dot become disappear and become non clickable
<div class="radio">
<?= $form->field($model, 'q_option')->radio(
['label' => 'Recent advances in MS', 'value' => 'Recent advances in MS']
) ?>
</div>
please guide me. Thanks a lot in advance.
Maybe be you mean radio buttons? Single radio doesnt have mean.
//controller
$model = new Cities();
return $this->render('index', [
'model' => $model,
]);
//view <?php
use yii\bootstrap\ActiveForm;
?>
<?php $form = ActiveForm::begin() ?>
<div class="radio">
<?= $form->field($model, 'city_name')->radio(
['label' => 'Recent advances in MS', 'value' => 'Recent advances in MS']
) ?>
</div>
<?php $form::end() ?>
work well
You use $model and table_field_name. Maybe you tried use radio button list? Like that:
//view
$model = new Cities();
return $this->render('index', [
'model' => $model,
'array' => Cities::find()->all(),
]);
//controller
<?php
use yii\bootstrap\ActiveForm;
use yii\helpers\ArrayHelper;
?>
<?php $form = ActiveForm::begin() ?>
<?= $form->field($model, 'city_name')
->radioList(ArrayHelper::map($array, 'id', 'city_name')) ?>
<?php $form::end() ?>

Yii2: How to validate a form with multiple instances of the same model

In my form I update more start and end dates from the same model at once. See the simplified form:
<?php $form = ActiveForm::begin(); ?>
<?php foreach($dates as $i=>$date): ?>
<?= $form->field($date,"[$i]start"); ?>
<?= $form->field($date,"[$i]end"); ?>
<?php endforeach; ?>
</table>
<?= Html::submitButton('Save'); ?>
<?php ActiveForm::end(); ?>
In the model I need to control, if the end date is after the start date:
public function rules() {
return [
[['end'], 'compare', 'compareAttribute' => 'start', 'operator' => '>', 'message' => '{attribute} have to be after {compareValue}.‌'],
];
}
I tried to change selectors similarly as described in: Yii2: Validation in form with two instances of same model, but I was not successful. I suppose I need to change the 'compareAttribute' from 'mymodel-start' to 'mymodel-0-start' in the validation JS:
{yii.validation.compare(value, messages, {"operator":">","type":"string","compareAttribute":"mymodel-start","skipOnEmpty":1,"message":"End have to be after start.‌"});}
So, I look for something like:
$form->field($date,"[$i]end", [
'selectors' => [
'compareAttribute' => 'mymodel-'.$i.'-start'
]
])
Solution
The solution is based on the answer of lucas.
In the model I override the formName() method, so for every date I have a unique form name (based on ID for the existing dates and based on random number for new dates):
use ReflectionClass;
...
public $randomNumber;
public function formName()
{
$this->randomNumber = $this->randomNumber ? $this->randomNumber : rand();
$number = $this->id ? $this->id : '0' . $this->randomNumber;
$reflector = new ReflectionClass($this);
return $reflector->getShortName() . '-' . $number;
}
The form then looks like this:
<?php $form = ActiveForm::begin(); ?>
<?php foreach($dates as $date): ?>
<?= $form->field($date,"start"); ?>
<?= $form->field($date,"end"); ?>
<?php endforeach; ?>
</table>
<?= Html::submitButton('Save'); ?>
<?php ActiveForm::end(); ?>
Override the formName() method in your model class to make it unique. If you don't want to change your model class, create a subclass of it for the purpose of working for this controller action. After doing this, the html ID and name fields will automatically be unique.

How to create a dropdown dependent on another dropdown in yii2?

I have two api's:
1: returns all the industries,
2: returns all the industry category(based on industry id).
I need two dropdowns, one dependent on other. On selecting industry 2nd dropdown should show only relevant categories.
Thanks in advance.
I got it. I have simply used ajax which posts the value from one dropdown and sends the data to an action which returns the data and i am simply putting those values to my other drop down. :)
<?= $form->field($model, 'industryId')->dropDownList($industry,
['prompt'=>'Select Industry',
'onchange'=>'
$.get( "'.Url::toRoute('/site/category').'", { id: $(this).val() } )
.done(function( data ) {
$( "#'.Html::getInputId($model, 'industryName').'" ).html( data );
}
);
','class' => 'form-control'
]
); ?>
<?= $form->field($model, 'industryName')
->dropDownList(
['prompt'=>'Select category','class' => 'form-control']);
?>
_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\Category;
?>
<?php $form = ActiveForm::begin(); ?>
$model = Category::find()->select('id,name')->orderBy('name asc')->all();
$listData = ArrayHelper::map($model, 'id', 'name');
<?= $form->field($model, 'industryId')->dropDownList($listData,
['prompt'=>'Select Category',
'onchange'=>'
$.get( "'.Url::toRoute('/category/subcats').'", { id: $(this).val() } )
.done(function( data ) {
$( "#'.Html::getInputId($model, 'sub_category').'" ).html( data );
}
);
','class' => 'form-control'
]
); ?>
<?= $form->field($model, 'sub_category')
->dropDownList(
['prompt'=>'Select sub cat','class' => 'form-control']);
?>
----
You can use this extension. You can find explanation of plugin on its guide page.

Login form requires second click to submit

My backend login form submits with a single click of the login button. It creates an <input type="hidden" name="login-button"> element then shortly thereafter submits. On the frontend the same is created on click but never posts the data until I click the button again.
Here is a sample of the view code:
<div id="login-form" class="col-sm-12">
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<div class="col-sm-5 col-sm-offset-4 forgotPassword">
<?= $form->field($model, 'rememberMe')->checkbox() ?>
<div style="margin:auto;color:#fff;margin:1em 0;font-size:10px;">
if you forgot your password you can <?= Html::a('reset it', ['site/request-password-reset']) ?>
</div>
<div class="form-group">
<?= Html::submitButton('login', ['class' => 'btn btn-primary loginButton', 'name' => 'login-button']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
The only difference I am seeing between the two is that there are some additional <div> tags that have been added for styling purposes. I have even removed those and I am still seeing the same issues.
Any ideas would be appreciated.
It looks like the id on the form div was causing the issue. Removing this has cleared everything up.
It was causing a conflict because it had the same id as the form.
I just want to add that usually you assign model "formName" as your form id
<?php $form = ActiveForm::begin(['id' => $model->formName()]); ?>

Different sidebars for different pages on Wordpress

I'm trying to design a theme on WordPress (version 3.3.2) but I am having a few problems in having the sidebar display a different set of widgets on certain pages.
I have tried several online tutorials and this one in particular http://www.rvoodoo.com/projects/wordpress/wordpress-tip-different-sidebars-on-different-pages/ but unfortunately there is no change in the sidebar when I move to a different page
I registered two sidebars on my functions.php as shown below, one which I would consider as main, and the other as a custom sidebar, and I also added different widgets to these sidebars.
<?php register_sidebar( //register sidebar
array(
'name' => 'Right-side',
'before_widget' => '<div class="rightwidget">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
));
register_sidebar( //register second sidebar
array(
'name' => 'Second-right',
'before_widget' => '<div class="rightwidget">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
));
?>
Following that, I created the files sidebar.php and sidebar-second.php to be able to call them.
sidebar.php
<div id="sidebar">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Right-side')) : ?>
<h3 class="widget-title">No widget added</h3>
<p> Please add some widgets</p>
<?php endif; ?>
</div><!--ends sidebar-->
sidebar-second.php
<div id="sidebar">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Second-right')) : ?>
<h3 class="widget-title">No widget added</h3>
<p> Please add some widgets</p>
<?php endif; ?>
</div><!--ends sidebar-->
And then I added replace my <?php get_sidebar() ; ?> statement with following conditional
<?php if( is_page('225') ) : ?>
<?php dynamic_sidebar('second'); ?>
<?php else : ?>
<?php get_sidebar() ; ?>
<?php endif ; ?>
However only the widgets added to the sidebar.php are displayed on every page. Any help on how I could change this, or pointers on what I could be doing wrong. Thanks.
I looks like you are using <?php dynamic_sidebar('second'); ?> where you should be using <?php get-sidebar('second'); ?>
What get_sidebar('foo') does is look for a file named 'sidebar-foo.php' in the root directory of your theme and includes it. dynamic_sidebar('bar'), on the other hand, looks for a sidebar registered with:
<?php register_sidebar( array( 'name' => 'bar' ... ) ) ?>
Hope this helps!
Why don't you use the Custom Sidebar Plugin?
It's really easy to use and does not need any coding
You have two sidebars. when you want to use the sidebar-second file on a particular page, instead of calling
<?php get_sidebar('second') ; ?>
TRY
<?php get_sidebar('sidebar-second.php') ; ?>
this should do the trick