"Connect" a form to one created in the view

4 messages Options
Embed this post
Permalink
Simeon Goranov-3

"Connect" a form to one created in the view

Reply Threaded More More options
Print post
Permalink
Hello,
I've got this one in my view script index.phtml:

<h2>Log In</h2>
<p>
<?php echo $this->form('Default_Form_Login', array('name' =>
'form_login', 'id' => 'form_login', 'method' => 'post', 'action' =>
$this->link('login', 'index')), false); ?>
Username <br />
<?php echo $this->formText('form_username');?><br />
Password <br />
<?php echo $this->formPassword('form_password');?><br />
<?php echo $this->formSubmit('Submit', 'Login');?>
</form>
</p>


The form up is created with the view helpers and I want to "connect"
this form to one created from the class:

class Default_Form_Login extends Zend_Form
{
   
    public function __construct($options=array())
    {
        parent::__construct($options);
       
        $this->setName('form_login');
        $this->setMethod('post');

        $element = new Zend_Form_Element_Text('form_username');
        $element->setLabel('Username');
        $this->addElement($element);
       
        $element = new Zend_Form_Element_Password('form_password');
        $element->setLabel('Password');
        $this->addElement($element);

        $element = new Zend_Form_Element_Submit('Login');
        $this->addElement($element);
    }
}


So I will be able to use the form in my action in this way:

public function loginAction()
{
    $form = new Default_Form_Login();
    $result = $form->getValues();

Is it possible and how ?

Regards,
S.G.

prodigitalson

Re: "Connect" a form to one created in the view

Reply Threaded More More options
Print post
Permalink
Well my first question is - if they are the same thing whay arent you
using the class to create the form used in the view you outlined?

Secondly if all the fields have the exact same names/ids then you can
just use the form class in your action as described simply using the
validate method

$form = new Default_Form_Login();
if($form->validate($this->getRequest()->getPost())
{
   $result = $form->getValues();
   // successful validation logic
}

or if you dont want/need to validate...


$form = new Default_Form_Login();
$form->populate($this->getRequest()->getPost());
$result = $form->getValues();

Although with the latter i dont undrestand why youd bother running it
through the form again unless its for redisplay purposes...



Simeon Goranov wrote:

> Hello,
> I've got this one in my view script index.phtml:
>
> <h2>Log In</h2>
> <p>
> <?php echo $this->form('Default_Form_Login', array('name' =>
> 'form_login', 'id' => 'form_login', 'method' => 'post', 'action' =>
> $this->link('login', 'index')), false); ?>
> Username <br />
> <?php echo $this->formText('form_username');?><br />
> Password <br />
> <?php echo $this->formPassword('form_password');?><br />
> <?php echo $this->formSubmit('Submit', 'Login');?>
> </form>
> </p>
>
>
> The form up is created with the view helpers and I want to "connect"
> this form to one created from the class:
>
> class Default_Form_Login extends Zend_Form
> {
>    
>     public function __construct($options=array())
>     {
>         parent::__construct($options);
>        
>         $this->setName('form_login');
>         $this->setMethod('post');
>
>         $element = new Zend_Form_Element_Text('form_username');
>         $element->setLabel('Username');
>         $this->addElement($element);
>        
>         $element = new Zend_Form_Element_Password('form_password');
>         $element->setLabel('Password');
>         $this->addElement($element);
>
>         $element = new Zend_Form_Element_Submit('Login');
>         $this->addElement($element);
>     }
> }
>
>
> So I will be able to use the form in my action in this way:
>
> public function loginAction()
> {
>     $form = new Default_Form_Login();
>     $result = $form->getValues();
>
> Is it possible and how ?
>
> Regards,
> S.G.
>

Simeon Goranov-3

Re: "Connect" a form to one created in the view

Reply Threaded More More options
Print post
Permalink
Thank you for the answer,
I don't use the class, because I want to place the fields and display
the errors in specific way (not in default form class view). That's the
reason to use the view helpers to create the form. But I still need the
form object, because future validation. Thanks again for the answer.

Regards,
S.G.


В 19:07 -0500 на 06.11.2009 (пт), Ant Cunningham написа:

> Well my first question is - if they are the same thing whay arent you
> using the class to create the form used in the view you outlined?
>
> Secondly if all the fields have the exact same names/ids then you can
> just use the form class in your action as described simply using the
> validate method
>
> $form = new Default_Form_Login();
> if($form->validate($this->getRequest()->getPost())
> {
>    $result = $form->getValues();
>    // successful validation logic
> }
>
> or if you dont want/need to validate...
>
>
> $form = new Default_Form_Login();
> $form->populate($this->getRequest()->getPost());
> $result = $form->getValues();
>
> Although with the latter i dont undrestand why youd bother running it
> through the form again unless its for redisplay purposes...
>
>
>
> Simeon Goranov wrote:
> > Hello,
> > I've got this one in my view script index.phtml:
> >
> > <h2>Log In</h2>
> > <p>
> > <?php echo $this->form('Default_Form_Login', array('name' =>
> > 'form_login', 'id' => 'form_login', 'method' => 'post', 'action' =>
> > $this->link('login', 'index')), false); ?>
> > Username <br />
> > <?php echo $this->formText('form_username');?><br />
> > Password <br />
> > <?php echo $this->formPassword('form_password');?><br />
> > <?php echo $this->formSubmit('Submit', 'Login');?>
> > </form>
> > </p>
> >
> >
> > The form up is created with the view helpers and I want to "connect"
> > this form to one created from the class:
> >
> > class Default_Form_Login extends Zend_Form
> > {
> >    
> >     public function __construct($options=array())
> >     {
> >         parent::__construct($options);
> >        
> >         $this->setName('form_login');
> >         $this->setMethod('post');
> >
> >         $element = new Zend_Form_Element_Text('form_username');
> >         $element->setLabel('Username');
> >         $this->addElement($element);
> >        
> >         $element = new Zend_Form_Element_Password('form_password');
> >         $element->setLabel('Password');
> >         $this->addElement($element);
> >
> >         $element = new Zend_Form_Element_Submit('Login');
> >         $this->addElement($element);
> >     }
> > }
> >
> >
> > So I will be able to use the form in my action in this way:
> >
> > public function loginAction()
> > {
> >     $form = new Default_Form_Login();
> >     $result = $form->getValues();
> >
> > Is it possible and how ?
> >
> > Regards,
> > S.G.
> >
>


Daniel Latter

Re: "Connect" a form to one created in the view

Reply Threaded More More options
Print post
Permalink
In reply to this post by Simeon Goranov-3
Hi,

Why don't you use a view script decorator?

http://framework.zend.com/manual/en/zend.form.standardDecorators.html#zend.form.standardDecorators.viewScript

This way you will still be able to get the fine-grained control your after.

Thanks





2009/11/6 Simeon Goranov <[hidden email]>
Hello,
I've got this one in my view script index.phtml:

<h2>Log In</h2>
<p>
<?php echo $this->form('Default_Form_Login', array('name' =>
'form_login', 'id' => 'form_login', 'method' => 'post', 'action' =>
$this->link('login', 'index')), false); ?>
Username <br />
<?php echo $this->formText('form_username');?><br />
Password <br />
<?php echo $this->formPassword('form_password');?><br />
<?php echo $this->formSubmit('Submit', 'Login');?>
</form>
</p>


The form up is created with the view helpers and I want to "connect"
this form to one created from the class:

class Default_Form_Login extends Zend_Form
{

   public function __construct($options=array())
   {
       parent::__construct($options);

       $this->setName('form_login');
       $this->setMethod('post');

       $element = new Zend_Form_Element_Text('form_username');
       $element->setLabel('Username');
       $this->addElement($element);

       $element = new Zend_Form_Element_Password('form_password');
       $element->setLabel('Password');
       $this->addElement($element);

       $element = new Zend_Form_Element_Submit('Login');
       $this->addElement($element);
   }
}


So I will be able to use the form in my action in this way:

public function loginAction()
{
   $form = new Default_Form_Login();
   $result = $form->getValues();

Is it possible and how ?

Regards,
S.G.