Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CONTACT FORM
- class ContactForm extends Form {
- public function __construct( $controller, $name )
- {
- // Fields
- $nameField = TextField::create('Name', 'Name');
- $nameField->setAttribute('placeholder', 'Name');
- $emailField = TextField::create('Email', 'E-Mail Address');
- $emailField->setAttribute('placeholder', 'E-Mail address');
- $messageField = TextAreaField::create('Message', 'Message');
- $messageField->setAttribute('placeholder', 'Your message');
- $fields = new FieldList($nameField, $emailField, $messageField);
- // Actions
- $actions = new FieldList(
- FormAction::create('doSubmit', 'Send Message')
- );
- // Validator
- $validator = new RequiredFields('Name', 'Email', 'Message');
- return parent::__construct( $controller, $name, $fields, $actions, $validator );
- }
- public function doSubmit( array $data, Form $form )
- {
- if( Director::is_ajax() )
- {
- die('ajax');
- // IS AJAX
- if( ContactPage::sendEmail( $data ) )
- {
- return json_encode(array(
- 'success' => true,
- 'message' => 'Your message has been sent! Please allow up to 2-3 business days before resubmitting your request.'
- ));
- }
- else
- {
- return json_encode(array(
- 'success' => false,
- 'message' => 'Your message could not be sent. Please contact staff@intellectproductions.com manually'
- ));
- }
- }
- else
- {
- die('not ajax');
- // NOT AJAX
- if( ContactPage::sendEmail( $data ) )
- {
- return $this->Controller->curr()->redirectBack();
- }
- else
- {
- Session::set('FormError', 'Your message could not be sent. Please contact staff@intellectproductions.com manually.');
- Session::set('FormInfo.' . $form->FormName() . '.data', $data);
- Session::set('FormInfo.' . $form->FormName() . '.errors', array());
- return $this->Controller->curr()->redirectBack();
- }
- }
- }
- public function forTemplate()
- {
- return $this->customise(array(
- 'FormError' => SessionExtension::flash('FormError')
- ))->renderWith(array( $this->class, 'Form' ));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement