I have MyForm extending Zend_Form, with init() doing regular job.
In this init() I also need to use a function to apply some transformation on some element values. Basically, get values from db -> custom transformation function -> apply them to form element.
I can put this custom function inside the form itself, but it's a general one and it will be reused by other forms.
Question: what is the best way to do this? I'm reading that helpers are associated more with views, and plugins with controllers. Is there a nice and easy way to get in the form the functionality of a custom function?
(something as helpers in symfony - just a bunch of functions)
Thanks.
p.s. this custom function is not a validator or so.
Related
Is it possible to create a form using form helpers without having an object to link the form to? I want to just design a form and have it render, but I don't have an object to point to yet. Or, is there a simple way to create a dummy object? For instance,
<p>#textField(objectName="dummy", property="name", label="Name")#</p>
objectName seems to be a required parameter. http://cfwheels.org/docs/1-3/function/textfield
In CfWheels, there are four types of form helper functions:
form object functions
form tag functions
Form Association functions
General form functions
The one you are using is form object function which requires object and there are other functions (form tag functions) like bellow that don't require object to bind to:
textFieldTag()
fileFieldTag()
checkBoxTag()
etc....
Check out all [form tag functions][1]
[1]: http://cfwheels.org/docs/1-0/function/category/view-helper category.
I hope this helps.
Actually I wanted to know that is there any way to define and call a function in Zend framework which does not need to put with $this like pr() function in cake php. I want to define a such a function which can be called directly in all controllers and views in zend framework. Can it be done by putting all functions in helper and use them without writing $this in front of them.
For example I wanted to make a function arrprint() to print the array in helper and use it globally with only by writing simply arrprint();
Please help me
You could just create a file anywhere and put the function in there. The just include it like you would any other files.
Or create a library called Utils and put your custom functions there as static and call them using:
Utils::myFunction();
Contrary to cake zf does not enforce much anything. You can leverage the framework to do whatever you want.
BTW $this is just a pointer to the current class and when you use $this->myFunction(); it's because that function is a member of the current class (or its parent).
I would like to add widgets (checkboxes) in an already defined form (with configure method).
I can't add them in the definition of the form because the number of widgets varies (according to the object).
I see two ways of doing it :
Either pass a variable into the configure method of the form or maybe use embedded forms.
But which one is the right way ? Is there another solution ?
Thank you
The right way is to pass the object right into the options. In the form you can use the $this->getOption method to retrieve the passed options.
I Agree with Don Pinkster on passing option and use it to configure form in configure() method.
But if need it or can't get the value when instanciating the class, you can use from anywhere :
$form->getWidgetSchema()->offsetSet($name, $widget);
$form->getValidatorSchema()->offsetSet($name, $validator)
The fact you use embedded forms or widget will not change that much, as you can do this after the form is initially configured :
$form->embedForm($name, $form2);
For just one checkbox I don't see advantages in using embedded form.
In both cases, I suggest you do this in a public method from your form's class, to avoid exploding the form configuration in the action class or elsewhere.
Regards,
I have a controller named class TestController which extends some Zend_Controller_Action. Now I would like to use create an instance of TestController in TestForms (a Zend_Form). I want to populate a Zend_Form_Element_Select dynamically.
Please suggest how I can do this. Thanx in advance.
Where are you instantiating the form - is it in the controller? Instead of having the form call an action on the controller to dynamically get the values, you should look at setting the values on the form after it has been instantiated.
A quick and dirty way of doing that would be to grab the values in the controller and assign it to the element via:
$values = $db->query('query');
$element = $form->getElement('dynamicSelect');
$element->setValue($values);
Of course having DB queries to a table in your controller isn't exactly best practice... Per philistyne's suggestion, I use a a form builder class to build forms dynamically from my models. I have mappers for each model, and I pass in the mapper to the form builder class so it can dynamically populate my select elements.
A couple of things to try (passing a controller into a form or instantiating from within one is not recommended):
Use a model to access the dynamic values you want to put into your Zend_Form_Element_Select.
If the form is complex, create a form builder class to take care of, and separate out, the heavy lifting of the form construction.
Create customised form elements by extending from Zend_Form_Element_(Radio, Select, etc etc) if you feel you need very fine control over the form element's construction/behaviour/appearance, but wish to be able to reuse that element elsewhere.
I just created a custom schema formatter by extending sfWidgetFormSchemaFormatter for my forms. However, what is sad to know is that to use it, I need to specify that in the configure() method of every form I use, which is kinda painful.
Writing this in the BaseForm is a good idea, but the problem is that ModelForm & BaseModelForm don't implicitly call the BaseForm :(
Is there a way out to do this generically?
All doctrine forms extend from BaseFormDoctrine you can put it in the setup method.
Make sure that if you overload setup() any where else to call self::parent()