MageWork

Back to home

Forms

<?php
    
/** @var Core_Model_Form $form */
$form = App::getSingleton('form', Core_Model::TYPE);

if ($form->isPost()) {
    $form->setFormSpamField('subject');
    $form->setFormRequiredFields(
        [
            'firstname' => 'Firstname', // field name => field label
            'customer' => [
                'firstname' => 'Customer Firstname'
            ]
        ]
    );
    $form->validate();
    
    if (!$form->getError()) {
        /* ... */
        $form->getFirstname(); // name="firstname"
        $form->getCustomerFirstname(); // name="customer[firstname]"
        
        /** @var Core_Mail $message */
        $message = App::getSingleton('contact', Core_Mail::TYPE); // Magework_Mail_Contact
        
        $form->setMailSubject('New contact message!');
        $form->setMailMessage($message->render());
        $form->setMailSendTo('contact@example.com');
        $form->sendMail();
    } else {
        $form->getErrorFieldName();
        $form->getErrorFieldLabel();
    }
}