Working on my new project i really need to find a good solutions for unsetting many fields in easy way. I don’t really like philosophy of unsetting each element that i don’t need. Sometimes when i add new fields to my tables i must remember to unset (all) that fields in my Forms - really irritating:) Finally i found a solution which is working really good for me - it’s nothing special but really helpfull.
All we need to do is to add new method to BaseFormPropel:
<?php
/*
* Unsetting all fields from Form
* except elements given in array
*/
public function unsetAllExcept($fields = array())
{
foreach($this->getObject()->toArray(BasePeer::TYPE_FIELDNAME) as $key => $val){
$tmp[] = strtolower($key);
}
$tmp = array_diff($tmp, $fields);
foreach($tmp as $value){
unset($this[$value]);
}
}
?>
We can also add some exception handling.
And now instead of calling unset many times:
<?php
class MyForm extends BaseMyForm
{
public function configure()
{
unset(
$this['field2'],
$this['field3'],
$this['field4'],
$this['field5'],
$this['field6'],
);
/* other stuff */
}
}
?>
We can just call our new function:
<?php
class MyForm extends BaseMyForm
{
public function configure()
{
$this->unsetAllExcept(array(
'field1'
));
/* other stuff */
}
}
?>
You can call this function what ever you wany but it should be intuitive i think
I completely support your request for having such a function in the forms classes…
On your proposed code however, I’m not sure whether
$this->getObject()
will provide a valid object at any time. AFAIK, it may be that forms do not have an object associated with it, particularly not when in the ::configure() method is being called.
I’m not sure, it’s just a question.
Cheers RAPHAEL
I’m almost sure that it will provide a valid object. You can chceck it in the constructor of sfFormPropel class. First of all we create the associated object to our Form (we can also pass that object as a parameter). Then class is calling parent’s constructor method and that method is calling our setup and configure functions. So, everything i think should be ok.
Part od constructor method of sfFormPropel:
$class = $this->getModelName();
if (is_null($object))
{
$this->object = new $class();
}
else
{
if (!$object instanceof $class)
{
throw new sfException(sprintf(’The “%s” form only accepts a “%s” object.’, get_class($this), $class));
}
$this->object = $object;
$this->isNew = false;
}
// So we can call now $this->getObject()
parent::__construct(array(), $options, $CSRFSecret);
Cheers KAMIL
Great idea and would be great to see this in the Symfony code base.
For Doctrine I have modified your code and now use this:
public function unsetAllExcept($keepFields = array()) {
foreach($this as $key => $value) $baseFields[]=$key;
$temp = array_diff($baseFields, $keepFields);
foreach ($temp as $unsetField) unset($this[$unsetField]);
}
Thanks for the inspiration!
C
Thank you so much, you’re the first result on google for ‘unset all form variables’.
You’ve saved me alot of work, I have some tables in my db that are 200 columns, and only 4-5 columns are going to be editable by the end user.
FYI… another implementation of this idea is at:
http://www.devexp.eu/?p=706
I found the above link helpful because it worked across embedded forms whereas I found the solution proposed here to only unset fields in the form in context.
I believe this function is better after adding following line
$tmp = array();
@krishan:
My mistake, cheers.