Access views url function in Zend_Form - zend-framework

I have some Forms that for some reasons have to be instantiated in the different modules bootstraps. But in those forms I want to use
$this->setAction($this->getView()->url(array('controller' => 'foo', 'action' => 'bar')));
in the constructor. But since the viewhelper url is not accessible yet since I'm in the bootstrap, is there anyway around this? What I get now is
Fatal error: Uncaught exception 'Zend_Controller_Router_Exception' with message 'Route default is not defined'
on that line. I have NO custom routes so I only use the default router and routes.

If you really need to instantiate the forms so early, then perhaps one possibility is the order of the initializations in your Bootstrap.
In the Bootstrap method where you instantiate your forms, just make sure that you bootstrap the view first. Then grab the view object and pass it to the form during instantiation.
Something like:
protected function _initForms()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$form = new My_Form($view);
// ...
}
Then in your form, you can do something like:
public function __construct($view)
{
$this->setView($view);
}
public function init()
{
// set the action
$this->setAction($this->getView()->url(array(
'controller' => 'foo',
'action' => 'bar',
)));
// create your form elements
// ...
}
Whaddya think?

I decided to change my approach and made the class that manages all the forms accept strings of forms instead of the forms.
static public function registerForm($formClassName, $formName) {
self::$_forms[$formName] = $formClassName;
}
Then I created function to get a form or all forms from the class like so
public function getForm($name) {
if(empty(self::$_forms[$name])) {
throw new Core_Form_Store_Exception;
}
// If the for is instanciated we return it
else if(self::$_forms[$name] instanceof Zend_Form) {
return self::$_forms[$name];
}
else {
// Instanciate the class and return it
$form = new self::$_forms[$name];
self::$_forms[$name] = $form;
return $form;
}
}
getForms() only calls getForm() for each available form. I tired to mimic the interface of the normal Zend_Form a bit when it comes to the names of functions and the order arguments are passed. Now I have it working and as a bonus I don't actually instantiate any form until I need it. And if I only request a specific form from the store class only that form is instantiate and saved for future access.

How about you set the action from the view when rendering the form, eg
<?php echo $this->form->setAction($this->url(array(
'controller' => 'foo',
'action' => 'bar'
))) ?>

Related

ZF2 - text element, array name, validation

I've been reading a bit about form collections/fieldsets in ZF2 as I want to add some array fields to my form.
However as I understand - fieldsets/collections are build with domain entities in mind. In my situation this solution doesn't seem to be anywhere close to good idea.
My form is not related to any entity, it is just meant to pass params to pdf generator. These params are serialized and save in just one column in db.
Form itself is quite big and has a lot of fields like:
Question:
a) smth
b) smth
c) smth
d) other
a) other 1
b) other 2
...
x) other 22
So, I have multiple elements for extra input from user which have to be added dynamically.
Building a separated class for a fieldset containing ... 1 extra field each would result in atleast 20 extra classes per one form.
I thought I can make it simply by:
$element = new Element\Text('citiesOther[]');
$element->setAttributes(array(
'id' => 'citiesOther',
'placeholder' => 'Other city',
));
$this->add($element);
//view:
$this->formElement( $form->get( 'citiesOther[]' )
And then in the frontend there was a button "add another" which was just cloning whole input. It worked quite well, I could receive params from the post.
The thing is... I am unable to filter/validate these fields. If I pass a name "citiesOther[]" to inputFilter, it's not even validating one of these fields, not even mentioning whole array.
Collections in Zend Framework are a bit tricky to understand. Basically you need a basic fieldset for a collection. This fieldset implements the InputFilterProviderInterface for filtering and validation. With this in mind, you have to define all your repeatable input fields and the validation for this fields in a collection. It is not a good idea not validating this data. There are a few scenarios for injecting really bad data in a pdf. So please validate all data, which is coming from an form. Always!
Here 's a quick example how collection with Zend Framework are working.
namespace Application\Form;
use Zend\Form\Fieldset;
use Zend\Form\Element\Text;
use Zend\InputFilter\InputFilterProviderInterface;
class YourCollection extends Fieldset implements InputFilterProviderInterface
{
public function init()
{
$this->add([
'name' => 'simple',
'type' => Text::class,
'attributes' => [
...
],
'options' => [
...
]
]);
}
public function getInputFilterSpecification()
{
return [
'simple' => [
'required' => true,
'filters' => [
...
],
'validators' => [
...
],
],
];
}
}
This is the basic fieldset class, which acts as collection of all input fields you want to repeat in your form. This basic fieldset filters and validates itself by implementing the InputFilterProviderInterface. By using the implemented method, you can place filters and validators for your inputs
If you have dependent input fields, you have to define them here in this fieldset.
For using it right, you need an entity, which is bound to the basic fieldset later in this post. Next, we create the entity for this fieldset.
namespace Application\Entity;
class YourCollectionEntity
{
protected $simple;
public function getSimple()
{
return $this->simple;
}
public function setSimple($simple)
{
$this->simple = $simple;
return $this;
}
}
This is a very simple entity. This will act as your data holder and will be bound to the collection later. Next we need a fieldset, that contains the collection and a hidden field for the collection count. Sounds a bit complecated using another fieldset. But in my eyes this is the best way.
namespace Application\Form;
class YourCollectionFieldset extends Fieldset implements InputFilterProviderInterface
{
public function init()
{
$this->add([
'name' => 'collectionCounter',
'type' => Hidden::class,
'attributes' => [
...
],
]);
// add the collection fieldset here
$this->add([
'name' => 'yourCollection',
'type' => Collection::class,
'options' => [
'count' => 1, // initial count
'should_create_template' => true,
'template_placeholder' => '__index__',
'allow_add' => true,
'allow_remove' => true,
'target_element' => [
'type' => YourCollection::class,
],
],
]);
}
public function getInputFilterSpecification()
{
return [
'collectionCounter' => [
'required' => true,
'filters' => [
[
'name' => ToInt::class,
],
],
],
];
}
}
This fieldset implements your collection and a hidden field, which acts as a counter for your collection. You need both when you want to handle collections. A case could be editing collection contents. Then you have to set the count of the collection.
As you can imagine, you need an entity class for this fieldset, too. So let 's write one.
namespace YourCollectionFieldsetEntity
{
protected $collectionCounter;
protected $yourCollection;
public function getCollectionCounter()
{
return $this->collectionCounter;
}
public function setCollectionCounter($collectionCounter)
{
$this->collectionCounter = $collectionCounter;
return $this;
}
public function getYourCollection()
{
return $this->yourCollection;
}
public function setYourCollection(YourCollectionEntity $yourCollection)
{
$this->yourCollection = $yourCollection;
return $this;
}
}
This entity contains the setYourCollection method, which takes an YourCollectionEntity instance as parameter. We will see later, how we will do that little monster.
Let 's wrap it up in a factory.
namespace Application\Form\Service;
YourCollectionFactory
{
public function __invoke(ContainerInterface $container)
{
$entity = new YourCollectionFieldsetEntity();
$hydrator = (new ClassMethods(false))
->addStrategy('yourCollection', new YourCollectionStrategy());
$fieldset = (new YourCollectionFieldset())
->setHydrator($hydrator)
->setObject($entity);
return $fieldset;
}
}
This factory adds a hydrator strategy to the hydrator. This will hydrate the entity bound to the repeatable fieldset.
How to use this in a form?
The above shown classes are for the needed collection only. The collection itself is not in a form yet. Let 's imagine we have a simple form, which implements this collection.
namespace Application\Form;
class YourForm extends Form
{
public function __construct($name = null, array $options = [])
{
parent::__construct($name, $options);
// add a few more input fields here
// add your collection fieldset
$this->add([
'name' => 'fancy_collection',
'type' => YourCollectionFieldset::class,
]);
// add other simple input fields here
}
}
Well, this is a simple form class, which uses the collection shown above. As you have written, you need a validator for validating all the data this form contains. No form validator without an entity. So first, we need an entity class for this form.
namespace Application\Entity;
class YourFormEntity
{
// possible other form members
...
protected $fancyCollection;
public function getFancyCollection()
{
return $this->fancyCollection;
}
public function setFancyCollection(YourCollectionFieldsetEntity $fancyCollection)
{
$this->fancyCollection = $fancyCollection;
return $this;
}
// other getters and setters for possible other form fields
}
... and finally the validator class for your form.
namespace Application\InputFilter;
class YourFormInputFilter extends InputFilter
{
// add other definitions for other form fields here
...
}
}
We don 't need to redefine filters and validators here for your collection. Remember, the collection itself implements the InputFilterProviderInterface which is executed automtically, when the validator for the form, which contains the collection, is executed.
Let 's wrap it up in a factory
namespace Application\Form\Service;
class YourFormFactory
{
public function __invoke(ContainerInterface $container)
{
$container = $container->getServiceLocator();
$entity = new YourFormEntity();
$filter = new YourFormInputFilter();
$hydrator = (new ClassMethods(false))
->addStrategy('fancy_collection', new FancyCollectionStrategy());
$form = (new YourForm())
->setHydrator($hydrator)
->setObject($entity)
->setInputFilter($filter);
return $form;
}
}
That 's all. This is your form containing your collection.
Hydrator strategies are your friend
Above two hydrator strategies are added to the hydrators you need for your form. Adding hydrator strategies to a hydrator works like a charme, if you have complex post data, which is ment to be pressed in enities.
namespace Application\Hydrator\Strategy;
class YourCollectionStrategy extends DefaultStrategy
{
public function hydrate($value)
{
$entities = [];
if (is_array($value)) {
foreach ($value as $key => $data) {
$entities[] = (new ClassMethods())->hydrate($data, new YourCollectionEntity());
}
}
return $entities;
}
}
This hydrator strategy will hydrate the entity four the repeated collection fieldset. The next strategy will hydrate the whole collection data into your form entity.
namespace Application\Hydrator\Strategy;
class FancyCollectionStrategy extends DefaultStrategy
{
public function hydrate($value)
{
return (new ClassMethods())
->addStrategy('yourCollection', new YourCollectionFieldsetEntity())
->hydrate($value);
}
}
This one will hydrate the collection count and the repeated fieldset. That 's all for the hydration of your data.
How does that look in a controller?
Well, that 's simple. Now, as we have all the classes we need for a complex form with collection, wie can go on with the controller.
namespace Application\Controller;
class YourController extends AbstractActionController
{
protected $form;
public function __consturct(Form $form)
{
$this->form = $form;
}
public function indexAction()
{
$request = $this->getRequest();
if ($request->isPost()) {
$this->form->setData($oRequest->getPost());
if ($this->form->isValid()) {
// get all data as entity
$data = $this->form->getData();
// iterate through all collection data
foreach ($data->getFancyCollection()->getYourCollection() as $collection) {
// get every simple field from every collection
echo $collection->getSimple();
}
}
}
}
}
Sure, this is way more complex than just rerieving the raw post data. But as a mentioned before, you should not use the raw data because of security reasons. Always validate and filter data, which was given by a user over a form. Or just to keep it simple: do not trust the user!

Symfony Form Component Standalone Form Type Extension

I have a project using some components from Symfony. Namely Twig, Doctrine, and Form.
I would like to modify all form field types to be able to take a new 'suffix' argument when they are created. It seems like this is usually simple in the full Symfony stack and could be done by extending the existing form type. However I'm not sure how to get the form component to load my custom extension when using the Form Component standalone.
Any help would be great, thank you!
Okay, if I understood your question correctly, what you basically want to do, is add new option to your form builder for given type.
index.php - Basically here, we will just create a FormBuilder instance and add one field for testing purpose.
<?php
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
$formFactory = Forms::createFormFactoryBuilder()
->getFormFactory()
->createBuilder()
->add('test', 'text', array('customAttribute' => true))
->getForm()
->createView();
If we open the browser right now, we will get nice and big error, telling us that "customAttribute" is unknown option.
So, let's create custom form type! As you saw I named it TextCustomType since I will extends "text" form type.
The Type class:
<?php
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
class TextCustomType extends AbstractTypeExtension {
public function getName() {
return "text";
}
public function getExtendedType() {
return "text";
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setOptional( array('customAttribute') );
$resolver->setDefaults( array('customAttribute' => true) );
}
public function buildView(FormView $view, FormInterface $form, array $options) {
$view->vars['customAttribute'] = $options['customAttribute'];
}
}
Now, we created our custom type, so lets add it to the form factory:
$formFactory = Forms::createFormFactoryBuilder()
->addTypeExtension( new TextCustomType() ) // once the class is loaded simply pass fresh instance to ->addTypeExtension() method.
->getFormFactory()
->createBuilder()
->add('test', 'text', array('customAttribute' => true))
->getForm()
->createView();
Refresh your browser, and you should be good to go! Hope you got the idea.
Updated as per OP's suggestion.
The answer is pretty simple! It was just a matter of looking in the right place. The FormFactoryBuilder is the key:
use Symfony\Form\Component\Form\Forms;
$form = Forms::createFormFactoryBuilder()
->addTypeExtension(new MyExtension())
->getFormFactory()
->create();
This $form variable now knows about my new 'suffix' property.

Yii2: how to use custom validation function for activeform?

In my form's model, I have a custom validation function for a field defined in this way
class SignupForm extends Model
{
public function rules()
{
return [
['birth_date', 'checkDateFormat'],
// other rules
];
}
public function checkDateFormat($attribute, $params)
{
// no real check at the moment to be sure that the error is triggered
$this->addError($attribute, Yii::t('user', 'You entered an invalid date format.'));
}
}
The error message doesn't appear under the field in the form view when I push the submit button, while other rules like the required email and password appear.
I'm working on the Signup native form, so to be sure that it is not a filed problem, I've set the rule
['username', 'checkDateFormat']
and removed all the other rules related to the username field, but the message doesn't appear either for it.
I've tried passing nothing as parameters to checkDateFormat, I've tried to explicitly pass the field's name to addError()
$this->addError('username', '....');
but nothing appears.
Which is the correct way to set a custom validation function?
Did you read documentation?
According to the above validation steps, an attribute will be
validated if and only if it is an active attribute declared in
scenarios() and is associated with one or multiple active rules
declared in rules().
So your code should looks like:
class SignupForm extends Model
{
public function rules()
{
return [
['birth_date', 'checkDateFormat'],
// other rules
];
}
public function scenarios()
{
$scenarios = [
'some_scenario' => ['birth_date'],
];
return array_merge(parent::scenarios(), $scenarios);
}
public function checkDateFormat($attribute, $params)
{
// no real check at the moment to be sure that the error is triggered
$this->addError($attribute, Yii::t('user', 'You entered an invalid date format.'));
}
}
And in controller set scenario, example:
$signupForm = new SignupForm(['scenario' => 'some_scenario']);
Try forcing the validation on empty field
['birth_date', 'checkDateFormat', 'skipOnEmpty' => false, 'skipOnError' => false],
Also, make sure you don't assign id to your birth_date field in your view.
If you do have id for your birth_date, you need to specify the selectors
<?= $form->field($model, 'birth_date', ['selectors' => ['input' => '#myBirthDate']])->textInput(['id' => 'myBirthDate']) ?>
To make custom validations in yii 2 , you can write custom function in model and assign that function in rule.
for eg. I have to apply password criteria in password field then I will write like this in model.
public function rules()
{
return [
['new_password','passwordCriteria'],
];
}
public function passwordCriteria()
{
if(!empty($this->new_password)){
if(strlen($this->new_password)<8){
$this->addError('new_password','Password must contains eight letters one digit and one character.');
}
else{
if(!preg_match('/[0-9]/',$this->new_password)){
$this->addError('new_password','Password must contain one digit.');
}
if(!preg_match('/[a-zA-Z]/', $this->new_password)){
$this->addError('new_password','Password must contain one character.');
}
}
}
}
You need to trigger $model->validate() somewhere if you are extending from class Model.
I stumbled on this when using the CRUD generator. The generated actionCreate() function doesn't include a model validation call so custom validators never get called. Also, the _form doesn't include and error summary.
So add the error summary to the _form.
<?= $form->errorSummary($model); ?>
...and add the validation call - $model->validate() - to the controller action
public function actionCreate()
{
$model = new YourModel();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {...
Although it's an old post i thought I should answer.
You should create a Custom Validator Class and to create a validator that supports client-side validation, you should implement the yii\validators\Validator::clientValidateAttribute() method which returns a piece of JavaScript code that performs the validation on the client-side. Within the JavaScript code.
You may use the following predefined variables:
attribute: the name of the attribute being validated.
value: the value being validated.
messages: an array used to hold the validation error messages for
the attribute.
deferred: an array which deferred objects can be pushed into
(explained in the next subsection).
SO that means you can use messages array to push your messages to the client end on runtime within the javascript code block in this method.
I will create a class that includes dummy checks that could be replaced the way you want them to. and change the namespace according to your yii2 advanced or basic.
Custom Client-side Validator
namespace common\components;
use yii\validators\Validator;
class DateFormatValidator extends Validator{
public function init() {
parent::init ();
$this->message = 'You entered an invalid date format.';
}
public function validateAttribute( $model , $attribute ) {
if ( /*SOME CONDITION TO CHECK*/) {
$model->addError ( $attribute , $this->message );
}
}
public function clientValidateAttribute( $model , $attribute , $view ) {
$message = json_encode ( $this->message , JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
return <<<JS
if ($("#DATE-1").val()=="" || $("#DATE-2").val() =="") {
messages.push($message);
}
JS;
}
}
and then inside your model SigupForm add the rule
['birth_date', 'common\components\DateFormatValidator'],
Deferred Validation
You can even add ajax calls inside the clientValidateAttribute function and on the base of the result of that ajax call you can push message to the client end but you can use the deferred object provided by yii that is an array of Deferred objects and you push your calls inside that array or explicitly create the Deferred Object and call its resolve() method.
Default Yii's deferred Object
public function clientValidateAttribute($model, $attribute, $view)
{
return <<<JS
deferred.push($.get("/check", {value: value}).done(function(data) {
if ('' !== data) {
messages.push(data);
}
}));
JS;
}
More about Deferred Validation
You need to render the model from controller. Without initializing the model in view. And in the controller you need to call the validate function
Are you sure the first parameter of addError shouldn't be like this
$this->addError(**'attribute'**, Yii::t('user', 'You entered an invalid date format.'));
I had common problem.
In your validation function:
public function checkDateFormat($attribute, $params)
{
// no real check at the moment to be sure that the error is triggered
$this->addError($attribute, Yii::t('user', 'You entered an invalid date format.'));
}
$params doesn`t get any value at all. It actually always equals to Null. You have to check for your attribute value in function:
public function checkDateFormat($attribute, $params)
{
if($this->birth_date == False)
{
$this->addError($attribute, Yii::t('user', 'You entered an invalid date format.'));
}
}
that`s how it worked for me.
If you don't use scenarios for your model, you must mark your atribute as 'safe':
['birth_date','safe'],
['birth_date', 'checkDateFormat'],
And, on the other hand, you can use this for date validation:
['birth_date','safe'],
[['birth_date'],'date', 'format'=>'php:Y-m-d'],
You can change format as you want.
**We should set attributes to the function to work with input value **
public function rules()
{
return [
['social_id','passwordCriteria'],
];
}
public function passwordCriteria($attribute, $params)
{
if(!empty($this->$attribute)){
$input_value = $this->$attribute;
//all good
}else{
//Error empty value
$this->addError('social_id','Error - value is empty');
}
}
Are you by any chance using client side validation? If you do then you have to write a javascript function that would validate the input. You can see how they do it here:
http://www.yiiframework.com/doc-2.0/guide-input-validation.html#conditional-validation
Another solution would be to disable client validation, use ajax validation, that should bring back the error too.
Also make sure that you have not overwritten the template of the input, meaning make sure you still have the {error} in there if you did overwrite it.
Your syntax on rules should be something like this man,
[['birth_date'], 'checkDateFormat']
not this
['birth_date', 'checkDateFormat']
So in your case, it should look like below
...
class SignupForm extends Model
{
public function rules()
{
// Notice the different with your previous code here
return [
[['birth_date'], 'checkDateFormat'],
// other rules
];
}
public function checkDateFormat($attribute, $params)
{
// no real check at the moment to be sure that the error is triggered
$this->addError($attribute, Yii::t('user', 'You entered an invalid date format.'));
}
}

How to generate subform of form - Zend Framework

I have a form with all field that map an object.
Of this, i would generate a subform within of the Form class.
I'm trying to do this by the displaygroup, but when call the "subform" in the controller, the tag form, is not generate.
how can I solve ?
Thanks
this is the code.
<?php
$username = new Zend_Form_Element_Text('user');
...//param for field
$password = new Zend_Form_Element_Password('pwd');
...//param for field
$name = new Zend_Form_Element_Text('name');
...//param for field
$submit = new Zend_Form_Element_Submit('submit');
...//param for field
$this->addElement(array($user,$password,$name,$submit));
$this->addDisplayGroup(array($user,$password,$submit),'login');
$this->addDisplayGroup(array($user,$password,$name, $submit),'create');
?>
A subform is something different than a display group. A subform is a Zend_Form_SubForm instance nested in Zend_Form instance. You can use this to embed one form into another. As an example, you might have a user profile form and a registration form. In the registration form you can enter profile values as well as some other details. So, you can use this profile form as subform embedded inside the registration form. A subform is mainly used for DRY (don't repeat yourself) principles or to create a multi-page form.
A display group is just a visual representation of some form elements grouped together. In html syntax this is called a fieldset. The main purpose is to create groups of elements which belong to each other. For example in a shopping cart you might have an invoice address group and a shipping address group. Such display group is mainly used for semantics and visual representation.
On of the largest differences is that for display groups, the form has awareness of those form elements, as with subforms the form has no awareness of the elements of the subforms. This said, I notice you want to create one form which contains two display groups: one when you login, one when you create (or register) a user. With the given from above you cannot use display groups for this. One option is to use two form instances:
class LoginForm extends Zend_Form
{
public function init ()
{
$this->addElement('text', 'user');
$this->addElement('password', 'pwd');
$this->addElement('submit', 'submit');
}
}
class RegisterForm extends Zend_Form
{
public function init ()
{
$this->addElement('text', 'user');
$this->addElement('password', 'pwd');
$this->addElement('text', 'name');
$this->addElement('submit', 'submit');
}
}
If you want to reuse the fields user and pwd you might want to use subforms for this:
class BaseForm extends Zend_Form_SubForm
{
public function init ()
{
$this->addElement('text', 'user');
$this->addElement('password', 'pwd');
}
}
class LoginForm extends Zend_Form
{
public function init ()
{
$subform = new BaseForm;
$this->addSubform($subform, 'base');
$this->addElement('submit', 'submit');
}
}
class RegisterForm extends Zend_Form
{
public function init ()
{
$subform = new BaseForm;
$this->addSubform($subform, 'base');
$this->addElement('text', 'name');
$this->addElement('submit', 'submit');
}
}
In both cases, you can simply instantiated one of those forms in your controller:
public function loginAction ()
{
$form = new LoginForm();
// More code here
$this->view->form = $form;
}
public function registerAction ()
{
$form = new RegisterForm();
// More code here
$this->view->form = $form;
}
Zend_Form_SubForm doesn't render <form> tags by default.
In order to get it to do so, you need to add the 'Form' decorator to your subform instance before you render it.
Try:
$mySubForm->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
->addDecorator('Form');
and then, in your view script, you can do:
<?php echo $this->mySubForm; ?>

ZendFramework Noob: Adding text on top of a form

I've just (as in today) started working on some ZF stuff.
I have a Form that needs to have some text in a div appear at the top of the form, but I have no idea how to include it.
The structure of the form is:
class MyForm extends \app\forms\FormType {
public function init() {
// gets all the form elements of the parent
parent::init();
// A few additional form elements for MyForm created here
}
}
Any help would be apprecaited!
In your controller where you instantiate the form object just set it with the view object like this:
public function actionNameAction()
{
// ...
if (/* some condition to check form page */) {
$this->view->divText = 'your text';
}
}
Then put the div in the action-name.phtml script:
views/scripts/controller/action-name.phtml
Contents:
<?php if (!empty($this->divText)): ?>
<div><?php echo $this->divText; ?></div>
<?php endif; ?>
Additionally, you could pass the view object by reference to your form class. Just overload the construct function like so:
public function __construct($options = null, &$view)
{
parent::__construct($options);
$this->view = $view;
}
Then in your controller when you instantiate your form object do this:
$form = new MyForm(null, $this->view);
Let's go back to your form class once again and modify the init() method:
public function init()
{
// ...
$this->view->divText = 'Text set from within ' . __CLASS__;
}
Using this way, you won't have to put any conditional if statements checking anything in the controller. You're already checking if $this->divText is not empty in the view, so by passing the view object to your form class you can ensure that that text will only be set when the form is being used.