Вопрос

I'm using Zend Framework 1.12 on a project. I having some problems with the Zend_Form. Some fields are generated dynamically on execution time, but the Zend_Form is static, a element predefined at creation.

So, when the form is sent, the validation doesn't work because new fields were added and the sent form doesn't match the form created.

How do that adaptation?

Это было полезно?

Решение

You should try, following solution: after sending the form, get the $_POST array, then check which fields/values do you have and create/modify form Object with this fields/validation.

Другие советы

I would have done this way :

class MyForm extends Zend_Form
{
    public function init()
    {
        //... Create here the basic elements

    }

    public function initFromPostValue( $post )
    {
        if( array_key_exists( 'dynamicsField', $post ) ) {
            $el = $this->createElement( 'select', 'dynamicsField' )
                ->setValidators( array( ... PUT your validators here ) );

            $this->addElement( $el );
        }

    }
}

In the validation action :

public function validationAction()
{
    $form = new MyForm();
    $form->initFromPostValue( $_POST );

    if( $form->isValid( $_POST ) ) {
        // Form is valid
    } else {
        // Form is invalid
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top