![]() |
|
Snippets |
|
Extend the myUser.class.php in any /lib folder with the following function:
public function isInGroups(array $groups, $strict = false) { if ($strict) { return (array_intersect($groups, $this->getGroupNames()) == $groups); } else { foreach ($groups as $g) { if (in_array($g, $this->getGroupNames())) return true; } } return false; }
Now you can check the groups of the user as following:
if ($sf_user->isInGroups(array('admin', 'editor'))) { // do stuff here... }
You can also check if the user is in all the groups specified. Just set the second parameter to true:
if ($sf_user->isInGroups(array('admin', 'editor'), true)) { // this is executed only if the user is in the groups admin AND editor // please note: he might still be a member of other groups }
I spent some time trying to figure out how to make sure that one checkbox was checked before saving a form in symfony 1.2.
I read about Global Validators (http://www.symfony-project.org/forms/1_2/en/02-Form-Validation#chapter_02_global_validators) but I couldn't find anything to fit my needs. Finally, after looking at this snipped: http://www.symfony-project.org/cookbook/1_2/en/conditional-validator , I created the validation I needed, here is the snipped to help someone in the same situation:
class SampleForm extends BaseSampleForm { public function configure() { // add a post validator $this->validatorSchema->setPostValidator( new sfValidatorCallback(array('callback' => array($this, 'checkAtLeastOne'))) ); unset( $this['created_at'], $this['updated_at'] ); } public function checkAtLeastOne($validator, $values) { if (!$values['sunday'] && !$values['monday'] && !$values['tuesday'] && !$values['wednesday'] && !$values['thursday'] && !$values['friday'] && !$values['saturday']) { // no checkbox was checked, throw an error throw new sfValidatorError($validator, 'Check at least one day of the week'); } // at least one checkbox is checked, return the clean values return $values; } }