user698812
user698812

Reputation: 53

Zend form in a popup (fancybox, lightbox....)

I am developping a web application using Zend and I ran out of ideas for a problem I am having. In just a few words, I am trying to have a contact form in a popup (Fancybox, lightbox, colorbox or whatever...). The whole thing works fine, in the sense that it shows up the contact form in the popup and allows to send emails. However, whenever there are errors (unfilled input or filled wrong), I couldn't get those errors to be displayed on the popup (it actually redirects me back to the form in a normal display (view+layout), to show the errors.

It is perhaps possible but I now thought that perhaps I could more easily bring my error message to a new popup (the contact page, filled unproperly, would lead to a error popup page...). I think this alternative could look cool but am having real trouble doing it. Now my real question is : Can we really make a form on a popup, using Facybox (Lighbox or any other actually ... just want my popup) and Zend? Any Guru outhere?? Thanks a lot here is the code:

the link for instance:

<a class="popLink" href=" <?php echo $this->url(array('module'=>'default', 'controller'=>'contact', 'action'=>'sendmail')).'?ProID='.$this->proProfil->getProID(); ?>">Contact</a>

the action:

public function sendmailAction()
{       
    $this->_helper->layout()->setLayout('blank');
    $request = $this->getRequest();     

    $proID = $this->_getParam("ProID");             
    $professionalsList = new Model_DirPro();
    $proName = $professionalsList->getProInfo($proID);

    $translate = Zend_Registry::get('translate');       
    Zend_Validate_Abstract::setDefaultTranslator($translate);       
    Zend_Form::setDefaultTranslator($translate);

    $contactform = new Form_ContactForm();          
    $contactform->setTranslator($translate);
    $contactform->setAttrib('id', 'contact');

    $this->view->contactform = $contactform;        
    $this->view->proName = $proName;

    if ($request->isPost()){
        if ($contactform->isValid($this->_getAllParams())){
            $mailSubject = $contactform->getValue('mailsubject');           
            if ($contactform->mailattcht->isUploaded()) {
                $contactform->mailattcht->receive(); 
                //etc....

the form:

class Form_ContactForm extends Zend_Form
{
  public function init ()
  {
    $this->setName("email");
    $this->setMethod('post');

    $this->addElement('text', 'mailsubject', 
    array('filters' => array('StringTrim'), 
    'validators' => array(), 'required' => true, 'label' => 'Subject:'));

    $mailattcht = new Zend_Form_Element_File('mailattcht');
    $mailattcht->setLabel('Attach File:')->setDestination(APPLICATION_PATH.'/../public/mails');
    $mailattcht->addValidator('Count', false, 1);
    $mailattcht->addValidator('Size', false, 8000000);
    $mailattcht->addValidator('Extension', false, 
    'jpg,png,gif,ppt,pptx,doc,docx,xls,xslx,pdf');
    $this->addElement($mailattcht, 'mailattcht');

    $this->addElement('textarea', 'mailbody', 
    array('filters' => array('StringTrim'), 
    'validators' => array(), 'required' => true, 'label' => 'Body:'));

    $this->addElement('submit', 'send', 
    array('required' => false, 'ignore' => true, 'label' => 'Send'));

    $this->addElement('hidden', 'return', array(
    'value' => Zend_Controller_Front::getInstance()->getRequest()->getRequestUri(),                         
            ));

    $this->setAttrib('enctype', 'multipart/form-data');
  }
}

Upvotes: 2

Views: 3563

Answers (3)

user698812
user698812

Reputation: 53

I found a "probably not the prettiest" working solution, it is to indeed use ajax as mentioned in the previous zendcast for validation to stop the real validation (preventdefault), process the data return the result and if everything's ok restart it.

Upvotes: 0

Ifthikhan
Ifthikhan

Reputation: 1474

Ajax requests are handled via the contextSwitch action helper. You can to specify the various contexts an action needs to handle (xml or json) in the init method of the controller as follows:

public function init()
{
    $this->_helper->contextSwitch()            
        ->addActionContext('send-mail', 'json')
        ->initContext()
    ;
}

The request url should contain a "format=json" appended to the query string. This will execute the action and send the response in json format. The default behaviour of JSON context is to extract all the public properties of the view and encode them as JSON. Further details can be found here http://framework.zend.com/manual/en/zend.controller.actionhelpers.html

Upvotes: 0

Fatmuemoo
Fatmuemoo

Reputation: 2217

I would suggest implementing AJAX validation. This would allow for the form to be verified before it is submitted. ZendCasts has a good tutorial on how to accomplish this: http://www.zendcasts.com/ajaxify-your-zend_form-validation-with-jquery/2010/04/

Upvotes: 2

Related Questions