This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » legacy support » symfony 1.2 » Form select with session
Form select with session [message #81387] Wed, 08 July 2009 20:29 Go to next message
ojie43  is currently offline ojie43
Messages: 26
Registered: June 2009
Location: indonesia
Junior Member
hi....all

.....
$this->setWidgets(array(
'id' => new sfWidgetFormInputHidden(),
'username' => new sfWidgetFormInput(),
'email' => new sfWidgetFormInput(),
'password' => new sfWidgetFormInputPassword(),
'level' => new sfWidgetFormChoice(array(
'expanded' => false,
'choices' => array('mahasiswa' =>'Mahasiswa', 'staff' =>'staff', 'dosen' =>'Dosen', 'superadmin' =>'Administrator'))),
'created_at' => new sfWidgetFormDateTime(),
));

How show level only dosen and mahasiswa for staff credential ?
thanks
Re: Form select with session [message #81389 is a reply to message #81387 ] Wed, 08 July 2009 20:59 Go to previous messageGo to next message
Russ  is currently offline Russ
Messages: 413
Registered: August 2008
Location: Oslo, Norway
Faithful Member

Personally I like to keep the form classes decoupled from the application logic, so you could either accept a user object as a parameter to the form (dependency injection) or use an option that is passed when the form is instantiated, which simply sets a flag you can inspect in your form class.

For example in your action:

// standard form
$myForm = new mySomethingForm(array(), array("user" => $this->getUser()));

//object form
$myForm = new mySomethingForm($myObject, array("user" => $this->getUser()));


Then in your form class:

if (!isset($this->getOption("user")) || (!$this->getOption("user") instanceof sfUser)
{
  throw new InvalidArgumentException("You must pass a user object as an option to this form!);
}

$user    = $this->getOption("user");
$choices = array('mahasiswa' =>'Mahasiswa', 'dosen' =>'Dosen');

if ($user->hasCredential("superAdmin")
{
  $choices['superadmin'] = 'Administrator';
  $choices['staff']      = 'staff';
}


Since your form depends on the user object so it can check credentials, it is fine to force the developer to pass one when declaring the form. Do not be tempted to do this:

$user = sfContext::getInstance()->getUser();


As you will then force your form to only work in the symfony context, which is bad. Especially when you come to write unit tests and things like that...

The alternative which I mentioned above would be instead of passing a user object to the form, you can make the decision in the action and then pass the decision (as a flag) to the form options. array("show_admin_options" => true) for example. The downside with that is you will have to remember to apply the same logic every time you instantiate the form, and it's possible you will forget...

Hope that helps Smile

[Updated on: Thu, 09 July 2009 13:06]


*On Freenode I am rooster*
http://www.walkandclimb.co.uk/images/monkeydust.jpghttp://www.walkandclimb.co.uk/userpix/2_linux_wave_small_2.jpg
Re: Form select with session [message #81422 is a reply to message #81389 ] Thu, 09 July 2009 07:45 Go to previous messageGo to next message
ojie43  is currently offline ojie43
Messages: 26
Registered: June 2009
Location: indonesia
Junior Member
thanks russ for your answer...but I get error....
for more details, here I give my code and some images
please help me....
thanks...

this image for superadmin's credential level have 4 options
http://i567.photobucket.com/albums/ss119/ojie43/1-1.jpg

this image for staff's credential level have 2 options
http://i567.photobucket.com/albums/ss119/ojie43/2.jpg

plese help me....
thanks

[Updated on: Thu, 09 July 2009 10:17]

Re: Form select with session [message #81433 is a reply to message #81422 ] Thu, 09 July 2009 10:35 Go to previous messageGo to next message
Russ  is currently offline Russ
Messages: 413
Registered: August 2008
Location: Oslo, Norway
Faithful Member

Hi,

Where is your form class? And what error did you get?


*On Freenode I am rooster*
http://www.walkandclimb.co.uk/images/monkeydust.jpghttp://www.walkandclimb.co.uk/userpix/2_linux_wave_small_2.jpg
Re: Form select with session [message #81440 is a reply to message #81433 ] Thu, 09 July 2009 11:49 Go to previous messageGo to next message
ojie43  is currently offline ojie43
Messages: 26
Registered: June 2009
Location: indonesia
Junior Member
Catchable fatal error: Argument 1 passed to sfFormPropel::__construct() must be an instance of BaseObject, array given, called in E:\web\simalkom\apps\depan\modules\user\actions\actions.clas s.php on line 26 and defined in E:\Ojie Dokument\xampp\php\PEAR\symfony\plugins\sfPropelPlugin\lib\f orm\sfFormPropel.class.php on line 35AR\symfony\plugins\sfPro


Re: Form select with session [message #81455 is a reply to message #81440 ] Thu, 09 July 2009 13:19 Go to previous messageGo to next message
ojie43  is currently offline ojie43
Messages: 26
Registered: June 2009
Location: indonesia
Junior Member
hi russ thanks...
this my code in userForm class

class UserForm extends BaseUserForm
{
public function configure()
{
$ses= sfContext::getInstance()->getUser();
$r=$ses->getAttribute('superadmin');
if($r=TRUE)
{
$choices = array('mahasiswa' =>'Mahasiswa', 'dosen' =>'Dosen');

}
else
{
$choices = array('mahasiswa' =>'Mahasiswa', 'staff' =>'staff', 'dosen' =>'Dosen', 'superadmin' =>'Administrator');
}
$this->widgetSchema['level'] = new sfWidgetFormChoice(array(
'expanded' => false,
'choices' => $choices));

}
}

in my action

$this-> new UserForm();

it' work to make 2 option..
thanks..
Re: Form select with session [message #81456 is a reply to message #81387 ] Thu, 09 July 2009 13:24 Go to previous messageGo to next message
Russ  is currently offline Russ
Messages: 413
Registered: August 2008
Location: Oslo, Norway
Faithful Member

Your form extends an object form, not a basic sfForm, so you need to pass an object as the first parameter (or no parameters).

So if you are passing options as the second array, you should pass an empty object as the first parameter:

 public function executeNew(sfWebRequest $request)
  {
    $this->form = new UserForm(new User(), array("level" => $this->getUser()));
  }


*On Freenode I am rooster*
http://www.walkandclimb.co.uk/images/monkeydust.jpghttp://www.walkandclimb.co.uk/userpix/2_linux_wave_small_2.jpg
Re: Form select with session [message #81466 is a reply to message #81456 ] Thu, 09 July 2009 14:07 Go to previous messageGo to next message
ojie43  is currently offline ojie43
Messages: 26
Registered: June 2009
Location: indonesia
Junior Member
I use standar form
but my code work...

for action new

 public function executeNew(sfWebRequest $request)
  {
    $this->form = new UserForm();
  }


for UserForm class

<?php
class UserForm extends BaseUserForm
{
  public function configure()
  {
$ses= sfContext::getInstance()->getUser();
$choices = array('mahasiswa' =>'Mahasiswa');

if(!$ses->hasCredential('superadmin'))
{
	
$choices['mahasiswa']      = 'Mahasiswa';

}
else
{
	$choices['superadmin'] = 'Administrator';
  $choices['mahasiswa']      = 'Mahasiswa';
$choices['dosen'] = 'Dosen';
  $choices['staff']      = 'staff';

}
	$this->widgetSchema['level'] = new sfWidgetFormChoice(array(
	'expanded' => false,
	'choices' => $choices));

  }
}



for superadmin's credential

I succeses get 4 option
-Superadmin
-mahasiswa
-dosem
-staff

for other I get one and it default value in my schema.yml


Russ, do you have a facebook account, I want you be my friend in facebook or your IM. if you are interested through this link http://www.facebook.com/ojie.permana?ref=name

thanks before
Re: Form select with session [message #81470 is a reply to message #81466 ] Thu, 09 July 2009 14:25 Go to previous messageGo to next message
Russ  is currently offline Russ
Messages: 413
Registered: August 2008
Location: Oslo, Norway
Faithful Member

In the interest of security, you should probably work the other way around with the options assignment:

class UserForm extends BaseUserForm
{
  public function configure()
  {
    $ses= sfContext::getInstance()->getUser();
    $choices = array('mahasiswa' =>'Mahasiswa');

    if($ses->hasCredential('superadmin'))
    {	 
      $choices['superadmin'] = 'Administrator';
      $choices['dosen']      = 'Dosen';
      $choices['staff']      = 'staff';
    }
 	$this->widgetSchema['level'] = new sfWidgetFormChoice(array(
	'expanded' => false,
	'choices' => $choices));

  }
}


Is "superadmin" an actual credential that you have set? Or have you set the is_super_admin flag in the database?

If "superadmin" is an actual credential you are checking for, then it is case sensitive! If is_super_admin is set to 1 in the database, then hasCredential() will always return true!

I'm not extending my "services" to Facebook support just yet I'm afraid, but feel free to ping me on IRC Smile


*On Freenode I am rooster*
http://www.walkandclimb.co.uk/images/monkeydust.jpghttp://www.walkandclimb.co.uk/userpix/2_linux_wave_small_2.jpg
Re: Form select with session [message #81472 is a reply to message #81387 ] Thu, 09 July 2009 14:28 Go to previous messageGo to next message
Russ  is currently offline Russ
Messages: 413
Registered: August 2008
Location: Oslo, Norway
Faithful Member

Also please remove the:

sfContext::getInstance()->getUser();


It will bite you in the ass later, and is a bad habit Rolling Eyes


*On Freenode I am rooster*
http://www.walkandclimb.co.uk/images/monkeydust.jpghttp://www.walkandclimb.co.uk/userpix/2_linux_wave_small_2.jpg
Re: Form select with session [message #81477 is a reply to message #81472 ] Thu, 09 July 2009 14:47 Go to previous messageGo to next message
ojie43  is currently offline ojie43
Messages: 26
Registered: June 2009
Location: indonesia
Junior Member
oke russ......thanks
Re: Form select with session [message #99227 is a reply to message #81389 ] Mon, 17 May 2010 19:48 Go to previous message
JessOVR  is currently offline JessOVR
Messages: 48
Registered: April 2008
Location: France
Member
Russ wrote on Wed, 08 July 2009 20:59

Personally I like to keep the form classes decoupled from the application logic, so you could either accept a user object as a parameter to the form (dependency injection) or use an option that is passed when the form is instantiated, which simply sets a flag you can inspect in your form class.


Hi,

These days I faced this situation, where you need to inject bizness rules (like user credentials) inside a form, in order to tweaking it.
In my case, 'tweaking' meant setting different validators according to user profile.

Looking for some good practrices in the symfony forum, I read this post. As Russ, I don't want to couple application logic and form, so far as I can.

I like the practrices proposed by Russ, and want to share my solution. It simply relies on the fact we're dealing with classes Smile Don't forget your form is a class. Then you can derive from it and specialize its child for a profile (or whatever you need).

Then you keep the credential and bizness logic in the controller (the action), where you decide to instantiate one or another form class. It is very simple to maintain, and keep your form clean.

Hope it helps as well Smile
I'm also quite sure all these solutions have their own pros/cons, depending on architecture and bizness logic.

Previous Topic:Pagination
Next Topic:forward404 - second time with http code 200
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software