• Edit
  • Delete

ullright contact form with honeypot

apps/frontend/lib/form/ContactForm.php

<?php

class ContactForm extends BaseForm
{
  public function configure()
  {
    $this->setWidgets(array(
      'name'  => new sfWidgetFormInputText(array(), array('size' => 40)),
      'phone' => new sfWidgetFormInputText(array(), array('size' => 40)),
      'email' => new sfWidgetFormInputText(array(), array('size' => 40)),
      'text'  => new sfWidgetFormTextArea(array(), array('rows' => 10, 'cols' => 60)),
      'fill_in_this_if_youre_not_human' => new sfWidgetFormInput(array(),
        array('style' => 'display: none;'))
    ));
    
    $this->widgetSchema->setNameFormat('fields[%s]');
    $this->widgetSchema->setFormFormatterName('ullTable');
  
    $this->widgetSchema->setLabels(array(
      'name'  => __('Name', null, 'common'),
      'phone' => __('Phone', null, 'common'), 
      'email' => __('Email', null, 'common'),
      'text'  => __('Subject', null, 'common'),
    ));
    
    $this->setValidators(array(
      'name' => new sfValidatorString(
            array('required' => true), 
            array(
              'required'    => 'Bitte Ihren Namen angeben',
            )),
      'phone' => new sfValidatorString(
             array('required' => false) 
            ),  
      'email' => new sfValidatorEmail(
            array('required' => true),
            array(
              'invalid'     => 'keine gültige E-Mail-Adresse',
            )),
      'text' => new sfValidatorString(
            array('required' => true), 
            array(
              'required'    => 'Bitte einen Text eingeben',
            )),
      'fill_in_this_if_youre_not_human' => new ullValidatorMustBeEmpty(
            array('required' => false)),
    ));
    
  }
}

actions.php

  public function executeShowContact(sfRequest $request)
  {
    $this->allow_edit = UllUserTable::hasPermission('ull_cms_edit');
    
    $this->contact_form = new ContactForm();
    
    if ($body = $request->getParameter('body')) 
    {
      $this->contact_form->setDefault('text', $body);
    }
    
    if ($request->isMethod('post'))
    {
      $this->contact_form->bind($request->getParameter('fields'));
    
      if ($this->contact_form->isValid())
      {
        $name = $this->contact_form->getValue('name');
        $phone = $this->contact_form->getValue('phone');
        $email = $this->contact_form->getValue('email');
        $text = $this->contact_form->getValue('text');
        $this->sendEmailNewContactForm($name, $phone, $email, $text);
        
        $this->contact_form = new ContactForm();
    
        $this->getUser()->setFlash('message', __('Thank you for your message.', null, 'custom'));
      }
    
    }    
  }

ContactSuccess.php (template)

<div class="contact_form contact_box">
  <?php include_partial('ullTableTool/flash', array('name' => 'message')) ?>
  <?php include_partial('ullTableTool/globalError', array('form' => $contact_form)) ?>

  <?php echo form_tag('@ull_cms_show?slug=contact') ?>
    <table>
      <tbody>        
        <tr>
          <td><?php echo $contact_form['name']->renderLabel() ?></td>     
          <td><?php echo $contact_form['name']->render() ?></td>
          <td><?php echo $contact_form['name']->renderError() ?></td>
        </tr>
        <tr>
          <td><?php echo $contact_form['phone']->renderLabel() ?></td>     
          <td><?php echo $contact_form['phone']->render() ?></td>
          <td><?php echo $contact_form['phone']->renderError() ?></td>
        </tr> 
        <tr>
          <td><?php echo $contact_form['email']->renderLabel() ?></td>     
          <td><?php echo $contact_form['email']->render() ?></td>
          <td><?php echo $contact_form['email']->renderError() ?></td>
        </tr>
        <tr>
          <td><?php echo $contact_form['text']->renderLabel() ?></td>     
          <td><?php echo $contact_form['text']->render() ?></td>
          <td><?php echo $contact_form['text']->renderError() ?></td>
        </tr>
      </tbody>
    </table>
    
    <?php echo $contact_form['fill_in_this_if_youre_not_human']->render() ?>
    <?php echo $contact_form['fill_in_this_if_youre_not_human']->renderError() ?>    
    
    <?php echo submit_tag(__('Send', null, 'common'), array('class' => 'contact_submit')); ?>
      
  </form>
</div>