adding an attribute to form field without overwriting defaults in symfony2 - php

I need to add an attribute to a form field in symfony.
I do it like this in my form type:
->add('myfield','text',array('attr'=>array('myattrib'=>"test")))
But this overwrites defult attributes of that field (e.g. classes)
How can I add an attribute to a field without overwriting other attributes?
Thanks

you can add it using twig if you wanna just to keep your php code clear, here is an exemple :
{{ form_row(form.Address,{
'attr':{
'class':'form-control',
'min-length':'4',
'required':'true',
}
}) }}

In your form add the attribute like this :
->add('myfield','text',array('attr'=>array('myattrib'=>"test"),'mapped'=>false))
Otherwise you can add it in your twig view when rendering you form with form_row() just add the input like normale html and get int in your action after posting like this :
$posted_value = $this->get('request')->request->get('Name attribute of your input')

Related

laravel - get attribute from html tag

So I have a form in my view:
{{Form::file('projectPicture', ['class' => 'uploadedImage', 'data-some-attribute' => ''])}}
with the attribute data-some-attribute.
And in my route I retrieve it like so:
$request->file('projectPicture');
How do I get a data-some-attribute in the route? Is it even possible?
I know I can use ajax to pass any data, but can it be avoided in this case?
Thank you!
It is not possible how you intend it to work, only because it's not how form data is working under the hood. The second argument in your sample Form::file is just decorating the rendered form element. It has no correlation with the form data that is transferred between the server and client.
For all intents and purposes form data is just a glorified set of key value pairs. If you wanted to pass some-data-attribute to your route controller, you have two options -
Add another form field, and make it empty using Form::hidden. In this case, you would just name the field some-data-attribute.
If your form is submitted through a POST method, you can tack on some-data-attribute onto the form's route and retrieve it from the request.
ie - your/route becomes your/route?some-data-attribute=whatever, and you can retrieve it later with something like $request->input('some-data-attribute').

Symfony2 Forms GET submit field type - remove 'name' param

I'm using submit form type in my FormType class (I want it to be customized for css classes and button label).
Since it's a search, I decided to go with GET, not default POST. What I noticed in Symfony2, is that:
submit field gets a "name" parameter
and when form is submitted, one of params in url is an empty "submit" (i.e.: http://url....?phrase=searchphrase&submit)
I tried to remove auto-genarated HTML "name" attr from the "button / submit" HTML tag in controller, in form type class, and even in Twig template, e.g. by overriding "name" - nothing seems to work for Symfony2, and the name="submit" for this button is always generated.
It there a way to remove this HTML attr from submit button, or am I forced to only render whole submit button by myself, and remove it from Form Type class?
Template for the search form is very basic, nothing extraordinary for Symfony2:
{{ form_start(form) }}
<div>
{{ form_widget(form.target) }}
</div>
<div>
{{ form_widget(form.phrase) }}
{{ form_widget(form.submit) }}
</div>
{{ form_errors(form) }}
{{ form_end(form) }}
And for the form type, I use standard submit:
$builder->add('submit', 'submit', array('label' => 'search', attr(name => null) ....)
As you see, here name attr is explicilty set to null. Symfony2 generate it any way, and give it a "submit" value to this 'name' attr.
The only problem is: can I make somehow Symfony2 to NOT generate "name" attr in the HTML Button/Submit tag? If Symfony2 cannot do that, it seems that the only way is to simply remove "submity" from Form Type class, and put HTML for this button directly in the template by myself, which I'm trying to not do (but if it's not possible, I will have to).
To me, it seems like it's a problem with Symfony2. Submit type is quite new thing in forms here, and I can imagine that auto-generated "name" attr follows the same rules as other form types in Symfony2 - although it is really not needed in HTML forms!
SOLUTION: So, I ended up with rendering it all by myself, and removed it from Form Type. Still I think Symfony2 shouldn't generate 'name' for this particular tag - I saw never ever "name" attr being assigned to SUBMIT button HTML tag in any form on the web, it's obviously not needed and not desired.
I think there are three solutions:
Have a form without the submit button and create the button outside of the form. Then submit the form with JS.
Override the default twig template responsible for generating HTML content of the submit button.
Generate the HTML content by yourself and not use the name there.

Zend Form Element Label "for" Attribute

I subclass Zend_Form to allow re-use as I describe in my other SO question. It's working very well, except for one issue that I found. In my view script I use this code to render the label for fields:
echo $this->formLabel($this->element->getFullyQualifiedName(),
$this->element->getLabel());
The rendered label has the original element id as the value in the for attribute rather than the new, suffixed, element id. Is there a bug in the Zend code, am I missing a step or doing something incorrectly?
I think the reason is that you use formLabel view helper independently. As a result, the helper is not aware of any attributes that you specified for your input text field. So, you should provide these attributes to the formLabel. For example you could do the following:
echo $this->formLabel(
$this->element->getFullyQualifiedName(),
$this->element->getLabel(),
$this->element->getAttribs()
);
The above code should produce for tag that matches your input elements id. Otherwise, the for tag will be set to the elements name.

How to hide a field in a Drupal form

I have a specific content type in drupal6. I want to implement a hook, which hides the body field of that content type from the add form, but not from the edit form. How can I do that?
You can use hook_form_alter. Which you can programmatically alter the contents of the form api build. This gives you the full $form array of which you can simply unset($form['the_field_you_dont_want']);.
But the easier way to get rid of the body field is in the edit content type there is a field labelled 'Body field label:' just leave this blank and the body field will be omitted.
unset seems to destroy the value as well, as does the #access property.
I just use this to hide a field (in this case a reference if it was preset using the URL:
$form['field_reference']['#prefix'] = "<div class='hide'>";
$form['field_reference']['#suffix'] = "</div>";
The solution found here https://drupal.stackexchange.com/questions/11237/hide-field-in-node-add-page works great for me. Here I am repeating moon.watcher's solution:
function test_remove_filed_form_alter(&$form, &$form_state) {
if (arg(0) == 'node' && arg(1) == 'add') {
$form['field_test']['#access'] = 0;
}
}
The disadvantage of using unset() is that it will entirely remove the field and you can't further on like, for example, on node presave. In my case, I just wanted to remove the field from the form on the first moment, but I wanted to populate it later on, prior to saving the node. The solution on the link above works perfect for me for this reason.
Did you implement the content type within a module (using hook_node_info)? If so, set the has_body attribute to false.

Is there a built in way in Zend-Form to hide the value of the SUBMIT input field?

If I build a form:
$search_words = new Zend_Form_Element_Text('text');
$search_words->setRequired(true)->setDecorators(array(array('ViewHelper')));
$form->addElement($search_words);
$go = new Zend_Form_Element_Submit('gogogo');
$go->setDecorators(array(array('ViewHelper')))
->setIgnore(true);
$form->addElement($go);
With method GET.
I will see in the URL gogogo=gogogo. If I was writing the markup myself, I simply wouldn't give the submit any [name] attribute and that would have solved that. Trying to set the name of a submit to '' won't work (either throws an exception or is being ignored, depends on the way you do it).
Any (built in) ideas?
Another possibility would be to disable the submit button before the form is submitted:
$go->setDecorators(array(array('ViewHelper')))
->setIgnore(true)
->setAttrib('onclick', 'this.disabled = true');
This way, the value of the submit button will be ignored upon submitting the form.
There are a few possible options:
Use a custom decorator to build the markup, so a name attribute is not specified
Use a string replacement function on the markup returned by Zend_Form's render methods, to remove the attribute
What I often do, as follows
I usually add a custom route so that either of the following is valid:
domain.tld/search/keyword
domain.tld/search?q=keyword
Then you can use javascript to redirect to the cleaner form of the URL, taking care to urlencode the keyword field
Most of your users will see the cleaner URL this way.

Categories