Difference between #153 and #154 of
Yii v2 snippet guide

Revision #154 has been created by rackycz on Oct 13, 2019, 11:40:26 AM with the memo:

richtext
« previous (#153) next (#155) »

Changes

Title unchanged

Yii v2 snippet guide

Category unchanged

Tutorials

Yii version unchanged

2.0

Tags unchanged

tutorial,beginner,yii2

Content changed

[...]
```php
$model->scenario = "abc";
```

... but had no **rule** defined yet. I wanted to implement the rule later, but I didnt know that when you set a scenario to your model it **must** be used in method rules() or defined in method scenarios(). So take this into consideration. I expected that when the scenario has no rules it will just be skipped or deleted.

 
 
**Richtext / wysiwyg HTML editor - Summernote**
 
---
 
If you want to allow user to enter html-formatted text, you need to use some HTML wysiwyg editor, because ordinary TextArea can only work with plain text. It seems to me that [Summernote](https://summernote.org/getting-started/#simple-example) is the simplest addon available:
 
 
```javascript
 
// Add following code to file layouts/main.php .. 
 
// But make sure jquery is already loaded !! 
 
// Read above in chapter "Adding a google-like calendar"
 
 
<!-- include summernote css/js -->
 
<link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote.css" rel="stylesheet">
 
<script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote.js"></script>
 
 
// And then in any view you can use this code:
 
 
<script>
 
$(document).ready(function() {
 
  $('#summernote1').summernote();
 
  $('#summernote2').summernote();
 
});
 
</script>
 
<div id="summernote1">Hello Summernote</div>
 
 
<form method="post">
 
  <textarea id="summernote2" name="editordata"></textarea>
 
</form>
 
 
```
 
 
in this tutorial I showed how to save Contacts inqueries into database. If you want to use this richtech editor in this section, open view contast/\_form.php and just add this JS code:
 
 
```javascript
 
<script>
 
$(document).ready(function() {
 
  $('#contact-body').summernote();
 
});
 
</script>
 
```
 
 
It will be saved to DB as HTML code. But this might be also a source of problems, because user can inject some dangerous HTML code. So keep this in mind.
 
 
Now you will also have to modify view contact/view.php like this in order to see nice formatted text:
 
 
```php
 
DetailView::widget([
 
  'model' => $model,
 
  'attributes' => [
 
    // ...
 
    'body:html', // html
 
  ],
 
])
 
```
 
 
7 0
4 followers
Viewed: 254 387 times
Version: 2.0
Category: Tutorials
Written by: rackycz
Last updated by: rackycz
Created on: Sep 19, 2019
Last updated: 5 months ago
Update Article

Revisions

View all history