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']) ?>
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 my signup form with email input to work wherever I want on the site.
I have an EmailsController that has add() and will add an email when I am on the add page.
How do I get the layout for Emails/Add.ctp to be injected into my home page and wherever I like while keeping the functionality and giving feedback to a user?
<div>
<?= $this->Element('upone_scriptimports'); ?>
<?= $this->Form->create() ?>
<?php echo $this->Form->control('email'); ?>
<?= $this->Form->button(__('Submit'), ['class'=>'btn btn-default text-right right']) ?>
<?= $this->Form->end() ?>
</div>
You will use elements, see CookBook
Put your code in here: src/Template/Element/Emails/add.ctp, you use your element like this echo $this->element('Emails/add');
Also add an action attribute to your form, specifying the controller
and action that will process the data.
<?= $this->Form->create(null, [
'url' => ['controller' => 'Articles', 'action' => 'publish']
]); ?>
<?= $this->Form->control('email'); ?>
<?= $this->Form->button(__('Submit'), [
'class' => 'btn btn-default text-right right'
]); ?>
<?= $this->Form->end(); ?>
See Setting a URL for the Form
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'])?>
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 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.