Wolf-Tech
Wolf-Tech

Reputation: 1309

symfony2 form validation formbuilder and custom form type

i'm creating a form with ancustom form type.

This is my mainform:

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);                
} 

This is the 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');                                
}

Evertything i do, the custom Formtype is not validated. What must i do to validate the form type correctly and get the Error via the Location Attribute in the parent Form ?

Upvotes: 0

Views: 2889

Answers (1)

Mun Mun Das
Mun Mun Das

Reputation: 15002

In getDefaultOptions method of main form you have to add 'cascade_validation' => true to to returned array to enable validation on child forms. It is a recent change that hasn't been updated in documentation yet.

Upvotes: 2

Related Questions