Form callbacks

Form currently provides the following methods for receiving callback functions:

creating

Calling on a new page (non-committed action)

  1. $form->creating(function (Form $form) {
  2. if (...) { // verification logic
  3. $form->responseValidationMessages('title', 'Wrong title format');
  4. // If there are multiple error messages, the second parameter can be passed as an array.
  5. $form->responseValidationMessages('content', ['content format error', 'content cannot be empty']);
  6. }
  7. });

editing

Invoked on the edit page (non-commit operation)

  1. $form->editing(function (Form $form) {
  2. if (...) { // verification logic
  3. $form->responseValidationMessages('title', 'Wrong title format');
  4. // If there are multiple error messages, the second parameter can be passed as an array.
  5. $form->responseValidationMessages('content', ['content format error', 'content cannot be empty']);
  6. }
  7. });

submitted

Called before the form is submitted, this event allows you to modify or delete the data submitted by the user or abort the submitted operation.

  1. $form->submitted(function (Form $form) {
  2. // Get user submission parameters
  3. $title = $form->title;
  4. // The above is equivalent to
  5. $title = $form->input('title');
  6. // Delete user-submitted data
  7. $form->deleteInput('title');
  8. // Interruption follow-up logic
  9. return $form->response()->error('The server is down.~');
  10. });

saving

A callback before saving, in which you can modify or delete the data submitted by the user or interrupt the commit operation.

  1. $form->saving(function (Form $form) {
  2. // Determine if it is a new operation
  3. if ($form->isCreating()) {
  4. }
  5. // Delete user-submitted data
  6. $form->deleteInput('title');
  7. // Interruption follow-up logic
  8. return $form->response()->error('The server is down.~');
  9. });

saved

This event is shared by both the add and modify operations. The second parameter $result is used to determine if the data is stored in success.

{tip} The value of $result under the Add page is the self-added ID of the new record.

  1. $form->saved(function (Form $form, $result) {
  2. // Determine if it is a new operation
  3. if ($form->isCreating()) {
  4. // Self-added ID
  5. $newId = $result;
  6. // You can also get a self-added ID like this
  7. $newId = $form->getKey();
  8. if (! $newId) {
  9. return $form->error('Data save failed');
  10. }
  11. return;
  12. }
  13. // Modification operations
  14. });

{tip} $form->repository()->eloquent() to get the currently added or edited eloquent model.

  1. $form->saved(function (Form $form, $result) {
  2. // Get eloquent after the form is saved
  3. $form->repository()->eloquent()->update(['data' => 'new']);
  4. });

deleting

Callback before deletion

  1. $form->deleting(function (Form $form) {
  2. // Gets the data for the deleted rows, in this case a two-dimensional array.
  3. $data = $form->model()->toArray();
  4. });

deleted

The second parameter $result is used to determine if the data is deleted successively.

  1. $form->deleted(function (Form $form, $result) {
  2. // Gets the data for the deleted rows, in this case a two-dimensional array.
  3. $data = $form->model()->toArray();
  4. // With $result, you can determine if the data is deleted from the successor.
  5. if (! $result) {
  6. return $form->response()->error('Data deletion failure');
  7. }
  8. // Return to delete success reminder, where the jump parameter is invalid.
  9. return $form->response()->success('Delete success');
  10. });

uploading

Image, file upload events

{tip} The file upload is a separate api request and the redirect method is invalid in this event.

  1. use Dcat\Admin\Form;
  2. use Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Contracts\UploadField as UploadFieldInterface;
  4. use Symfony\Component\HttpFoundation\File\UploadedFile;
  5. $form->uploading(function (Form $form, UploadFieldInterface $field, UploadedFile $file) {
  6. // $file is the currently uploaded file.
  7. /* @var Field $field */
  8. // Get the name of the file upload field
  9. $column = $field->column();
  10. });

uploaded

Image/file upload completion event

{tip} The file upload is a separate api request and the redirect method is invalid in this event.

  1. use Dcat\Admin\Form;
  2. use Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Contracts\UploadField as UploadFieldInterface;
  4. use Symfony\Component\HttpFoundation\File\UploadedFile;
  5. $form->uploaded(function (Form $form, UploadFieldInterface $field, UploadedFile $file, $response) {
  6. // $file is the currently uploaded file.
  7. /* @var Field $field */
  8. // Get the name of the file upload field
  9. $column = $field->column();
  10. $response = (array) $response->getData();
  11. // File Uploadsuccess
  12. if ($response['status']) {
  13. // file access address
  14. $url = $response['url'];
  15. }
  16. });

Access to data in the model

  1. $form->saved(function (Form $form) {
  2. $id = $form->getKey();
  3. $username = $form->model()->username;
  4. // Get the final saved array
  5. $updates = $form->updates();
  6. });

Modify or delete user-submitted data

This feature is valid for the saving and submitted events.

  1. $form->select('author_id');
  2. $form->saving(function (Form $form) {
  3. // Modifying user-submitted data
  4. $form->author_id = 1;
  5. // Delete, ignore data submitted by users
  6. $form->deleteInput('author_id');
  7. });

Modify the data in the model

Modifying the data in the model needs to be used in conjunction with the hidden form. For example:

  1. $form->hidden('author_id');
  2. $form->saving(function (Form $form) {
  3. $form->author_id = 1;
  4. });

Form response

{tip} This method is not available in creating and editing events.

Refer to the Actions and Form Responses section of the documentation for detailed usage.

redirect (partial refresh / single page refresh)

  1. // Jump and prompt for success information
  2. $form->saved(function (Form $form) {
  3. return $form->response()->success('Save success')->redirect('auth/user');
  4. });
  5. // Jump and error messages
  6. $form->saved(function (Form $form) {
  7. return $form->response()->error('system error')->redirect('auth/user');
  8. });

Returns only error messages without skipping

  1. $form->saving(function (Form $form) {
  2. return $form->response()->error('system anomaly');
  3. });

Error messages can also be displayed in the form of exceptions thrown

  1. $form->submitted(function ($form) {
  2. throw new \Exception('Access prohibited');
  3. });

Form callbacks - 图1

Return field validation error message

The responseValidationMessages method makes it easy to return field validation error messages without using the Laravel validation feature.

General Use

  1. protected function form()
  2. {
  3. return Form::make(new Model(), function (Form $form) {
  4. if (...) { // verification logic
  5. $form->responseValidationMessages('title', 'Wrong title format');
  6. // If there are multiple error messages, the second parameter can be passed as an array.
  7. $form->responseValidationMessages('content', ['content format error', 'content cannot be empty']);
  8. }
  9. });
  10. }

Use in events

{tip} This method is only available for the submitted event.

  1. $form->submitted(function (Form $form) {
  2. // Receive form parameters
  3. $title = $form->title;
  4. if (...) { // verification logic
  5. $form->responseValidationMessages('title', 'Wrong title format');
  6. // If there are multiple error messages, the second parameter can be passed as an array.
  7. $form->responseValidationMessages('content', ['content format error', 'content cannot be empty']);
  8. }
  9. });