Symfony : Add widgets to already defined form - forms

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,

Related

backpack for laravel: how to customize routes?

We need to change every <anything>/show routes to something localized.
How can we customize the show string in something like dettagli ?
You can do in two way:
By creating a custom operation, starting from the default Show operation. Copy-paste the code of the ShowOperation.php in your project, and change the route. Then throughout your project use your ShowOperation, instead of the one provided by Backpack.
By overriding the protected function setupShowRoutes($segment, $routeName, $controller) in your CrudController. If you have that method in your ProductCrudController for example, your method will be run instead of the one in the ShowOperation trait. However, this needs to be done in all CrudControllers individually, so it's less DRY.

CFWheels form without objectName for design purposes

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.

Label of element as variable in error message for validation

Is it possible to use a variable inside the error message referring to the label of the form element? It's possible to map custom variables like %hostname% (in email validator) and the %value% is also available, but I'd like to have the form label as well.
I could't find it in the ZF codebase, but the use case is for example that the Zend_Validate_NotEmpty can return a message like:
"The field %label% is required and can't be empty"
Instead of:
"Value is required and can't be empty"
I think it's not possible without subclassing the validators. The Zend_Validate classes are not intended to be used with Zend_Form_Element only. Having a %label% in the message would introduce a coupling between both components.
A possible solution could be to create custom validators by extending Zend_Validate_NotEmpty (or whatever validators you are using) and pass the label to the constructor. This way, you could compose the appropriate message every time you instantiate it.
EDIT:
If you follow the method above, you could even define your own %label% "magic variable" and attach it to a member of the class. See the $_messageVariables member in the Example #2 in the Zend Framework documentation: Writing validators
Hope that helps...
I finally went with the decorator. The solution of dinopmi is possible, however you need to inject the label all the times. My error decorator for the form element replaces now the %label% to the real label.

How to create instance to zend controller

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.

Zend Framework - Using custom functions in Forms

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.