phpoctobercmsoctobercms-backendoctobercms-pluginsoctober-partial

Custom Contact Form in Partials and use in Static Pages plugin


I have used OctoberCMS, Static Pages plugin, through which I am creating Static Pages.

The thing is, I have created one contact form in Partial like below.

contactform_snippet.htm - Markup

enter image description here

contactform_snippet.htm - Code

enter image description here

And below is the Static Page which I have created and used contactform_snippet.htm which I just created.

enter image description here

And below is Preview how its looking like.

enter image description here

The thing is, Even if I click on "Submit" button, it is not doing anything.

I also changed the form code from data-request="{{ __SELF__ }}::onSendInquiry" to data-request="onSendInquiry"but then I am getting below error saying:

AJAX handler 'onSendInquiry' was not found.

The thing here is, similar thing I have created and copied in CMS Page instead of Static Page and all is working there with validations and email being sent.

So My question is how can make the same thing work in Static Pages here using Snippets. I know the can be achieved via creating Components but I have so many of forms and I want to implement something like this to make it work. Any thoughts what should I need to make this work here ?

Thanks


Solution

  • Ok Guys, Eventually I made it work by putting my doing this data-request="onSendInquiry" in my form and putting below code in my default.htm layout file.

    function onSendInquiry()
    {
        // Collect input
        $name = post('name');
        $email = post('email');
        $subject = post('subject');
        $msg = post('msg');
    
        // Form Validation
        $validator = Validator::make(
            [
                'name' => $name,
                'email' => $email,
                'subject' => $subject,
                'msg' => $msg,
            ],
            [
                'name' => 'required',
                'email' => 'required|email',
                'subject' => 'required',
                'msg' => 'required',
            ]
        );
    
        if ($validator->fails())
        {
    
            $messages = $validator->messages();
            throw new ApplicationException($messages->first());
            //Retrieving all error messages for all fields
            /*foreach ($messages->all() as $message) {
                throw new ApplicationException($message);
            }*/
            //throw new ApplicationException($messages);
        }
    
    
        // All is well -- Submit form
        $to = System\Models\MailSetting::get('sender_email');
        //$to = System\Models\MailSettings::get('sender_email');
        //$to = config('mail.from.address');
        //$to = 'mittul@technobrave.com';
        //die($to);
        $params = compact('name','email','subject','msg');
        Mail::sendTo($to, 'yourappname.website::mail.contactform', $params);
        return true;
    }]
    

    Was so close yet so far. Got it in the end. Thanks