A simple site to test Drupal 8 and share my CakePHP 3 tutorials.
CakePHP's validation is very robust, and while this is immensely helpful, getting to grips with all its possibilities can be daunting.
One handy feature is Conditional Validation. When adding a record, what if we want to ensure a field is not empty only when a matching field is also not empty? For example a schedule for work should have a start time and an end time, but if you wanted to schedule the day off it would have neither.
By default CakePHP uses the unique Id of each record to view each record. For example
http://hostname/application/model/view/6. This makes things simple and convenient, except when you want to allow an anonymous user to add and view their record, but no one else. Another user could simply iterate the id field and view all records in the table.
Obscurity is not real security, but can be enough in some instances, and is slightly better than no security at all.
There may come a case, as in the previous tutorial (CakePHP 3 Tutorial 18: Autocomplete) that you want to display one thing in a form field, yet save a different value. In the previous case, you select a user's name from an autocomplete list, yet the user's id is the value you want to save in the database.
In the previous two tutorials we discussed adding associated records. But what if you want to select a record from an associated table where there are thousands of records? Autocomplete can help with this issue, but it requires a combination of parts to work.
In the previous tutorial (CakePHP 3 Tutorial 16: Table Relationships) we set up associating tables and displaying associated data, but there's no point to displaying associated data if you can't add it first.
There are multiple ways to save associated data. We'll start with the easiest way first.
When working with CakePHP it is important to remember "Convention over Configuration." If you start from the bottom, or back end, the database, and work your way forward correctly, CakePHP will do much of legwork for you, saving tons of redundant and repetitive work. If you don't, there are almost always ways to make things work the hard way.
An example of this is the relationships between tables. If you name your database tables and fields following CakePHP convention, it becomes trivial to set up relationships between them.
In CakePHP 2 there was a handy feature called "neighbors" where you could simply request the neighboring records (previous and next) for easy linking. This is not present in CakePHP 3, probably due to the limited use of such a feature, however, with custom finders you can replicate the functionality.
In your model (e.g.
src/Models/Table/UsersTable.php) add the following custom finder:
There may come a time when you have a form that has optional requirements. By that I mean that depending on a previous selection, future selections may become required, or not.
A simple method I've found to do this is with jQuery on the front end and no validation requirements on the back end. An example follows: