<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ivan Bernatović</title>
    <description>The latest articles on DEV Community by Ivan Bernatović (@ivanbernatovic).</description>
    <link>https://dev.to/ivanbernatovic</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F38671%2Fbf135319-93b7-4c10-bffe-b4e6693c064f.jpeg</url>
      <title>DEV Community: Ivan Bernatović</title>
      <link>https://dev.to/ivanbernatovic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ivanbernatovic"/>
    <language>en</language>
    <item>
      <title>The best way to set up LEMP stack for local development – 2021 edition</title>
      <dc:creator>Ivan Bernatović</dc:creator>
      <pubDate>Sat, 06 Feb 2021 14:12:27 +0000</pubDate>
      <link>https://dev.to/ivanbernatovic/the-best-way-to-set-up-lemp-stack-for-local-development-2021-edition-22lc</link>
      <guid>https://dev.to/ivanbernatovic/the-best-way-to-set-up-lemp-stack-for-local-development-2021-edition-22lc</guid>
      <description>&lt;p&gt;&lt;strong&gt;The original post was published on my dev blog and can be read &lt;a href="https://www.techyfingers.com/lemp-stack-for-local-development-2021-edition/"&gt;here&lt;/a&gt;. Please consider visiting my blog.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Linux is THE platform for developing web applications. Developing apps on the platform which is running on a production environment is the best choice for learning how stuff works. This guide will show you how to install and configure software that’s commonly referred to as LEMP stack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nginx&lt;/strong&gt; – HTTP server software&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PHP 8&lt;/strong&gt; – more precisely PHP FPM&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MariaDB&lt;/strong&gt; – a fork of MySQL&lt;/li&gt;
&lt;li&gt;Install &lt;strong&gt;Composer&lt;/strong&gt; dependency manager&lt;/li&gt;
&lt;li&gt;Configure your PHP and Nginx so you can host your projects in your home directory without the usual issues with file and folder permissions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The major requirement is to have Ubuntu 20.04 or higher installed or any other Ubuntu-based distribution which uses apt as the package manager. First, we’re going to install everything, then we will deal with configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Nginx – the E in LEMP
&lt;/h2&gt;

&lt;p&gt;The first thing we’re going to install is HTTP server called &lt;strong&gt;Nginx&lt;/strong&gt;. It is a popular server software with easy to use configuration syntax. We need to update our package repositories and then install Nginx. Open terminal and type the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check if Nginx is installed by typing nginx -v  in terminal. You should see the version in the output. Also, when you type localhost in your web browser address bar you should see Welcome to nginx! message which confirms our Nginx server is working well.&lt;/p&gt;

&lt;p&gt;There are a few important things to know about Nginx. The main configuration file is located &lt;code&gt;/etc/nginx/nginx.conf&lt;/code&gt;. Another important part is setting up server blocks (virtual hosts in Apache world). All of the available server blocks are located in &lt;code&gt;/etc/nginx/sites-available&lt;/code&gt; directory. To activate available server block configuration we need to create a symbolic link in &lt;code&gt;/etc/nginx/sites-enabled&lt;/code&gt;. We will get to configuring server blocks in a minute.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing MariaDB – the M in LEMP
&lt;/h2&gt;

&lt;p&gt;Now we need to install a database management system (DBMS). We choose MariaDB, which is modern relational DBMS and arguably better version of MySQL (of which it is a fork). Type following command in terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;mariadb-server mariadb-client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check if MariaDB is installed by running &lt;code&gt;mysql --version&lt;/code&gt;. Now, to configure the root user you should run &lt;code&gt;mysql_secure_installation&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing PHP 8 – the P in LEMP
&lt;/h2&gt;

&lt;p&gt;Next thing to install is PHP. Since we’re using a newer version of Ubuntu (20.04 or above) default version of PHP in package repositories is PHP 7.4. In this tutorial, we’re going to install PHP 8 instead because we want to be on the latest version. There are a few options but the easiest one is to install PHP from a well supported PPA such as &lt;a href="https://launchpad.net/~ondrej/+archive/ubuntu/php/"&gt;ondrej/php&lt;/a&gt;. To add this PPA to your sources, run this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;add-apt-repository ppa:ondrej/php
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can install PHP 8 and a bunch of common extensions that you’ll most likely need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;php8.0 php8.0-fpm php8.0-zip php8.0-xml php8.0-mbstring php8.0-curl php8.0-gd php8.0-mysql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run &lt;code&gt;php -v&lt;/code&gt; in the terminal and you should see a similar output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;PHP 8.0.1 &lt;span class="o"&gt;(&lt;/span&gt;cli&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;built: Jan 13 2021 08:23:31&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt; NTS &lt;span class="o"&gt;)&lt;/span&gt;
Copyright &lt;span class="o"&gt;(&lt;/span&gt;c&lt;span class="o"&gt;)&lt;/span&gt; The PHP Group
Zend Engine v4.0.1, Copyright &lt;span class="o"&gt;(&lt;/span&gt;c&lt;span class="o"&gt;)&lt;/span&gt; Zend Technologies
    with Zend OPcache v8.0.1, Copyright &lt;span class="o"&gt;(&lt;/span&gt;c&lt;span class="o"&gt;)&lt;/span&gt;, by Zend Technologies
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Configuring PHP for development needs
&lt;/h2&gt;

&lt;p&gt;To avoid various file permissions issues, I want to serve my sites from my home directory. I prefer &lt;code&gt;/home/ivan/projects&lt;/code&gt; as my directory of all web projects. I want to avoid dealing with permissions issues.&lt;/p&gt;

&lt;p&gt;We need to create a dedicated PHP FPM configuration file. First, let’s copy the default config file intended for www-data user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo cp&lt;/span&gt; /etc/php/8.0/fpm/pool.d/www.conf /etc/php/8.0/fpm/pool.d/your_user.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to make a few changes so open the newly copied file by running &lt;code&gt;sudo nano /etc/php/8.0/fpm/pool.d/your_user.conf&lt;/code&gt;. We need to make the following changes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Change the &lt;code&gt;[www]&lt;/code&gt; to &lt;code&gt;[your_user]&lt;/code&gt; to change the pool name&lt;/li&gt;
&lt;li&gt;Change the line &lt;code&gt;user = www-data&lt;/code&gt; to &lt;code&gt;user = your_user&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Change the line &lt;code&gt;group = www-data&lt;/code&gt; to &lt;code&gt;group = your_user&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Change the &lt;code&gt;listen = /run/php/php8.0-fpm.sock&lt;/code&gt; to &lt;code&gt;listen = /run/php/php8.0-your_user-fpm.sock&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now run &lt;code&gt;sudo systemctl restart php8.0-fpm.service&lt;/code&gt; to load the new config and restart the main FPM process.&lt;/p&gt;

&lt;p&gt;Now let’s install the most popular dependency manager for PHP – Composer. To install the latest version of Composer just run &lt;code&gt;curl-sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer&lt;/code&gt;. That command will install Composer globally on your OS for all users. Run &lt;code&gt;composer -v&lt;/code&gt; to verify the installation. You should also add your user to the www-data group by running &lt;code&gt;sudo adduser your_user www-data&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Server block configuration example
&lt;/h2&gt;

&lt;p&gt;Let’s say you started a new PHP project your project directory, so the path to the project could look like this &lt;code&gt;/home/your_user/projects/my-awesome-app&lt;/code&gt; and you want to serve it from a custom domain such as &lt;a href="http://my-awesome-app.test"&gt;http://my-awesome-app.test&lt;/a&gt;, you’ll need to create the following server block. Run &lt;code&gt;sudo nano /etc/nginx/sites-available/my-awesome-app.conf&lt;/code&gt; and paste this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="k"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kn"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;root&lt;/span&gt; &lt;span class="n"&gt;/home/your_user/projects/my-awesome-app/public&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;index&lt;/span&gt; &lt;span class="s"&gt;index.php&lt;/span&gt; &lt;span class="s"&gt;index.html&lt;/span&gt; &lt;span class="s"&gt;index.htm&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;server_name&lt;/span&gt; &lt;span class="s"&gt;my-awesome-app.test&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;try_files&lt;/span&gt; &lt;span class="nv"&gt;$uri&lt;/span&gt; &lt;span class="nv"&gt;$uri&lt;/span&gt;&lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="n"&gt;/index.php&lt;/span&gt;&lt;span class="nv"&gt;$is_args$args&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kn"&gt;location&lt;/span&gt; &lt;span class="p"&gt;~&lt;/span&gt; &lt;span class="sr"&gt;\.php$&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;try_files&lt;/span&gt; &lt;span class="nv"&gt;$uri&lt;/span&gt; &lt;span class="n"&gt;/index.php&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;fastcgi_pass&lt;/span&gt; &lt;span class="s"&gt;unix:/var/run/php/php8.0-your_user-fpm.sock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;fastcgi_index&lt;/span&gt; &lt;span class="s"&gt;index.php&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;fastcgi_param&lt;/span&gt; &lt;span class="s"&gt;SCRIPT_FILENAME&lt;/span&gt; &lt;span class="nv"&gt;$document_root$fastcgi_script_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;include&lt;/span&gt; &lt;span class="s"&gt;fastcgi_params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don’t forget to replace your_user with the name of your user profile. Save it and close. Now you have to run &lt;code&gt;sudo ln -s /etc/nginx/sites-available/my-awesome-app.conf /etc/nginx/sites-enabled&lt;/code&gt; and &lt;code&gt;sudo systemctl reload nginx.service&lt;/code&gt; to enable the new configuration file and reload Nginx.&lt;/p&gt;

&lt;p&gt;The final thing is to edit &lt;code&gt;/etc/hosts&lt;/code&gt; file to let your operating system know you’re hosting my-awesome-app.test on your machine. Run &lt;code&gt;sudo nano /etc/hosts&lt;/code&gt; and add the following line to the bottom of the file: &lt;code&gt;127.0.0.1 my-awesome-app.test&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations, you’ve made it to the end. You now have a great development environment for developing LEMP-based web applications. Good luck with developing awesome apps!&lt;/p&gt;

</description>
      <category>php</category>
      <category>linux</category>
      <category>laravel</category>
      <category>wordpress</category>
    </item>
    <item>
      <title>How to easily make reusable forms in Vue</title>
      <dc:creator>Ivan Bernatović</dc:creator>
      <pubDate>Thu, 04 Feb 2021 20:30:28 +0000</pubDate>
      <link>https://dev.to/ivanbernatovic/how-to-easily-make-reusable-forms-4b0o</link>
      <guid>https://dev.to/ivanbernatovic/how-to-easily-make-reusable-forms-4b0o</guid>
      <description>&lt;p&gt;&lt;strong&gt;The original post was published on my web dev blog and can be read &lt;a href="https://www.techyfingers.com/how-to-easily-make-reusable-forms-in-vue/"&gt;here&lt;/a&gt;. The only difference between the original post and this one is that on my blog you also have a working Codesandbox environment.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Web applications contain a lot of forms nowadays. Many times, we have the same form layout when creating or editing something (it can be anything: user, project, todo item, product etc.). Usually, creating and editing a resource is implemented on 2 separate pages. To keep the code DRY and avoid code repetition we should try to reuse the same form component for both scenarios. Luckily, if you use Vue you can implement reusable form components easily. Let's get started then.&lt;/p&gt;

&lt;p&gt;Let's create a reusable form component&lt;br&gt;
We will create a simple form for creating or editing a user. For simplicity, we will only have 2 fields: email and name. Without further ado, here's how the reusable form will look like in the end.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;submit.prevent=&lt;/span&gt;&lt;span class="s"&gt;"$emit('on-submit', form)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"form-group"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Email&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"form.email"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
​
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"form-group"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Name&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"form.name"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
​
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
​
&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;form&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;UserForm component has an optional user prop and contains 2 inputs. Each input has a binding to a data entry - form.email and form.name. On form submission, we will emit the custom on-submit event with the form object as an event payload. The user prop is an object and if passed it will be used to get values for the email and name inputs. If the prop is not passed, inputs will default to empty strings.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use reusable forms
&lt;/h2&gt;

&lt;p&gt;Let's create a root Vue App component that will be mounted in the app and also use the UserForm component. This component will import the UserForm and render it 2 times: first to show the create user scenario, and the second time to show the update user scenario.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;Create user form&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;UserForm&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;on-submit=&lt;/span&gt;&lt;span class="s"&gt;"createUser"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
​
    &lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;Edit User form&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;UserForm&lt;/span&gt; &lt;span class="na"&gt;:user=&lt;/span&gt;&lt;span class="s"&gt;"user"&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;on-submit=&lt;/span&gt;&lt;span class="s"&gt;"updateUser"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
​
&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;UserForm&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/UserForm&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;App&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;components&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UserForm&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;john@example.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userForm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;creating&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userForm&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
      &lt;span class="c1"&gt;// call an API to create a new user&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nx"&gt;updateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userForm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;updating&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userForm&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
      &lt;span class="c1"&gt;// call an API to update the existing user&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;​&lt;br&gt;
The App component has a user object (containing email and name) as part of its data. We will use this user to showcase the update user scenario. App also has 2 methods which are handlers for the custom on-submit event for the create and update form. The handler has 1 parameter and that is the userForm object which contains email and name. The first time we use UserForm component we don't pass the user prop, only the handler for the on-submit event. This is the create mode of the form. In the second example, we pass the user object as a prop which means we will use the form component in edit (or update) mode. This time, the form will be pre-filled with the values for the email and name inputs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reusable forms improve maintainability and code reuse
&lt;/h2&gt;

&lt;p&gt;The benefits of reusing the same component for forms are better code maintainability and code reuse. You simply have to write less code when implementing or refactoring forms. In bigger forms, it will probably slightly increase the complexity, but the benefits are even greater then.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
    </item>
  </channel>
</rss>
