Zend form change elements required options to false at runtime - zend-framework

I have a form and having two file upload elements. it is like
$data_file_one = $this->createElement('file','data_file_one');
$data_file_one->setRequired(true)
->addValidator('Extension', false, 'csv')
->setDestination($filepath);
Both are set to required true. I use the same form for new post and edit posts. When it is used for editing the file upload should not be mandatory and must be set to required false. So, I need to change
setRequired(true) to setRequired(false)
How can I do when edit action is called to load form and change this element option?
Thanks in advance.

Zend_Forms have a method called getElement that allow you to retrieve an element from a form by its name. This gives you the ability to modify an element's default value before rendering it to the user.
For example, to change a field from being required to being optional, you can do the following:
$form->getElement('data_file_one')->setRequired(false);

Related

Seperate error messages for required TextFields and DropDownChoices

I'm currently working on a Wicket 6.20 project which already has dozens of pages with TextFields and DropDownChoices. Now the requirement came up to change the default '${label}' is required. message to something more specific, depending on whether a TextField or a DropDownChoice doesn't have a value.
I know I can put Required=My Text in a properties file of the application, but that changes the message for all FormComponents. And specifying the full component path to either the TextFields or the DropDownChoices in the form of myform.mycontainer.mydropdownchoice.Required=My Text isn't feasible, since of course the structure of the Forms can differ on each page.
Ideally I'd like to put something like
org.apache.wicket.markup.html.form.DropDownChoice.Required=Please select a value for '${label}'
org.apache.wicket.markup.html.form.TextField.Required=Please enter a value in '${label}'
in my application's property file, either with or without FQCN.
Is there a way to achieve this with any of the default IStringResourceLoader that come with Wicket?
It is not necessary to provide the complete path to the components. It could be a path with just few parents:
mycontainer.mydropdownchoice.Required=My Text
mydropdownchoice.Required=My Text
If this still is not an option then you can override org.apache.wicket.markup.html.form.FormComponent#reportRequiredError() for any instance or type (e.g. MyDropDownChoice).

InfoPath 2010 How to avoid validation when a section is hidden?

I am trying to design an info path form that links to a share point list.
I have a "extra" section on the form which is only visible if a check box value = true.
The problem is some of the fields on the "extra" section is mandatory. So when the check box value = false, the section is hidden and I cannot fill in the fields. But when I try to submit the form I get an error message to say mandatory fields must be completed. But I do not want to complete those fields.. as the section is to be kept hidden.
Any suggestion on how to get this working would be very helpful thanks.
Do not add the required field checkbox instead create a validation rule that checks if your value is true and the hidden field is empty then send a required error.
This way you can submit the form.
put a sample value on that field like "none" then when the section is checked, set a rule that will clear that field.

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.

Zend: Form Validation After AJAX

I have a form done with Zend. I load it with ajax in a dialog. It has 2 selects. Depending on what is selected in the first select, it loads the content of the second one. However, when I submit the form I get a validation error because the options of the second form weren't there at the time of creating it.
Is there a way to fix this "issue"? It does what it needs to do but I don't want it to verify that field anymore. Any way to specify that I don't want that?
You can disable the inArray validator. When constructing the form's select element, set
'registerInArrayValidator' => false
Also, a different solution would be to overload the isValid method, inspect the selected option for the first select element and then populate the options for the second element. Then call parent::isValid to check if the form is in fact valid or not.

Zend Framework: How do I set a custom validator on a form element?

I'm writing a custom validator that checks that at least one field has a value. I want to validate that either 'namefield' or 'idfield' has a value. One of them can be empty, or both can have a value, but at least one of them must have a value.
$nameField = new Zend_Form_Element_Hidden('namefield');
$nameField->setValue($this->nameFieldValue)
->addValidator('AtLeastOneHasValue', false, array('idfield'));
From what I understand, my validator will not validate unless I set my form element to required.
->setRequired(true)
But if I set it to required, it automatically validates that it is not empty, and the error message says that it is empty. I want to allow the field to be empty, and validate multiple fields with my custom validator. How do I validate a form element with my custom validator, without setting the form element to required?
Check this documentation page for the setAllowEmpty() method. This should help you get where you are trying to go.
setAllowEmpty($flag) and
getAllowEmpty() allow you to modify
the behaviour of optional elements
(i.e., elements where the required
flag is false). When the 'allow empty'
flag is true, empty values will not be
passed to the validator chain.