requiredAddFields
requiredAddFields(array $fields)
The requiredAddFields
method is a specialized version of the requiredFields
method, designed specifically for the
insert form. It checks whether the user has provided values for certain fields during the insert operation.
By using requiredAddFields
, you can specify fields that are mandatory only when adding new entries.
This method offers the following features:
- An asterisk is displayed next to the field name, indicating that it is required when adding a new entry.
- Client-side validation is performed, alerting the user to fill in the required field before the data is sent to the server.
- Server-side validation is also in place, serving as a fallback if the client-side validation is bypassed for any reason.
The usage is straightforward. Simply pass an array of the database field names that are required for the insert form. For instance:
$crud->requiredAddFields(['first_name', 'last_name'])
Here's a complete example:
$crud->setTable('customers');
$crud->setSubject('Customer', 'Customers');
$crud->columns(['customerName','phone','addressLine1','creditLimit']);
$crud->displayAs('customerName', 'Name');
$crud->displayAs('contactLastName', 'Last Name');
// A real case example where you need to have some fields required to add
// form but the user can't edit them later.
$crud->readOnlyEditFields(['customerName', 'contactLastName', 'contactFirstName']);
$crud->requiredAddFields(['customerName', 'contactLastName', 'contactFirstName']);
$output = $crud->render();
In this example, if you attempt to add a new customer without providing values for 'customerName', 'contactLastName', or 'contactFirstName', the form will prompt you to fill in these fields.