Confirm form submission without loops using jQuery

Introduction This post falls in the category “Short tricks that I would teach to my younger self”. Let’s assume that you want to display a confirmation dialog before submitting an important form. You can leverage the Window.confirm() method, which displays a modal dialog with two buttons, Ok and cancel. Window.confirm() returns true if the user clicks on the Ok button. <form onsubmit="return confirm('Do you really want to submit the form?');"> You can also implement this using jQuery: $(form).on('submit', function() { if(confirm('Do you really want to submit the form?')) { return true; } return false; }); Or more succint: $(form).on('submit', function() { return confirm('Do you really want to submit the form?'); }); This simply works out of the box because the confirm() method blocks the event loop, therefore the form is not submitted until the user clicks one of the two choices. Now, let’s say that we want to use a...

Read More
Check for foreign key existence in migrations

Laravel provides a couple of handy features to check for the existence of a table or column using the hasTable and hasColumn methods: <?php if (Schema::hasTable('users')) { // } if (Schema::hasColumn('users', 'email')) { // } However, sometimes you need to check the existence of foreign keys or indexes on tables, for example when you need to rename columns, in order to avoid annoying errors. Unfortunately Laravel doesn’t provide such methods out of the box, but you can easily leverage the doctrine/dbal library in order to get the current state of columns. Notice: You will need to add the doctrine/dbal dependency to your composer.json file and run the composer update command in your terminal to install the library. After that, you can easily get the Doctrine SchemaManager instance in your migration files, which provides additional abstraction methods on top of SQL. For example, you can easily build a function that returns...

Read More
Integrate the Facebook SDK in a React Native application

Introduction React Native FBSDK is a wrapper around the iOS and Android Facebook SDK which allows for Facebook integration in React Native apps. It allows to access to native components, from login to sharing, entirely through documented JavaScript modules so you don’t have to call a single native function directly. I thought that setting it up would be quite simple but unfortunately I was wrong! It is a little bit complicated to get it running and you have to figure out by yourself the mess that is Facebook’s documentation. In this post I will try to provide the correct setup for Facebook SDK on a React Native app and create a login page that uses FBSDKLogin API. Note: For the purpose of this project, instructions below are specifically for Android. I also assume that your dev environment is correctly setup with the Android SDK and a functional emulator. First steps...

Read More
Array Validation in Laravel 4

Array Validation in Laravel 4 At work we are still using Laravel 4 for a few projects. From time to time we need to validate an array of form fields using the Laravel Validator, however before Laravel 5.2 this was not a trivial task. Let’s break down the problem a little bit. Suppose that you want to validate a form that uses dynamic fields, added at run time by the user. Take the example of a product in an ecommerce website. We need to insert some information on a product, let’s say the name, the price and a list of barcodes (EAN, ISBN and so on). Each product can have zero or more bar codes, added dynamically to the form using JavaScript. We want to be able to validate all of them. Laravel 4 doesn’t have Form requests therefore we need to create a validator using the Validator facade. Creating...

Read More
Basic ProcessWire website workflow - Part Four

Introduction Following Part 1, Part 2 and Part 3. In this post we will make the final touches and personalizations to our website. Let’s see what are going to do: Implement a basic Sitemap Implement ProcessWire Roles and Permissions Tweak CKEditor a little bit Implement a little countermeasure to avoid bots to read emails Implement the Facebook plugin Add the cookie notification Also this post concludes this series on ProcessWire, but don’t worry, I’m planning to write again on this subject. But now, let’s dive in! Sitemap The default site profile of ProcessWire offers a basic Sitemap template right out of the box, however it is not tailored for multilingual sites, because you get a different sitemap for each language, for example: site.com/it/sitemap.xml site.com/de/sitemap.xml site.com/de/sitemap.xml We need to “glue” multilanguage sitemaps together. This can be achieved with a handy snippet available on the processwire-recipes.com website, a directory which ”[…] aims...

Read More
Easily impersonate any user in a Laravel Application

Introduction Impersonate is quite a handy feature in some applications. It allows you to choose from a list of users and to impersonate them, in order to see the application from their point of view, without having to log out and log in again. For example, as an administrator you want to recreate a bug encountered by one of your users, without having them to share their password with you. It’s a functionality that it’s really powerful, but at the same time it’s easy to implement in Laravel. You just need to make sure that a normal user cannot impersonate an administrator. Meet the impersonate Middleware “HTTP middleware provide a convenient mechanism for filtering HTTP requests entering your application.” Middleware are additional layers that enclose the application logic, allowing modifications on the requests and responses of the application. Middleware. For example, Laravel uses a middleware that verifies if the user...

Read More