Yii2 - radioList choosing bindend to two different fields - php

I have two radioList bindend to two different fields:
<div class="form-group row">
<div class="col-lg-5">
<?= $form->field($model, 'isGdo')->radioList([0 => 'No', 1 => 'Yes'])->label("Are you a distributor ?"); ?>
</div>
<div class="col-lg-5">
<?= $form->field($model, 'isProducer')->radioList([0 => 'No', 1 => 'Yes'])->label("Are you a producer?"); ?>
</div>
</div>
Now my aim is to refactor this and create a radioList with 3 options in which I can set both these properties.
I know that I can switching to a single field with 3 values, but there are a lot of checks in the program, so keeping these two fields separate could be better for me.

Create A radio 3 radio buttons for showing with same name.
isGdo and isProducer
kept as hidden input field .
Write jquery code on above radio button and set that value on input box on basis of selection of radio button.
echo $form->field($model, 'isGdo')->hiddenInput(['value'=>''])->label(false);
echo $form->field($model,'isProducer')->hiddenInput(['value'=>''])->label(false);
$form->field($model, 'publicattribute')->radioList([1 => 'yes', 0 => 'No'])->label('');
<script>
$(document).ready(function(){
$("input[type='button']").click(function(){
var radioValue = $("input[name='']:checked").val();
if(radioValue){
alert("Your are a - " + radioValue);
**insert that value on your condition in the hidden input field**
}
});
});
</script>

Related

multiple select field - select field must be an array and just one <option> is shown in the $request output

I have the code below that is a resume of the process to create a new conference. To create a new conference is ncessary that the user introduce some info like the conference name, etc. The user also needs to introduce between 1 and 3 categories for the conference.
So there is a select element using select2 plugin so the user can select the categories:
<div class="form-row">
<div class="form-group col-lg-6">
<label for="categories">Category</label>
<select id="tag_list" required multiple class="form-control" value="{{ old('categories') }}" name="categories" id="categories">
#foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
</div>
</div>
Then the laravel code to store the conference info and also the categories of the conference in the conference_category table since there is a many to many relationship between confernece and category:
public function store(Request $request)
{
$this->validate($request, [
'conference_name' => 'required|max:255|string',
'conference_categories' => 'required|array|between:1,3|integer',
]);
$conference = Conference::create([
'name' => $request->conference_name,
]);
$conference->categories()->attach($request->conference_categories);
}
The select2 JS:
$(function() {
$('#tag_list').select2({
placeholder: '',
dropdownAutoWidth: 'true',
width: '100%'
});
});
Errors
If the user selects more than one category in the $request output just appears the id of one category
And it appears a laravel validation error after submit the form "The conference categories must be an array.
"
Do you know where can be the issue?
You gotta change the name attribute of your select to an array, like this and also remove one id attribute from it, it can't have 2
<select required multiple class="form-control" value="{{ old('categories') }}" name="categories[]" id="categories">

CakePHP 3.3 - Formbuilder first value to be 1

I got a problem with CakePHP formbuilder. The first value of the options have to be 1 instead of 0.
View:
<div class="form-group">
<label class="col-md-3 control-label" for="competentie">Competenties</label>
<div class="col-md-6">
<?php echo $this->Form->input('competence._ids',['class'=>'form-control ','options'=> $competence,'div'=>false,'label'=>false]); ?>
</div>
</div>
Controller:
$category = $this->Cv->Category->find('list', ['keyField' => 'category_id', 'valueField' => 'category']);
$this->set(compact('cv', 'category'));
$this->set('_serialize', ['cv']);
Any idea how i can make it start at value 1? Instead of 0.
Screenshot of it: https://i.gyazo.com/5ebeeeccc11ee08762efd9a84ba357ba.png
Does categories table have category_id field ?
If not you may trying to do:
$category = $this->Cv->Category->find('list',
['keyField' => 'id', 'valueField' => 'category']);
Otherwise look at the debug value one more time and confirm if data are coming correctly.

Array of values from the same radio button group

I'm making a website in Laravel where user needs to enter his pseudonym, and on the settings page he can add multiple pseudonyms with jQuery. When he clicks the plus button, a text field, radio button and minus fields are added.
Radio buttons are there to mark one of those nicknames as default.
Image: screenshot of how the inputs look like
How do I pass an array of values out of those radio buttons? ie, if I selected 3rd out of 4 pseudonyms to be default:
{'0' => null, '1' => null, '2' => '1', '3'=> null}
So I can merge this array into pseudonyms array to be like:
{'0' => {'0' => 'John Doe', '1' => null},
'1' => {'0' => 'John Smith', '1' => null},
'2' => {'0' => 'John Wayne', '1' => '1'},
'3' => {'0' => 'John X', '1' => null}}
Finally, I would save it like this, or some other method that would also check if already existent and then update, but that's not important right now:
foreach($pseudonyms as $pseudonym)
$user->pseudonyms()->create(['name' => $pseudonym[0], 'status' => $pseudonym[1]]);
I'm probably just inexperienced and don't know some html/php basics that will let me do that, but what can I do :)
Anyway, here's the Blade part where inputs are:
<div class="input_fields_wrap">
<div class="row">
<div class="col-md-8">
{!! Form::text('pseudonyms[]', null,['class' => 'form-control']) !!}
</div>
<div class="col-md-2">
{!! Form::radio('default[]', 1, null,['class' => 'form-control']) !!}
</div>
<div class="col-md-2">
{!! Form::button('<i class="fa fa-plus" aria-hidden="true"></i>',['class'=>'btn btn-default add_field']) !!}
</div>
</div>
</div>
And here is the part of jQuery that appends the additional fields in .input_fields_wrap
$(document).ready(function() {
var max_fields = 5; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap .row"); //Fields wrapper
var add_button = $(".add_field"); //Add button ID
var added_fields = 1; //initial text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(added_fields < max_fields){ //max input box allowed
$(wrapper).append('<div class="added"><div class="col-md-8 class1"><input type="text" name="pseudonyms[]" class="form-control"/></div>' +
'<div class="col-md-2 class2" ><input type="radio" name="default[]" value="1" class="form-control"/></div>' +
'<div class="col-md-2 class3" ><input type="button" value="-" class="remove_field btn btn-default add_field fa fa-minus"></div></div>'); //add input box
added_fields++; //text box increment
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault();
$(this).closest('.added').remove();
added_fields--;
})
});
Thank you for your comments!
Of course, solution was working on something else, coming back and having an answer after 2 minutes thinking about it. Created new array with a key from the radio button's value, so I could merge it with the other array with
foreach($request->pseudonyms as $key => $value)
{
if(array_key_exists($key, $default))
{
$pseudonyms[$key] = array($value, $default[$key]);
} else {
$pseudonyms[$key] = array($value, null);
}
}

How to disable button when text input is empty in yii2?

I want to disable button when text input is empty in yii2?
can you help me?
this is my code
thanks
<div class="pemesanan-search thumbnail padding-baru col-sm-6 input">
<?php $form = ActiveForm::begin([
'action' => ['menubayar'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'NO_REKENING') ?>
<?= $form->field($model, 'NO_KARTU') ?>
<?= $form->field($model, 'NAMA') ?>
<div class="form-group">
<center><?= Html::submitButton('Cek Kartu Kredit', ['class' => 'btn btn-default']) ?></center>
</div>
<?php ActiveForm::end(); ?>
</div>
First disable the submit button by default, by using disabled attribute
in button's option.
After that you have to register a JS code using Yii2 RegisterJS.
Then trigger event using jQuery to remove disabled attribute after the input value is changed.
$( "# [id of the input that needs value]" ).change(function() {
$('input[type="submit"]').removeAttr('disabled');
});
By using above code, if you change the value of the input whith this id, it means that its not empty anymore and the disabled attribute will be removed.
Or also you can check if $("# [id of the input that needs value]").val(); is not empty remove the disabled attribute again with this code:
$('input[type="submit"]').removeAttr('disabled');
P.S: You may also define rules in its model and make that attribute as Required in the model rule, and let Yii handles the validation. You can learn more about that by reading This document.

How to Customizing/add a class to the ActiveForm items with Material Bootstrap design classes on Yii2

I'm trying to add a class for a TextField like this one:
<div class="form-group-lg">
<?= $form->field($model, 'email'); ?>
</div>
Output:
And that would be something like this?(the label will be inside the TextField).i mean, can we use it like below codes?
Example:
I need something like this:
<div class="form-group label-floating">
<label class="control-label" for="focusedInput1">Write something to make the label float</label>
<input class="form-control" id="focusedInput1" type="text">
</div>
http://fezvrasta.github.io/bootstrap-material-design/bootstrap-elements.html
Forms section-first one.
So, the question is,
How can i add a class to this item with above codes on Yii2 ActiveForm?
You need to use fieldConfig Propertie in ActiveForm.
Like as
<?php $form = ActiveForm::begin([
'id' => 'active-form',
'fieldConfig' => [
'template' => "<div class=\"form-group label-floating\">{label}{input}{error}</div>",
],
]); ?>
For more info read Docs

Categories