odd Zend_Form_Element_File behavior - zend-framework

I have the following element in my form:
$attachment = new Zend_Form_Element_File('attachment');
$attachment->setLabel('Attach File (2MB Max)');
$attachment->addValidator('Count', false, 1);
$attachment->addValidator('Size', false, 2048000);
If I print the entire form in the view, this field works fine. However I have some very custom HTML that I'm using for display, so I am echoing out each form element like this in my view:
<?=$this->form->attachment?>
This works fine for other fields. However for this file field. Zend_Form validation breaks with the message The file 'attachment' exceeds the defined ini size However I am not including any file upload in my post, and even if I do, it still errors with the same message.
Any ideas?

Are you doing the right kind of EncType? It should be 'multipart/form-data'. If it's not, the file element's key might not be getting put in the $_FILES array and the file element interprets that as the file was too big to be uploaded to the server.

Mmm if it says ini size, have you checked the upload_max_filesize directive in your php.ini?

check the view of the form, maybe you are rendering the form partially, try to put this on the view of your form:
<?=$this->form?>
(this worked for me)

only form?> will work fine with uploading any file.
but problem occurs when we try to put partial form as : form->file ?>
form->file ?> it will always show error so try to implement full form and avoid putting elements of form.

Related

cakephp element not displaying

I have a number of form inputs inside an element as they are repeated in other forms, however it does not output the content of the element when i include it in a view. I can place "Die();" within the element and it will show everything up until that point including the inputs within the element. When checking the source code (without the "Die();") it seems to have disregarded everything within the element including formatting such as DIV's.
The only thing i can think is that cakephp does not like you setting a form in a view then including inputs to the form using elements, does any one know if this is the case, or know the actual reason why this is happening?
The answer is i'm an idiot and forgot to echo the element.... yeah.

Disable escape in Zend Form Element Submit

I can't disable escaping in a Zend_Form_Element_Submit, so when the label has special characters it won't display it's value..
This is my actual Zend Form piece of code:
$this->submit = new Zend_Form_Element_Submit('submit');
$this->submit->setLabel('Iniciar Sesión');
$this->submit->setIgnore(true);
$this->addElement($this->submit);
I've tried $this->submit->getDecorator('Label')->setOption('escape', false); but I obtain an "non-object" error (maybe submit isn't using the "Label" Decorator)..
I've also tried as suggested $this->submit->setAttrib('escape', false); but no text will be shown either.. Any idea? Thanks
Should be as simple as doing this:
$element->addDecorator('Label', аrray('escape'=>false));
Or see setEscape(). http://framework.zend.com/manual/1.12/en/zend.form.standardDecorators.html
Regarding failure to retrieve named decorator... Try getDecorators() Do you see 'label' in the results?
There is no Label decorator for submit form element by default (this is why you get the error).
The $this->submit->setLabel('Iniciar Sesión'); value goes to Zend_View_Helper_FormSubmit, which always does escaping and uses the label as a value.
The helper used by the Submit element escapes by default. Unlike with the label decorator, submit labels are included in a HTML attribute so they need to be escaped.
Your label - Iniciar Sesión - is a perfectly valid UTF-8 string, so the escaped version of it will be the same. If your label is not appearing then something else is going wrong. I'd guess that your page is being served using a charset that doesn't match what Zend View is using (UTF-8 by default).
View the page source to see what actually gets output - that might give you some more clues. Alternatively if the page this form is on is public, if you can provide a URL we might be able to spot the issue.
I ran into a similar issue. In my instance, I added both a label and a description to a text field element. This line of code allowed me to turn off the html escaping for the description attached to that field element:
$form->getElement('txtUPC')->getDecorator('description')->setOption('escape', false);
In my testing, the setEscape() was not recognized by the form elements or their decorators.

symfony2: setting the value of a form field outside the form, inside a controller action

I need to set the value of a symfony2 form element.
I use a doctrine2 entity, a Symfony\Component\Form\AbstractType and the createForm() method inside my Controllers Action.
$saleDataForm = $this->createForm(new SaleType(), $sale);
Now, how do i get an element from that form, and how can i set it's value?
I want to do something like this, but it doesn't work:
$saleDataForm->get('image')->setValue('someimapge.jpg');
FYI: I need to do this to render the field correctly (using this approach, my image field is always empty and i need to set it to the content of imagePath to present a preview of an uploaded image)
For a more exact answer you should include the entities you use in this form so we can see the getters and setters. But based on your question this should work:
Inside the controller do this:
$saleDataForm->getData()->getImage()->setValue('someimage.jpg');
$form->setData($form->getData());
This is if the form is already created so:
$saleDataForm = $this->createForm(new SaleType(), $sale);
$saleDataForm->getData()->getImage()->setValue('someimage.jpg');
$form->setData($form->getData());
To get the data use this:
$saleDataForm->getData()->getImage()->getValue();
thanks MatsRietdijk, you helped me, but I had to change code to this
$form = $this->createForm(new SaleType(), $sale);
$form->getData()->setImage('someimage.jpg');
$form->setData($form->getData());
If you try fill file upload field - it's wrong idea. Ask yourself: what data should be there? If an user upload an image, then in this field will be a path to the image on his local system. But then after file is uploaded you change its name and location, so you don't need info about this path and you just don't keep it.
The appropriate way should be left empty upload field below (or above, or wherever you have it ;) ) image. Then after form is submitted and if the field is not empty you should change edited image.

silverstripe backend linebreaks in content do not show up in frontend

I have a textareaField in Silverstripe Backend in Edit Page View... The text to insert contains linebreaks. If I save the Page the text shows correctly with linebreaks in the textareaField. The linebreaks are for sure saved correctly to the database. But how do I display the text correctly in the frontend? It´s always outputted without linebreaks in a single line.
I tried already $Text.RAW, $Text.XML,... nothing works.
Thanks for the help,
Kind regards,
Florian
Assuming that you are using 3.0 this is a bug. You can see it here http://open.silverstripe.org/ticket/7596
A work around is to write your own function calling nl2br on your text field.
Here is an example:
public function NiceDescription () {
return (nl2br (Convert::raw2xml ($this->Description), true));
}
You can replace "Description" with the name of your text property.
Then in your template file if you need to display the description field you will call the function:
$NiceDescription
to visually render the newlines in html, you need to convert them to <BR> tags.
see http://php.net/manual/de/function.nl2br.php

Line breaks in Zend Navigation Menu labels

I have a need to create a <br/> tag in the display label for a menu item generated using Zend_navigation, but don't seem to be able to find a way to do so.
My navigation item is defined in the XML config as:
<registermachine>
<label>Register your Slitter Rewinder</label>
<controller>service</controller>
<action>register</action>
<route>default</route>
</registermachine>
I want to force a tag in the output HTML between 'your' and 'slitter', such that it appears on two line as below:
Register your
Slitter Rewinder
However, I can't seem to do it. obviously using in the XML breaks parsing, and using html entities means that the lable is displayed as:
Register your <br/>Slitter Rewinder
Has anyone had experience of this that can offer advice?
Thanks in advance!
there is no such option built-in you have to use a partial
$partial = array('menu.phtml', 'default');
$this->navigation()->menu()->setPartial($partial);
echo $this->navigation()->menu()->render();
http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.navigation.menu
you may also try a hack with <label><![CDATA[Menu label<br/>Second line]]></label>
I found a (hacky) solution:
I updated my navigation.xml to use {br} tokens wherever a <br/> tag is required, and then amended the base Zend/View/Helper/Navigation/Menu.php file as follows:
within htmlify function, changed
$this->view->escape($label)
to
str_replace("{br}", "<br/>", $label)
I could (and probably will) override the Zend Library Menu View Helper with my own at some point, but this at least cover it for now.
there is a escapeLabels boolean used to convert html tags and it's true by default.
You can set your navigation like this
$this->navigation()
->menu()
->escapeLabels(false)
->...
http://framework.zend.com/apidoc/2.0/classes/Zend.View.Helper.Navigation.Menu.html#escapeLabels