When building web applications using Laravel, it’s crucial to ensure that new users agree to your terms and conditions before they can create an account. This blog post will guide you through adding a checkbox for accepting the terms and displaying the actual text of those terms directly within the registration form.
Understanding the Requirements
The user registration process in Laravel is handled by the built-in register route and view (resources/views/auth/register.blade.php). You’ll need to modify this blade file to include the necessary components.
Adding the Terms and Conditions Checkbox
First, open your resources/views/auth/register.blade.php file. Look for the section containing the form fields for creating a new user account.
<form method="POST" action="{{ route('register') }}">
@csrf
<!-- Existing form fields here -->
</form>
You want to add a new checkbox field just before the final <!-- Existing form fields here --> comment. The checkbox should be linked to a hidden field that will store the user’s acceptance of the terms.
<form method="POST" action="{{ route('register') }}">
@csrf
<!-- Your existing form fields -->
<label for="agree_terms">
<input type="checkbox" id="agree_terms" name="agree_terms"> I agree to the <a href="#" target="_blank">Terms and Conditions</a>
</label>
<!-- Existing form fields -->
</form>
Note that we’ve linked the checkbox field to a hidden input named agree_terms. This field will be checked when the user ticks the box.
Displaying the Terms and Conditions Text
To display the actual text of your terms and conditions, you can use an HTML modal or overlay. There are many ways to implement this, but for simplicity, let’s use Bootstrap’s built-in modal component.
First, include the necessary bootstrap CSS and JavaScript files in your layouts/app.blade.php file:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6prtV5fsCxd/s9tfT9nzjoTGATD88uk1bvb6acJJII" crossorigin="anonymous"></script>
Next, add the modal HTML to your register.blade.php file:
<!-- Modal -->
<div class="modal fade" id="termsModal" tabindex="-1" aria-labelledby="termsModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="termsModalLabel">Terms and Conditions</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<!-- Insert your actual terms and conditions text here -->
</div>
</div>
</div>
</div>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#termsModal">View Terms</button>
Validating the Checkbox in Laravel
To ensure that the user must agree to the terms before they can register, you need to add validation rules to your app/Http/Requests/Auth/RegisterRequest.php file.
use Illuminate\Foundation\Http\FormRequest;
class RegisterRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'agree_terms' => ['required']
];
}
}
The agree_terms rule ensures the user must check the box before submitting the form.
Conclusion
Adding a terms and conditions checkbox to Laravel’s registration process is a straightforward task. By modifying the default register.blade.php view, you can display the actual text of your terms within a modal window and validate that users agree before they can create an account.
Remember, it’s crucial to have clear legal language in your terms and conditions to protect both your business and your users.
For more advanced customization of the registration process, consider checking out our guide on Customizing Laravel’s Authentication Views.
Happy coding!