yii2 formwizard checkbox field and customize preview step - yii2

I am using yii2-formwizard and I want to insert a checkbox as form input field for the field is_legal in a tabular step.
So in fieldConfig array, reading the documentation, I inserted the following code:
'is_legal' => [
'options' => [
'type' => 'checkbox',
],
'labelOptions' => [
'label' => \Yii::t('app', 'Legal Representative'),
],
],
The following image shows the result:
However, when I go the preview step I see the field of the checkbox set as undefined:
In fact, when I try to save the model, the is_legal field is not set.
First Question: where is the problem with the checkbox form field?
Second Question: is there any way to customize the preview step? For example instead of 'Step 5', I would like to write 'Legal Data'.

i created this widget and there are a few things you need to know.
First Answer
The undefined it is showing is not the value but the label of the checkbox, if you look into the getLabel() function in the formwizard.js file it looks into the siblings of the input field for a label and gets its text
let text = $('#' + fieldName).siblings('label').text();
to display on the preview step, and by default Yii2 wraps the input inside the label like
<label><input stype="checkbox"></label>
so you need to use the template option of the checkbox like below
'is_legal' => [
'options' => [
'type' => 'checkbox',
'template' => '{input}{beginLabel}{labelTitle}{endLabel}{error}{hint}',
],
'labelOptions' => [
'label' => \Yii::t('app', 'Legal Representative'),
],
],
Second Answer
No, currently the widget does not support the custom title for the Preview step sections, but i think I can add the support for providing the title of the headings as it makes sense too, if that sorts your problem.
Update
Ignore the Second Answer given above i just pushed the updates to the live branch you can now use the previewHeading option in the step settings. Update your composer by running composer update to update to the latest version and clear cache using CTRL+F5.
See the following sample code how to use previewHeading option
use buttflattery\formwizard\FormWizard;
echo FormWizard::widget([
'enablePreview'=>true,
'steps'=>[
[
'model'=>$user,
'title'=>'My Shoots',
'previewHeading'=>'My Heading Step 1',
'description'=>'Add your shoots',
'formInfoText'=>'Fill all fields'
],
[
'model'=> $profile,
'title'=>'My Shoots',
'previewHeading'=>'My Heading Step 2',
'description'=>'Add your shoots',
'formInfoText'=>'Fill all fields'
],
]
]);

Related

HTML in YII2 Kartik Gridview column label (not heading)

I have a Kartik gridview in YII2 and I need to be able to put HTML into the label (not header, I'll come to that) of a column.
My column definition is as such
[
'attribute' => 'picked_percent',
'format' => 'raw',
'label' => 'P<span class="responsive">icked</span>',
],
But if I do this, it outputs
P<span class="drawn_head">icked </span>
I can change label to header and it looks fine but I need it to be clickable and it isn't when I change it to header.
I've also tried changing the format from raw to HTML and that makes no difference.
Any help is much appreciated.
If you want the label to not be encoded you need to add this config option in the column definition array:
'encodeLabel' => false

yii2 formwizard checkbox field value set to 1 even if not selected

I am using yii2-formwizard and I want to insert a checkbox as form input field for the field is_legal in a tabular step. So in fieldConfig array, reading the documentation, I inserted the following code:
'is_legal' => [
'options' => [
'type' => 'checkbox',
'template' => '{input}{beginLabel}{labelTitle}{endLabel}{error}{hint}',
],
'labelOptions' => ['label' => \Yii::t('app', 'Legal Representative')],
],
If I select the checkbox or not the value of the field is always 1 as shown on: .
However, when I add another instance of the model, in the preview step I have NA as value of the legal representative field :
Yes, you are correct about it. It incorrectly shows the value even if the check box is not checked i have updated the section and added a fix.
//check if single checkbox input
if (inputType.attr("type") == 'checkbox') {
return inputType.is(":checked") ? inputType.val() : '';
}
To get the latest code you need to repeat the steps for running composer using,
composer update
and clear the browser cache along with clearing the assets folder in the web directory.
Normally when i am working locally with extensions or if there is an update for an extension which includes javascript updates i add the following settings under the components array in my local config file that takes care of getting the latest files from, and the assets are force copied every time to the web/assets/ directory whenever you refresh the browser or the page reloads.
'components'=>[
'assetManager' => [
'forceCopy' => true,
],
]
Note: Dont leave it open on live site as it would make the page load slower.

kartik Select2 FilterType in gridview doesn't work

I have an issue about kartik select2 filterType that doesn't work , this is a portion of my code :
[
'format' => ['link',$idmodel],
'attribute' =>$attribute,
'filterType'=>GridView::FILTER_SELECT2,
'filterWidgetOptions' => [
'data' => \yii\helpers\ArrayHelper::map($model_::find()->all(),$attribute,'nom_'.strtolower($idmodel)),
'options' => ['placeholder' => '-'.$idmodel.'-'],
'pluginOptions' => ['allowClear' => true],
'theme' => Select2::THEME_KRAJEE,
],
When i comment this line :
'filterType'=>GridView::FILTER_SELECT2,
a text field appear in my filter grid view.
Can any one help me ??
Thanks in advance
As I see it from your conversation with Edvin Tenovimas your problem is that no jQuery Javascript file is loaded. Did you installed the Select2 package normally with composer?
$ php composer.phar require kartik-v/yii2-widget-select2 "#dev"
In that case you have to see the file vendor/bower/jquery/dist/jquery.min.js. The file has to be copied into your assets directory under web/assets automatically. You could remove the content of the assets directories to force the creation of the assets again.
$ rm -rf web/assets/*
I believe by default GridView enables filtering unless you manually disable it. When you comment that out, it uses default text input instead of Select2. To also disable that one, use this on columns you want to disable filtering:
'filter' => false,

Join Table with the id of another join table

i'm currently trying to build a backend for a project. In this project you will be able to create "ContentElements" that can be used to display content in a page (in my case Sites). Every ContentElement can have multiple options. When a user creates a new Site with an ContentElement (e.g. header) he would enter all options of the element. For example:
"src": "/img/bg.jpg",
"text": "Lorem ipsum..."
In order to save the option's value per page it is used in, i store these values in a separate table (content_elements_sites_values).
My scheme currently looks like this: data scheme
So what i'm currently trying to do is when i get all data associated with the Site i also want to get the data from 'content_elements_sites_values'
$site = $this->Sites->get($id, [
'contain' => ['Templates', 'Colors', 'SiteTypes', 'ContentElements' => [
'ContentElementOptions' => [
'ContentElementsSitesValues'
]
]
],
'conditions' => [
// Just to explain my problem.
'ContentElementsSites.id' => 'ContentElementsSitesValues.content_elements_sites_id'
]
]);
I really don't know if this is even possible or even if my "design" is a total bull***t. But i cannot think of another way to store the filled in data. I'm very open to suggestions of a better way to store this data. Please ask if you need further information in order to help me.
Thank you in advance!
EDIT
I try to explain better what i want to achieve.
Every site can have multiple content_elements of the same type (association is stored in content_elements_sites junction table).
Every content_element can have multiple content_element_options
All content_element_options and content_elements are defined by an Admin.
Another user can create a site and populate it with content_elements and enter content_elements_sites_value for all content_element_options. And as the same content_element (e.g. a paragraph or a list) can have multiple occurrences in the same site, i'll need to store every content_elements_sites_value the user entered.
Thats why i created the link between content_elements_sites and content_element_options.
Currently i'm using this query to get everything expect the value:
$site = $this->Sites->find('all', [
'conditions' => [
'Sites.id' => $id
],
'contain' => ['ContentElements' => [
'sort' => [
'ContentElementsSites.order' => 'ASC'
],
'ContentElementOptions' => [
'ContentElementsSitesValues' => [
'conditions' => [
'ContentElementsSitesValues.content_elements_sites_id' => 'ContentElementsSites.id',
]
]
]
]
]
]);
This results in empty content_elements_sites_values
(int) 1 => object(App\Model\Entity\ContentElementOption) {
'id' => (int) 7,
'content_element_id' => (int) 1,
'name' => 'Test',
'json_name' => 'test',
'option_type_id' => (int) 1,
'content_elements_sites_value' => null,
}
My scheme currently looks like this: data scheme
I'm wondering if this query is even possible. Or if the whole thing is just too flexible.
The way you have defined the relationships signifies that you wish to have a very modular approach so that a content element can be used with multiple sites and a content element option can be used with multiple content elements.
If that is the case, schema direction looks okay with few changes :
1) content_elements_sites_values table can have site_id column directly instead of content_elements_sites_id column as site will be always unique for an entry in that table so the connection id of content_elements_sites isn't required.
2) content_elements_sites_values table can be renamed to content_element_options_values.
3) You can remove id column from content_elements_sites and content_elements_sites_values junction tables.
maybe this is what you are looking for :
$site = $this->Sites->get($id, [
'contain' => ['Templates', 'Colors', 'SiteTypes', 'ContentElements', 'ContentElements.ContentElementOptions','ContentElements.ContentElementOptions.ContentElementsSitesValues'],
....]);
dont forget to define association relationship in model table.

Changing selected option in HTML::Form

I am working on an automated form submit script. It is logging in to a vendor's website and populating the fields of a form. When trying to submit, the desired result would be a ticket number displayed, which is acknowledging the form is submitted and the request is processed by their helpdesk.
However the form is not submitted correctly (no acknowledgement is displayed) and I suspect that it is caused by one of the inputs which is a SELECT.
Here is the code I use to set this field:
$forms[3]->value('ProductList','-2');
This has no effect on the the prepared form unfortunately, dumping $forms[3], i see this:
[...]
bless({
'onchange' => ' checkKC(document.all.ProductList, \'~0\'); prodExpand();',
'current' => 1,
'menu' => [
{
'seen' => 1,
'value' => '~0',
'name' => '<Please select>'
},
{
'seen' => 1,
'value' => '-2',
'name' => 'Product not found.... Search more'
},
{
'value' => '-1',
'name' => '------------------------------------'
},
{
'value' => 'Product1',
'name' => 'Product 1 Name'
}
],
'name' => 'ProductList',
'id' => 'ProductList',
'idx' => 1,
'type' => 'option'
}, 'HTML::Form::ListInput' ),
[...]
Am I using the right method of $forms[3]? (it was created by HTML::Form->parse($pageresult) btw) Or is there any other method I should try? I can't find any documentation for HTML::Form::ListInput
Thanks for any advice
Consider using WWW::Mechanize for form processing that takes more than one step. That way you can include the login process in your script along with going to the form and of course getting the result.
Or if you need to work with JavaScript, then use WWW::Mechanize::Firefox.