简体   繁体   中英

Overriding default behaviour of Symfony2 validation (with QueryBuilder)

I have a problem when I have to insert Entity data in the database.

The entities are showed with the entity field of Symfony2 and I'm using the Query Builder to show only data that is "active". (see below)

Data can be added to the "select" generated by Symfony via Ajax (new "profs" that aren't active (is_active = 0)). But when I select data, added via Ajax and that's not active, I have this error: "This value is not valid." => that's because Symfony2 adds automatic validation due to de querybuilder "where" statement.

Is it possible to override the default Symfony2 behaviour, so that also not active (is_active = 0 (boolean)) -added elements via Ajax- elements can be validated (items that doesn't match the query builder "where" statement below?

$builder->add('professionA', 'genemu_jquerychosen_entity', array(
    'class' => 'SportUserBundle:Profession',
    'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('prof')
            ->where('prof.is_active = :active')
            ->setParameters(array('active' => '1'))
            ->orderBy('prof.name', 'ASC');
    },
));

Thanks!

The easiest way is to initially register all possible elements in that field. That is, get all profession data from database and then recreate or limit the field content using ajax when the document is ready and when active state is changed.

'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('prof')->orderBy('prof.name', 'ASC');
}

Not a very fine solution I guess, but it simply works. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM