symfony2表单验证formbuilder和自定义表单类型

时间:2012-05-09 23:04:36

标签: validation symfony

我正在创建一个带有自定义表单类型的表单。

这是我的主要形式:

public function buildForm(FormBuilder $builder, array $options) {

    $builder->add('area', new AreaSelectType($this->em, true, true, $this->organizercountry), array('selectedArea'  => null,'label' => 'City'));        
    $builder->add('location', new LocationAutocompleteType('#steptwo_location_autocomplete', '#steptwo_location_hidden', '#steptwo_area_pkid'));                        
}

public function getDefaultOptions(array $options)
{

    $collectionConstraint = new Collection(array(            
        'area' => new NotNull(array('message' => 'nicht leer')),            
        'location' => new NotNull(array('message' => 'nicht leer'))            
    ));

    return array('validation_constraint' => $collectionConstraint);                
} 

这是LocationAutocompleteType:

public function buildForm(FormBuilder $builder, array $options) {

    $builder->setAttribute('source', $options['source']);                        
    $builder->add('autocomplete', 'text', array('label' => false, 'attr' => array('autocomplete' => 'off')));
    $builder->add('hidden', 'hidden', array('label' => false, 'attr' => array('autocomplete' => 'off')));        
}

public function getDefaultOptions(array $options) {

    $collectionConstraint = new Collection(array(            
        'hidden' => new NotNull(array('message' => 'nicht leer')),                        
    ));

    return array('validation_constraint' => $collectionConstraint, 'source' => 'organizer.ajax.location');                                
}

我做的事情,自定义的Formtype未经过验证。 我该怎么做才能正确验证表单类型并通过父表单中的位置属性获取错误?

1 个答案:

答案 0 :(得分:2)

在主窗体的getDefaultOptions方法中,您必须将'cascade_validation' => true添加到返回的数组以启用子窗体上的验证。这是最近尚未在文档中更新的更改。