Lorsque que les filtres par défaut ne suffisent pas, il arrive parfois que l’on ai besoin d’en créer de nouveaux pour l’admin generator. Pour un projet, j’ai eu besoin de filtrer les ID des objets entre deux valeurs. Il était donc nécessaire d’insérer deux champs dans la zone des filtres.
Dans le fichier lib/filter/MaClasseFormFilter.class.php, on va commencer par ajouter les nouveaux champs dans la fonction configure :
public function configure() { $this->widgetSchema['id_debut'] = new sfWidgetFormFilterInput(array('with_empty' => false)); $this->widgetSchema['id_fin'] = new sfWidgetFormFilterInput(array('with_empty' => false)); $this->validatorSchema['id_debut'] = new sfValidatorPass(array('required' => false)); $this->validatorSchema['id_fin'] = new sfValidatorPass(array('required' => false)); }
On ajoute deux nouveaux Widget Input ainsi que leurs validateurs associés.
On va à présent surcharger la fonction getFields qui retourne les champs au formulaire :
public function getFields() { return array_merge(array( 'id_debut' => 'IdDebut', 'id_fin' => 'IdFin', ), parent::getFields() ); }
L’Admin Generator génère pour chaque filtre une fonction addNomDuChampsColumnCriteria qui contient les critères pour la requête de filtrage. On va donc lui fournir deux nouvelles fonctions pour nos deux nouveaux champs. A noter ici que j’utilise Propel. Pour Doctrine, les paramètres de la fonction doivent être différents.
public function addIdDebutColumnCriteria(Criteria $criteria, $field,$values) { if (isset($values['text']) && '' != $values['text']) { $criteria->addAnd(MaClassePeer::ID, $values['text'], Criteria::GREATER_EQUAL); } } public function addIdFinColumnCriteria(Criteria $criteria, $field,$values) { if (isset($values['text']) && '' != $values['text']) { $criteria->addAnd(MaClassePeer::ID, $values['text'], Criteria::LESS_EQUAL); } }
En clair, on complète simplement le criteria de la requête de filtrage. La valeur du champs est contenue dans $values['text'].
Il ne reste plus qu’à ajouter nos champs dans le generator.yml de votre module :
filter: display: [ id_debut, id_fin ]
Génial !!!
Me manquait la notion de méthode autogénérée par champ !!!
Tu viens de me faire gagner une heure !!!! Merci !
De mon point de vue, l’override du getFields n’est pas nécessaire …
Cool =)
Howdy just wanted to give you a quick heads up. The text in your content seem to be running off the screen in Safari. I’m not sure if this is a format issue or something to do with browser compatibility but I figured I’d post to let you know. The layout look great though! Hope you get the issue solved soon. Cheers
My blog site is in the very same niche as yours and my visitors would truly benefit from a lot of the information you provide here. Please let me know if this ok with you. Regards!
You can definitely see your enthusiasm in the work you write. The world hopes for more passionate writers like you who aren?—t afraid to say how they believe. Always go after your heart.
31. I do agree with all of the ideas you’ve presented in your post. They’re really convincing and will certainly work. Still, the posts are too short for novices. Could you please extend them a bit from next time? Thanks for the post.
Actually instructive and superb structure of articles, now that’s user friendly (:.
I can see that you’re an professional in this place. I’m launching a site quickly,and your information and facts will likely be very helpful for me.. Thanks for all of your help and wishing you each of the success within your home business.
I was wondering if I could copy this information in my site. I’ll provide a link to your site, can I?
My brother recommended I would possibly like this blog. He was once entirely right. This submit actually made my day. You cann’t believe simply how a lot time I had spent for this information! Thank you!
Hi there just wanted to give you a quick heads up. The text in your post seem to be running off the screen in Firefox. I’m not sure if this is a formatting issue or something to do with browser compatibility but I thought I’d post to let you know. The layout look great though! Hope you get the problem solved soon. Kudos
Some really nice stuff on this internet site , I it.
I like the helpful info you provide in your articles. I will bookmark your blog and check again here regularly. I’m quite sure I will learn many new stuff right here! Best of luck for the next!