문제

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