Symfony2 access all form errors without form_bubbling - forms

I want to know if it is possible to access all of my form's errors without using form_bubbling since it gives the error to the parent and individual objects loose their error...I need both the form to know globally if there are errors to display a global "please fix your errors" message and the individual elements to know if they contain errors because I will add an error CSS class to invalid form inputs.
Thanks in advance!

Most efficient way I found to get the numbers of errors of a form, regarless its form_bubbling is trueor false is to add, in the controller, some variable indicating it :
return $this->render('Acme:Contrats:index.html.twig', array(
'myform' => $form->createView(),
'myformHasErrors' => !$form->isValid(),
));
If anyone find another one, please comment/answer this.

After speaking to people in Symfony's IRC channel, there exists two ways to do this in a Twig template:
form.has('errors') for a boolean stating whether or not the form contains errors.
form.vars.errors|length for the number of errors contained in the form.
Problem solved. All of this can be used without using error_bubbling.

Related

Displaying Float on Input Forms

I'm fairly new to the CakePHP game and need some insight on how to resolve a seemingly simple issue. I've inherited a few internal websites at my new job that utilize the framework and am struggling to tackle this issue with Google alone.
What I have is a view that loads a form and populates it with data already in the database. There are three fields that have this behavior and all have the same issue.
The fields are declared as such on the View:
echo $this->Form->input('hotel_costs');
echo $this->Form->input('misc_costs');
Within the database they're declared as float DataTypes and therefore, when loaded to the browser will sometimes display as floats do: (15.7 becoming 15.699999999...)
Looking around it seems there are Helpers such as NumberHelper able to tackle my issue but I have no clue how to include the functionality with the form values. Am I looking in the wrong place entirely?
Thanks!
It has nothing to do with forcing the input type to text. Are you saying that you don't want the float's value in the field, but you want a rounded (to how many decimal places) number instead? It would have to apply across the board. Do you have a custom view or a baked one?
(I'd ask these as comments to your question, but my rep aint high enough.)
you can try to force input[type=text], so that cake doesn't try to guess input type.
$this->Form->input('hotel_costs', array(
'type' => 'text'
));
$this->Form->input('misc_costs', array(
'type' => 'text'
));
if this still doesn't work, post back with more details.
Which Controller Action(add/index/edit)?
Code of that action
var_dump() of data from the database

Zend hidden elements: hide html values

I am facing a special scenario here on some of my forms.
I have settled a permission system over some fields many of which are required.
When removing the permission to view the field on a form, I set:
$field->setDecorators('disableLoadDefaultDecorators', true);
The problem in that case is that I get prompted with the validation error over the required field, which is logical.
The other option would be to set the $field to hidden but the issue turns to become an html problem where any person can retrieve the hidden value through the source code.
Hopefully someone can offer me a suggestion on how to hide the element from the form and metadata but return it on form validation as if it was displayed.
Thank you in advance!
Change the field validation rules so that it is not required:
$field->setRequired(false)->setDecorators('disableLoadDefaultDecorators', true);

How do I get a regular Checkbox in a Zend Form?

I have a form in Zend_Form that needs some checkboxes and I'd like them to be regular old checkboxes. You know, you give em a name and a value. If they are checked your post data contains name=>value.
Zend_Form is generating two inputs fields. One, the checkbox with a value=1 and the second a hidden input with a value=2. Both have the same name. I understand in theory how Zend expects the checkbox to work, but that's not how I expect it to work and it's not how I want it to work. How do I get my old fashion HTML checkbox back?
I have tried using $this->createElement, $this->addElement and creating a Zend_Form_Element_Checkbox manually. None allow me to set the checkbox's value and all generate the hidden input.
The final and REALLY correct answer is to add an option to the element :
$this->addElement('checkbox', 'my_element', array(
'label' => 'My Element Label',
'name' => 'my_element_name',
'disableHidden' => true
));
Zend_Form_Element_MultiCheckbox is what you're looking for.
The standard Checkbox element is meant to represent "yes/no" scenarios.
You could extend Zend library and add your own custom form element to render it just like you expect it. I did it for having a date field and it worked just fine.
I wonder why that does not work for you. You can set the values to anything you want (setCheckedValue() and setUncheckedValue()). So the only difference to normal checkbox is
if (null == $this->_getParam('checkbox', null)) {
//vs.
if ($unchecked == $this->_getParam('checkbox')) {
What exactly are you trying to do?

I'm not specifying the form action but it (automatically) gives different values in some cases

I'm creating my form using the Form helper, so the action of the form is specified automatically....
this form is used for editing a post..
so, the URL has the structure: mywebsite.com/posts/edit/id
and the form's action should be automatically generated as posts/edit/id
but the problem is, in some cases, I open the HTML code and I find that the form's action is only posts/edit without the id which causes the update to fail...
I spent a lot of time to figure out what situation brings this wrong action:
i'm generating fields dynamically (using javascript & ajax) depending on the post's category..
when the value of one of the dynamically generated fields is invalid, the generated action becomes posts/edit !!
I really need help, cuz I don't know why this is happening !!!
and I don't wanna waste more time digging into the core of cakephp...
so, if any of cakephp experts has an idea about this, plz help me !!
thank you in advance !
Use the url parameter, which allows you to explicitly define a url for the form:
echo $form->create('Post', array('url' => $html->url(array('action'=>'edit', $id))));
It sounds like $id probably isn't getting set, because it should be getting passed along if it is. You need to make sure it's set to edit the record in question. Make sure your javascript is including the hidden field with the record's id in it.
Normally done like this, with the form helper: echo $this->Form->input('id');
Also, if one of the fields is invalid, the form shouldn't actually be submitting properly, if you are using cake's validation, so this is to be expected.

How to show just one (e.g. the first) error message to a form field with Zend framework where validation has failed?

Is there any way to do that with a decorator or do I need to dig deeper into ZF?
If you have multiple validators attached to one form element and want only the first error message to pop up, you can set breakChainOnFailure option to TRUE for each validator. In this case, if one of the validators fails, all the subsequent validators are skipped.
$element->addValidator(
new Zend_Validate_StringLength(array('min' => 6, 'max' => 12)),
true)
->addValidator(new Zend_Validate_Alnum());
if $foo is in an instance of Zend_Form and there is an form element username inside this form then
$foo->username->getMessages() ; will return array of errors messages to show only one you simply need to do array_pop($foo->username->getMessages()); . Basically zend decorators uses error view helper to display the messages . You need to extend the default decorator and remove the view helper simply by echo
There are many ways you can do this. My personal favorite is to extend the validator you want to use to only return one custom message. For the email validator this is especially helpful since it shows some crazy error messages that you may not want to show anyway. You can see this method here: http://clintberry.com/2010/07/zend-form-email-validator-customizing-error-messages/