Submissions

Introduction

The Submissions entry type is used to store form data from Form submissions generated by the onFormSubmit() function on all layout templates. It's structure is defined in the submissions.yaml file

All form fields are disabled by default and are only used to display data from the submitted form. It is recommended that you disable entry creation for your client.

onFormSubmit() snippet

function onFormSubmit() {

    $form_input = input();
    $form_id = $form_input["form_id"];
    $form_success_message = $form_input["success_message"];
    $form = EntryRecord::inSection('Content\Forms')->find($form_id);
    $submission = EntryRecord::inSection('Content\Forms\Submissions');
    $settings = GlobalRecord::findForGlobal('Content\Settings');
    $form_validation_rules = [];
    $form_validation_messages = [];
    $form_input_formatted = '';
    $form_name = '(no data)';
    
    // Parse form validation data
    foreach ($form->fields as $field) {
        
        $field_validation_rules = [];
        $field_validation_messages = [];

        if($field->validation == true) {

            foreach ($field->validation_rules as $validation_item) {
                if($validation_item->attributes['type'] == "phone") {
                    $field_validation_rules[] = "regex:/^([0-9\s\-\+\(\)]*)$/";
                    $field_validation_rules[] = "min:9";
                    $field_validation_messages[$field->slug . '.' . "regex"] = $validation_item->attributes['error_message'];
                    $field_validation_messages[$field->slug . '.' . "min"] = $validation_item->attributes['error_message'];
                } else {
                    $field_validation_rules[] = $validation_item->attributes['type'];
                    $field_validation_messages[$field->slug . '.' . $validation_item->attributes['type']] = $validation_item->attributes['error_message'];
                }
                
            }
            
            $form_validation_rules[] = [$field->slug => $field_validation_rules];
            $form_validation_messages[] = $field_validation_messages;
            if($form->enable_recaptcha) {
                $form_validation_rules[] = [
                    'g-recaptcha-response' => 'required|recaptcha'
                ];
                $form_validation_messages[] = [
                    'g-recaptcha-response.required' => 'Please ensure you are not a robot.'
                ];
            }
        }

    }

    // Format Rules and messages
    $form_validation_rules = array_merge(...$form_validation_rules);
    $form_validation_messages = array_merge(...$form_validation_messages);

    $form_unwanted_fields = [
        "form_id",
        "g-recaptcha-response",
        "success_message",
        "submitted",
        "_preview_token"
    ];

    // Validate Fields
    $validation = Validator::make(request()->all(), $form_validation_rules, $form_validation_messages);

    if($validation->fails()) {
        throw new ValidationException($validation);
    }
    
    foreach ($form_input as $key => $value) {
        
        if(!in_array($key, $form_unwanted_fields)) {
            if($key == array_key_first($form_input)){
                $form_name = (!empty($value)) ? $value : $form_name;
            }
            $key = ucwords(str_replace("-", ' ', $key));
            $form_input_formatted .= $key . ": " . $value  . "\n";
        }

    }

    $submission->title = $form_name;
    $submission->form  = $form_input['form_id'];
    $submission->data  = $form_input_formatted;
    
    $submission->save();

    // Send Email
    $mail_data = [
        'form_name' => $form->title,
        'form_data' => $form_input_formatted,
    ];

    $mail_recipients = [];
    

    if(!$form->recipients->isEmpty()){
        foreach ($form->recipients as $recipient) {
            $mail_recipients[$recipient->email] = $recipient->name;
        }
    } else {
        if((!empty($settings->notification_email) && (!empty($settings->notification_email_recipient_name)))) {
            $mail_recipients = [
                $settings->notification_email => $settings->notification_email_recipient_name
            ];
        }
        
    }

    if(($form->enable_notifications) && (!empty($mail_recipients))) {

        Mail::send('artistro08.tailorstartercompanion::mail.form_submission', $mail_data, function($message) use ($mail_recipients, $form) {
            foreach ($mail_recipients as $email => $name) {
                $message->to($email, $name);
                $message->subject('New Form Submission from ' . $form->title);
            }
        });

    }

    Flash::success($form_success_message);

    $this['submitted'] = $form_input['submitted'];

}

Last updated