I want to do a search using a form which will search name field, brand field and category field.
Should I be using post or get for this situation?
How/where should I be creating my query search model, model or controller?
Any code snippets would be great.
I have the following in my view:
<?php $form = ActiveForm::begin(['id' => 'home-search','method' => 'get', 'action' => Url::to(['productitem/search'])]); ?>
<?= $form->field($productitem, 'name')->textInput(array('placeholder' => 'What are you looking for?'))->label(false) ?>
<?= $form->field($productitem, 'brand_id')->dropDownList(
ArrayHelper::map(ProductBrand::find()->all(),'id','name'),
['prompt'=>'Select Brand']
)->label(false) ?>
<?= $form->field($productitem, 'category_id')->dropDownList(
ArrayHelper::map(ProductCategory::find()->all(),'id','name'),
['prompt'=>'Select Department']
)->label(false) ?>
<div class="form-group search-button">
<button type="submit" class="btn btn-primary" name="login-button">Search <i class="fa fa-lg fa-arrow-circle-o-right"></i></button>
</div>
<?php ActiveForm::end(); ?>
The answer to your questions:
I would use get just for the convenience of pressing the "back" button. You are not changing the database at all so a GET should provide enough security / convenience
search model should be in the [Model]Search. Yii2 has specialized search models on top of the normal models why not keep them there? That is exactly what they are there for.
No code snippets SO helps you with your code, does not usually writes it for you. But then again somebody will probably come and spoon feed you code anyway so you will not learn anything so enjoy.
Related
Let's say I have created a button component I want to include into my project with Sage 10's #include function, how do I add fallbacks to the variables, in case I or another user forget to add a value?
My button*:
<button class="<?= esc_attr($button_class); ?>">
<?= esc_html($button_label); ?>
</button>
My include
#include('partials.button', [
'button_class' => 'btn btn-primary',
'button_label' => 'label',
])
How can I add fallbacks, so button isn't empty if I don't add button_label?
*Code is simplified for this example
In the external PHP-file, with the button, you can add the variables between some PHP-tags, to catch any empty variables and instead return a fallback.
In this case, the PHP-file with the <button> element would look like this:
<?php
$button_class = $button_class ?: 'lg:block w-full lg:w-auto btn btn-primary mr-4';
$button_label = $button_label ?: __( 'Insert text here', 'fallback', 'edc' );
?>
<button class="<?= esc_attr($button_class); ?>">
<?= esc_html($button_label); ?>
</button>
Sage will first apply the variables from the #include-part to the variables in the top, after which PHP uses the value as is after the closing ?>-tag.
I need to implement a button created in a view. In other programming languages this is very easy: your button has an ID so you can make a reference to it in a controller to implement its action. But in PHP I see like there are some predefined buttons (ex, submitbutton) and I don't understand how can you link an action with a button.
If someone could help me it would be very nice!
First you create an Action in your controller and then in your view try this:
<?= Html::a('YourFormName', ['yourControllerName/yourActionName'], ['class' => 'btn btn-success']) ?>
In an ActiveForm
<?php $form = ActiveForm::begin(); ?>
<div class="form-group">
<?= Html::submitButton('Button caption', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
This is the code for submit button
<?= Html::submitButton('Button Name',['class'=>'btn btn-success'])?>
if you want to make button from link here is the code
<?= Html:a('Caption',['controller/action'],['class'=>'btn btn-success'])?>
if you want to pass some query string in link the her is the code
<?= Html::a('caption',['controller/action','id'=>$model->id],['class'=>'btn btn-success'])?>
In
#dektrium/user/views/admin/_account.php
#dektrium/user/views/admin/_info.php
#dektrium/user/views/admin/_profile.php
there are
<?php $this->beginContent('#dektrium/user/views/admin/update.php', ['user' => $user]) ?>
'the rest codes'
<?php $this->endContent() ?>
and in #dektrium/user/views/admin/update.php there're
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-body">
<?= $content ?>
</div>
</div>
</div>
the $content will be replaced by code between 'beginContent' and 'endContent', how to implement this kind of layout in my new backend model 'Rayon'? I tried write a similar CRUD code, but keep getting an error 'Undefined variable content'.
Thank you for your help.
the row with code
<?php $this->beginContent('#dektrium/user/views/admin/update.php', ['user' => $user]) ?>
tells to the "view" which is, to take the code from the reference and add it to the place where the call is invoked ..
It performs an equivalent action to "include" within more the possibility of pass the variables to use in the "content"
then you should create the part of view that you want to reuse .. and the views in which you want to call to add this type of call
You can do something similar directly reusing the render () function inside de view and indicating which (other) view and which variables to use.
for (simple) example
view container_view.php in yourapp/views
<div>my container test</div>
<?= $content ?>
then
You want that the code in container_view is placed in a way that the code inside beginContent and endContent of the
create.php in yourapp/views/ is place in the same place you have $content in the container
<?php $this->beginContent('yourapp/views/container_view.php', ['model' => $model]) ?>
<div>this code is placed in $container</div>
<div>and the value of the var model is passed</div>
<br />
<?= $model->name '>
<?php $this->endContent() ?>
My apologies for the vague title.
I'm having trouble coming up with a way to pass input from a textfield forward to an action I'm calling with the button on that view.
The code below is an ActiveForm I have on my view.
<div class="user-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'unique_key') ?>
<?= Html::a('Test', ['//order/create', 'unique_key' => 'entered key here'], ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
</p>
What I want to accomplish is that the user fills the text field with a (randomly generated) key.
Then, when they click the button, their entered key should be passed through to the order/create action. Problem is that I think it is impossible to get the entered key at this stage as I think it isn't saved anywhere yet.
I know this probably is wrong in so many different ways but I'm still trying to learn.
Is there any way I can accomplish what I'am trying to do ?
Thanks in advance
You simply need a better form config and a button to submit your form :
<?php $form = ActiveForm::begin([
'action' => '//order/create',
'method' => 'get',
]); ?>
<?= $form->field($model, 'unique_key') ?>
<?= Html::submitButton('Test', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
I have a file input in my ActiveForm and I want to style it
but classes btn btn-primary doesn't have any effect on that
<?php
use yii\widgets\ActiveForm;
?>
<div class="jumbotron">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'file',['class'=>'btn btn-primary'])->fileInput() ?>
<button class="btn btn-default">Submit</button>
<?php ActiveForm::end() ?>
</div>
At this point everything is well fine. However, when I try to use the parameter options of the field method as follows:
<?= $form->field($model, 'file',['class'=>'btn btn-primary'])->fileInput() ?>
I have got the error:
Class btn btn-primary does not exist
but I used 'btn btn-primary' class in
<button class="btn btn-default">Submit</button>
and it worked very well.
Could anybody explain me why this error has been occurred?!
In total approach you can't style file button directly
You can make the primary file input hidden and use another button.
When this button clicked, you must trigger the click event of input file using js or jquery
Or you can write it like below
<?= $form->field($model, 'file')->label(null,['class'=>'btn btn-primary'])
->fileInput(['class'=>'sr-only']) ?>