Customize Row's ID attribute in Twig - html

Is it possible to customize HTML 'name' attribute in Twig when rendering a widget with {{ form_widget(form.NAME_OF_THE_FIELD) }} ?
Passing { 'name': 'my custom name ' }) } doesn't work...
Or {'attr': {'name': 'SOMETHING'}} also doesn't work...
Thanks for help!

The "name" is stored in the field vars under the "full_name" key so to customise it in your Twig file you can use..
{{ form_widget(form.NAME_OF_THE_FIELD, {'full_name': 'A DIFFERENT NAME' }) }}
NOTE
Changing the automatically generated "name" of a field wouldn't be recommend as, due to it not being expected by the form, it will not be picked up when submitting your form data.

Related

the type attribute is not working for form_widget

All the attributes work perfectly fine except the "type" attribute
I want to set the type of input(form_widget) to 'password' but it's not working! (the password shows up, it's not covered )
here is the code of my twig page :
username : {{ form_widget(form.username, {'attr':
{
'class': 'input100',
'placeholder': 'Username',
'name':'_username',
'type': 'text'}
}) }}
password : {{ form_widget(form.password, {'attr':
{
'class': 'input100',
'placeholder': 'password',
'name':'_password',
'type': 'password'}
})}}
you can set up the type from the Form Class you can found it in thr folder
src/Form/
->add('username', TextType::class)
and import the class like this
use Symfony\Component\Form\Extension\Core\Type\TextType;

How to add data attribute without value in react create element?

I am adding div to the DOM as below,
React.createElement("div", { className: "test"});
The rendered html should be as below
<div class="test" data-test>
</div>
Where can I add data-test in the createElement?
If you refer to the doc on createElement API, you can insert the value to your data-test prop like this:
React.createElement('div', {className: 'test', 'data-test': 'some value'});
You can't do that because attribute without any value means that this attribute has value true in JSX. Just don't pass data-test and this.props['data-test'] will be undefined in your component. Or if you need to have this attribute with empty value then just add it to your default values in component definition.
defaultProps: {
'data-test': ''
}

Passing value from Django view to AngularJS inserted template

Is there a way to pass a value from a Django view to an AngularJS inserted template?
In my view.py I have the code:
count_users = Profile.objects.filter(user_id__gte = 0).count()
context_dict = {'users': count_users}
return render_to_response('dashboard.html', context_dict)
In dashboard.html I am able to insert the user count into the html as follows:
{{ users }}
This works fine but dashboard.html uses AngularJS to insert some more html as follows:
<div ui-view class="fade-in-up">
</div>
Mt problem that the html file inserted by AngularJS does not respond to the:
{{ users }}
Is there a way to pass the value of users through to the AngularJS inserted HTML?
using ng-init you can attach your value into the $scope
<div ui-view class="fade-in-up" ng-init="User='{{user}}' " >
</div>
In your javascript do:
mainModule
.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('{$');
$interpolateProvider.endSymbol('$}');
});
This way, if you want to bind an angular variable in the html, you use:
{$ variable $}
If you want to add a Django variable, you use:
{{ variable }}
And your code may work if you leave it as it is, and just add this configuration to the angular module.

Database to checkbox feild in django

How to create a checkboxs field in html with django as backend and how to get the value of the selected checkbox back to the view for more processing
It's simple:
class Sample(models.Model):
checkbox = models.BooleanField(blank=False, null=False, default=True)
In the template you just simply
{{ object.checkbox }}
https://docs.djangoproject.com/en/1.6/topics/db/models/

How insert raw HTML to label?

Is there some easy way how put raw HTML tag to label? I have this:
{{ Form::label('firstName', 'First name <em>*</em>', array('class' => 'input_tag')) }}
and it produces:
<label class="input_tag" for="firstName">First Name <em>*</em></label>
BUT tag EM is not interpreted as it should be. What I want is:
<label class="input_tag" for="firstName">First Name <em>*</em></label>
use HTML::decode():
{!! HTML::decode(Form::label('firstName', 'First name <em>*</em>', array('class' => 'input_tag'))) !!}
Using sprintf in a macro is much faster than redecoding:
Form::macro('rawLabel', function($name, $value = null, $options = array())
{
$label = Form::label($name, '%s', $options);
return sprintf($label, $value);
});
You can create a helper function (macro) just like:
HTML::macro('raw', function($htmlbuilder) {
return htmlspecialchars_decode($htmlbuilder);
});
in your view:
{{ HTML::raw(Form::label('input_firstname', 'Prénom <sup>*</sup>')) }}
I often use raw html for form input and labels as I find it easier to read and use html5 attributes such as required and placeholder. This form is a good example of how to use raw html with Input::old which allows you to capture old input. https://github.com/Zizaco/confide/blob/master/src/views/login.blade.php
For required inputs, instead of trying to add HTML into the label, I add a class, 'required-input' (or something).
Then I have the following CSS rule
label.required-input:before {
content: ' * ';
color: #f00;
}
This would work unless you are needing to have the <em>*</em> for screen readers. But you can still add the required flag on the input.
Got it, it's using the e() helper function that uses htmlentities() to format your labels and this is converting your tag to &lt;em&gt;*&lt;/em&gt;.
The quick and (very) dirty fix is to add this to your start.php or some other place before the first call to Helpers.php:
function e($value) { return $value; }
But this far from ideal.
I believe the Form::label($name, $value, $attributes, $escape_html) takes a fourth parameter which tells it whether to not to escape html. It defaults to true. So if your expected result is an italic *, pass false as the fourth parameter.