How to customize Flash Message with inline css in cakephp3 - cakephp-3.0

In my Controller :
$this->Flash->success(__(' Your Data has been Saved Successfully. Sheets Name in this XLS file : <span style="color:#FF9900"> '.$SheetName.'</span>'));
In my success.ctp located in Element\Falsh :
<div class="alert alert-success fade in" onclick="this.classList.add('hidden')"> × <strong>Success!</strong> <?= h($message) ?> </div>
The Message is display like :
Now the question is how to use inline css in the flash message.

I think the soloution for your problem would be to use another layout for this type of message.
$this->Flash->set($message, $options[])
You can specify to use a specific element in the options. Something like this:
create a new confirmation.ctp inyour project:
<div class="[...]">
<?= h($message) ?>
<span style="color:#FF9900">
<?= h($params['sheetName']) ?>
</span>
</div>
now when you want to show the new confirmation use this code:
$this->Flash->set(__('The Message:'), [
'element' => 'confirmation,
'params' => [
'sheetName' => $SheetName
]
]);
This will display your custom flash element.

Related

How to use 'onchange' in Yii2 MaskedInput widget

I have an Yii2 app with advanced template. There is 2 fields on my form. 1st: MaskedInput and 2nd textInput with readonly attribute.
So, I wanted to fill 2nd textInput automatically right after I have filled MaskedInput. For this I tried to use onchange. But I got following error:
Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: yii\widgets\MaskedInput::onchange
Here is my code:
<?php $form = ActiveForm::begin(); ?>
<div class="row">
<div class="col-xs-3">
<?= Html::label("Ser num")?>
<?= MaskedInput::widget(['name'=>'serNum',
'mask'=>'AA 9999999',
'onchange'=>'
$.post("index.php?r=act/actid&serNum='.'"+$(this).val(),function(data){
$("select#ser-sernum").html(data);
});
'
])?>
</div>
<div class="col-xs-3">
<?= $form->field($model, 'sernum')->textInput(['readonly'=>true]) ?>
</div>
<div class="col-xs-6">
<b id="actstatus"></b>
</div>
</div>
Try with options property:
<?= yii\widgets\MaskedInput::widget(['name'=>'serNum',
'mask'=>'AA 9999999',
'options' => [
'onchange'=>'
$.post("index.php?r=act/actid&serNum='.'"+$(this).val(),function(data){
$("select#ser-sernum").html(data);
});'
]
])?>

Customize login cakephp3

I have a form like this:
email
[input]
password
[input]
Login-Button
I want to eliminate the "email" and "password" text and put them inside the inputs like placeholder and also change the "login" text to "LOGIN".
This is my login ctp.
<fieldset style="background:#F44336">
<?= $this->Form->create() ?>
<?= $this->Form->input('email') ?>
<?= $this->Form->input('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>
<div class="forgot">
<p>Forgot Password?</p>
</div>
Your question is confusing and vague.
But I believe you are asking you dont want the value or words "Email" and "Password" to be next to the input but rather inside the input textbox?
If so, you can add HTML code inside the HTML form
<input type="email" name="email_form" placeholder="Email">
Keep in mind that is HTML 5.
In HTML 4, the traditional way would to be adding an attribute to the HTML form to email or password as value="Email..."
Above code is correct but you should use helpers.
So a better way to achieve the same task with Html helpers is:
<?= $this->Form->input('email', array('label' => false)); ?>
<?= $this->Form->input('password', array('label' => false)); ?>
It's a suggestion to make your code better which we all should focus on.

Yii2 Add a Form field not in model

As we know,
<?= $form->field($model, 'name_field')->textInput() ?>
Adds a text field connected to 'name_field' in the model/table.
I want to add a field NOT in the model/table, and then run some JS when it loses focus to calculate the other fields.
How firstly do you add a free text field not connected to the model ?
Second, does anyone have any examples of adding JS/Jquery to the _form.php ?
The Html class contains the functions for generation of fields. In fact, your code above ends up calling Html::textInput(). To add a field
<?= Html::textInput("name", $value) ?>
To add javascript to a view just use registerJs():
$this->registerJs("alert('true');");
You can have the field rendered the same way as the ActiveField, with a label and classes. For example, let’s add a Cc field to a Mail form.
First display the To: field (in the model):
<?= $form->field($model, 'to')->textInput() ?>
Let’s add the Cc field (not in the model):
<?= Html::beginTag('div', ['class' => 'form-group field-mail-cc']) ?>
<?= Html::label('Cc:', 'mail-cc', ['class' => 'control-label']) ?>
<?= Html::textInput('Mail[cc]', '', ['id' => 'mail-cc', 'class' => 'form-control']) ?>
<?= Html::endTag('div') ?>
The class and id names mail-cc and field-mail-cc follow the ActiveForm naming pattern. The input name Mail[cc] adds your field to the ActiveForm group, so you can easily retrieve it with the usual
$form = Yii::$app->request->post('Mail');

Cakephp : inserting Image in view with class and alt tag

I tried to render something like this using html helper:
<img src="url/to/image.png" alt="alt-tag" />
I write this in my .ctp file:
<?php
echo $this->Html->link($this->Html->image("image.png", array("alt" => "alt-tag")),"#", array('class' => 'some-class'));
?>
But in browser, image was not displaying but simple plain text :
<img src="/url/to/image.png" alt="alt-tag" />
I did inspect element, where everything look as what I wanted to render. I tried to edit it live, so I selected edit as html option in inspect element. I surprised to see following code:
<img src="/url/to/image.png" alt="alt-tag" />
Please not that insted of < and > it was < and >.
I couldn't figure out what is the problem. (I am new to cakephp).
Use escape attribute to False
ex:
echo $this->Html->link($this->Html->image("image.png", array("alt" => "alt-tag")),"#", array('class' => 'some-class', 'escape' => false));

CakePHP Span Tag within Anchor Tag

I'm trying to have CakePHP output a link that looks like this:
<a href="/foo/bar" class="some other classes">
<span class="icon new"></span>FooBar</a>
So I use the following code in my view
<?php
echo $this->Html->link(
$this->Html->tag('span', null, array('class' => 'icon new')) . "FooBar",
array('controller' => 'foo', 'action' => 'bar'),
array('class' => 'some other classes', 'escape' => false)
);
?>
However CakePHP outputs the following:
<a href="/foo/bar" class="some other classes">
<span class="icon new">FooBar</span></a>
Which breaks my design. How can I get CakePHP to append "FooBar" after the <span> tags?
EDIT: Its also worth mentioning that I know a <span> tags shouldn't be within an anchor tag usually, but in the case its a must.
You need to use an empty string in stead of null as the text for the span, then your code will work as expected.
Looking at the source code of the HtmlHelper, null is treated as a 'special' value, causing only the opening tag of the span to be created. You can see this in this line:
https://github.com/cakephp/cakephp/blob/2.3.2/lib/Cake/View/Helper/HtmlHelper.php#L906
Change your code to this and it should work;
echo $this->Html->link(
$this->Html->tag('span', '', array('class' => 'icon new')) . "FooBar",
array('controller' => 'foo', 'action' => 'bar'),
array('class' => 'some other classes', 'escape' => false)
);
Additional explanation of the closing </span>
A bit of explanation, for those who wonder:
The closing </span> in your example is actually not present in the output generated by CakePHP, but automatically 'added' by your browser. If you view the source of the HTML in your browser, you'll see that this is what is actually in your HTML:
<a href="/foo/bar" class="some other classes">
<span class="icon new">FooBar</a>
As you can see, no closing 'span'
Because the <span> is not closed, the browser will try to correct this error and automatically assumes that you 'forgot' to close it. Therefor it will add a closing </span> before the next tag it finds (in this case the closing </a>).
The 'inspector' in your browser will always show the HTML that the browser uses to render the output. This includes automatic corrections made by the browser and dynamically generated elements (e.g. Elements added via JavaScript).
To check the output of your PHP scripts, always view the source, not the inspector
In this situation, I've avoided the CakePHP helpers entirely because the markup becomes really messy and cannot take advantage of autocomplete or validation within your IDE.
I usually do something like this:
<span class="icon-new"></span>Foobar
This looks a little overkill for me. Just do this:
echo $this->Html->link(
"<span class="icon new"></span> FooBar",
array('controller' => 'foo', 'action' => 'bar'),
array('class' => 'some other classes', 'escape' => false)
);
I've been using CakePHP for 4 years and I don't see the benefit of using tag in this instance.
Would you be able to use regular PHP in this case?
I am thinking you could do it like this:
<?PHP
echo('<span class="' . 'icon new' . '"></span>' . 'FooBar' . '')
?>