0

I'm creating a simple contact form within my ZF application. It doesn't feel like it's worth the trouble to manipulate decorators for only a few form elements.

My Question is: am I able to still use Zend Form Filters on elements that are not created with Zend Form:

For Example:

The Form:

<!-- Standard HTML - not generated with ZF -->
<form id="contact-form" method="post" action="/contact/submit">
    <input type="text" name="name" />
    <input type="email" name="email" />
    <input type="submit" name="submit" />
</form>

The Controller:

public function submitAction()
{
    $params = $this->_request->getParams();
    //Am I able to apply filters/validators to the data I get from the request?
    //What is the best way to handle this?
}

I took a look at this (the answer from Darcy Hastings) - and it seems like it would work, it just feels a bit hacky.

Any and all advice appreciated.

Thanks, Ken

1
  • I agree, I don't really like the Darcy Hastings Answer either. Commented Apr 1, 2012 at 7:03

2 Answers 2

3

yes, you can use Zend_Filter_Input, here is an example of how to set it up.

       //set filters and validators for Zend_Filter_Input
        $filters = array(
            'trackid' => array('HtmlEntities', 'StripTags')
        );

        $validators = array(
            'trackid' => array('NotEmpty', 'Int')
        );

        //assign Input
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());

            //check input is valid and is specifically posted as 'Delete Selected'
            if ($input->isValid()) {

also you may consider using the viewscript decorator to render a Zend Form, The control is absolute (or almost).:

//in your controller action
public function indexAction() {
        //a normally constructed Zend_Form
        $form = new Member_Form_Bid();
        $form->setAction('/member/bid/preference');
        //attach a partial to display the form, this is the decrator
        $form->setDecorators(array(
            array('ViewScript', array('viewScript' => '_bidForm.phtml'))
        ));
  $this->view->form = $form;

//the view
<?php echo $this->form?>

//the partial
//use a normal Zend_Form and display only the parts you want
//processing in an action is done like any other Zend_Form
form action="<?php echo $this->element->getAction() ?>"
      method="<?php echo $this->element->getMethod() ?>">
    <table id="sort">
        <tr>
            <th colspan="2">Sort By Shift</th>
            <th colspan="2">Sort By Days Off</th>
            <th colspan="2">Sort By Bid Location</th>
        </tr>
        <tr></tr>
        <tr>
            <td class="label"><?php echo $this->element->shift->renderLabel() ?></td>
            <td class="element"><?php echo $this->element->shift->renderViewHelper() ?></td>
            <td class="label"><?php echo $this->element->weekend->renderLabel() ?></td>
            <td class="element"><?php echo $this->element->weekend->renderViewHelper() ?></td>
            <td class="label"><?php echo $this->element->bidlocation->renderLabel() ?></td>
            <td class="element"><?php echo $this->element->bidlocation->renderViewHelper() ?></td>
        </tr>
        <tr></tr>
        <tr>
            <td colspan="6" style="text-align: center"><?php echo $this->element->submit ?></td>
        </tr>
    </table>   
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

Is there something you have to do here with addPrefixPath ? Getting this error Plugin by name 'Email' was not found in the registry; used paths: Zend_Form_Element_: Zend/Form/Element/ and based on my searches this is the only thing I'm gathering.
Nvm, it's because I don't have an element of type Email in Zend/Form/Element.
1

Yes, you definitely can use Zend_Form on self-rendered forms.

You can do this in two ways:

  1. Use a Zend_Form object, but don't render it. You create a Zend_Form instance as usual, with all the elements named correctly and attach validators and filters as per normal. In your action, you can then check the form's isValid() and use getValues() to ensure that you collect the filtered data.

  2. The second option is to use Zend_Filter_Input which is a chain of validators and filters. You set up your validators and filters at construction and then call setData to populate the filter with the information from the request. Again, you have isValid() to test and then you use getUnescaped() to retrieve the data. The manual page has more details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.