Suppress errors in Symfony 2 - forms

I try to remove some fields from form in event listener. But when I do that I get an error like this form should not contain extra fields. How can I suppress this error?
Here is my listener:
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$form = $event->getForm();
$form->remove('field');
}
});

if you want to ignore data submitted for a field you could unset it's data on "PRE_SET_DATA" or set it to current value in the entity.
Not sure why you would like to first allow the user to submit a form and then cut out part of it instead of just presenting a shorter form in the first place
you could also unset the data AND remove the field from the form on PRE_SUBMIT, but then in case vlaidation goes wrong, user gets back a different form

Related

Symfony 2.x Form Field Name

When I render a form, form Filed Name is given as an array. For example: search[item], search[keyword] etc. where search is name of the form.
I'm not great on working with forms but I think, the name should be rendered as simply, name="item" or name="keyword".
I've looked at all the documentation, customizing form rendering topic etc. but I can't find any way to change the default behaviour of Symfony form to render form filed name from 'search[item]' to 'item'.
This way, when I ask for the POST data, I can ask simply $this->getRequest()->request->get('item'), as I have to deal with lots of individual parameters.
Help would be great i) To figure out how to achieve what I want. ii) to let me know, why the name is rendered this way. is this the good practice?
Rather than accessing parameters from the Request object, you can bind the Request object to the form.
For example, in your controller method that you post your form to:
namespace Acme\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\Form\MyFormClass;
class MyFormController extends Controller
{
receiveFormAction(Request $request)
{
$form = new MyFormClass();
// you can specify that a route only accepts a post
// request in the routing definition
if ($request->isMethod('POST')) {
// this populates the form object with the data
// from the form submission
$form->bind($request);
if ( ! $form->isValid()) {
throw new \Exception('Invalid form');
}
// an array of the data the format you require
$data = $form->getData();
$data['item'];
$data['keyword'];
// etc.
}
}
}
The above is the way you should be handling forms in Symfony 2, and is how you can leverage the power that the forms component gives you, with validation etc.
Symfony supports multiple forms on a page. They might be instances of the same form or have similar field names. Having the fields for each form all together in an array makes this easy to do.

CodeIgniter repopulate form from both session data & form validation?

I have a form with a couple search options, like a checkbox array and radio button. By using the form validation library I have the form repopulating after a submit, like so:
echo form_checkbox('check_track[]', '1', set_checkbox('check_track[]', '1', TRUE));
echo form_dropdown('select_year', $options, set_value('select_year', '2013'), $attribs);
I also save all the form options (by storing the post) into session userdata. Is it possible to repopulate all the fields from the session data if $_SERVER['REQUEST_METHOD'] !== 'POST' but keep repopulating based on form validation otherwise?
The easier would probably be to separate the form generation from the value generation. In the snippet you provide, the value is read directly from the submitted form.
I would advise you, in you controller or your model to generate a data structure, each field corresponding to one of the form field.
For each, the value would either be the default, either the one stored in the session if it matches you condition ie: valid data and not after a POST if I understood you well.
I ended up just faking that a POST had happened before the form validation stuff ran to get repopulation to work:
if(!isset($_POST['something']) && $this->session->userdata('something'))
{
$_POST = $this->session->all_userdata();
}
$this->form_validation->set_rules('something', 'stuff', 'required');
.
.
.

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.

Must I re-populate Zend_Form fields after the post back manually?

After a post back, suppose validation fails and I want to show the form again with errors, I find that the form is empty, must I repopulate the form fields manually?
The method isValid populate the form field. You don't have to repopulate manually.
I find the best way to handle form processing is to use something like
$form = new My_Form;
if ($this->getRequest()->isPost()
&& $form->isValid($this->getRequest()->getPost()) {
// process form and redirect (PRG pattern)
}
$this->view->form = $form;
This way, your form is shown on the first request and if not valid, is re-shown with the submitted values and any validation messages.
In case you are using some custom isValid don't forget your form also has a populate() function.
So
$data = $this->getRequest()->getPost();
if(!$myForm->isValid($data)){
$myForm->populate($data);
}
No need to do it by hand thats for sure.
Of course dont forget to assign the same object you did the isValid - populate calls on
$this->view->form = $myForm;
After the checks.

Zend Framework: isValid() clears values from disabled form fields!

When you submit a form, disabled form fields are not submitted in the request.
So if your form has a disabled form field, it makes working with Zend_Form::isValid() a little frustrating.
$form->populate($originalData);
$form->my_text_field->disabled = 'disabled';
if (!$form->isValid($_POST)) {
//form is not valid
//since my_text_field is disabled, it doesn't get submitted in the request
//isValid() will clear the disabled field value, so now we have to re-populate the field
$form->my_text_field->value($originalData['my_text_field']);
$this->view->form = $form;
return;
}
// if the form is valid, and we call $form->getValues() to save the data, our disabled field value has been cleared!
Without having to re-populate the form, and create duplicate lines of code, what is the best way to approach this problem?
Are you setting the element to disabled so that the user can't edit it's contents but only to see it? If so, just set the element's readonly attribute to true, I think it'll work that way.
I use a custom class inherited from Zend_Form. Class adds some features and solves this problem by replacing the method isValid as follows:
class Murdej_Form extends Zend_Form {
function isValid($data) {
$_data = $this->getValues();
$valid = parent::isValid($data);
$this->populate($_data);
return $valid;
};
};
instead of using isValid() we can use isValidPartial(). Unlike isValid(), however, if a particular key is not present, it will not run validations for that particular element. So, isValidPartial() will not validate for disabled fields.
Reference: ZF Documentation