Mediawiki - Transfer of empty attributes from form to template - forms

I got a Mediawiki-form with different attributes. If i got an optional text field which keeps empty most of the time. My problem is that even when im filling my text field with an empty string by default it will not be transferred to the template and generates ugly artefacts (a missing reference instead of a blank field).
Is there any way to make sure the field is transferred to the template?
Excerpt from my form:
! Priority:
|{{{field|Priority|input type=category|default=High|top
category=Priority|height=85|hideroot=on}}}
|-
! Date:
| {{{field|Date|input type=datepicker|default= {{CURRENTYEAR}}/
{{CURRENTMONTH}}/{{CURRENTDAY}} }}}
! Comment:
|{{{field|Comment|input type=text|default=" "|height=85|hideroot=on}}}
|}
It generates the template:
{{Template
|Priority=High
|Date=01/01/1970
}}
But it doesnt generate:
{{Template
|Priority=High
|Date=01/01/1970
|Comment=
}}
I tried this if-check in the template but if the attribute is null it shows {{{Comment}}}:
! [[Attribut:Comment| Comment]] | [[Comment::{{{Comment}}}
|{{#if:{{{Attribut:Comment}}}|{{{Comment}}}| }}]]

The problem is that blank. You can set the default to >non< and check is the comment string >non<.
! Comment:
|{{{field|Comment|input type=text|default="non"|height=85|hideroot=on}}}
|}
A other solution is to use ParserFunctions especially the if functionality
{{#if:{{{1|}}}|You entered text in variable 1|There is no text in variable 1 }}

Related

How to display the title of the node in plain text in TWIG on Drupal 8?

I created TWIG for my node, my products, my groups and my stores.
I'm on Drupal 8. For my nodes, I want to display the title. I use the following code:
{{ label }}
But there is the markup inside and I do not want it. So I tried the following code and it works. But is it a good practice ?
{{ label.0 }}
How to display the title of the node in plain text in TWIG on Drupal 8 ?
When using the node.html.twig or a child of that template, you will have access to the node variable, which is a limited implementation of the node object.
From the comments for the classy theme node.html.twig file:
* - node: The node entity with limited access to object properties and methods.
* Only method names starting with "get", "has", or "is" and a few common
* methods such as "id", "label", and "bundle" are available. For example:
* - node.getCreatedTime() will return the node creation timestamp.
* - node.hasField('field_example') returns TRUE if the node bundle includes
* field_example. (This does not indicate the presence of a value in this
* field.)
* - node.isPublished() will return whether the node is published or not.
As is described in that comment, the value of the node title without any markup can be accessed in twig by using:
{{ node.label }}.

Displaying a DateInterval in Twig

I'm trying to display a DateInterval in Twig using the following code:
{{ event.endTime.diff(event.startTime)|date("i's''") }}
where event is an Entity who get 2 DateTime object: endTime and startTime. With that command I've got this output:
i's''
Instead of min'sec'' like 08'15''
It is said in the date doc that
The date filter accepts [...] DateInterval instances
This work to display min and sec from a date object.
Note that doing: {{ (event.endTime.diff(event.startTime))|date("i's''") }} doesn't change anything
I also tried {{ date(event.endTime.diff(event.startTime))|date("i's''") }} but this lead to an exception Object of class DateInterval could not be converted to string
I've also seen time_diff from Twig Extensions but this return a string (in or ago) instead of a Date object, then I can't display it as I want.
Let me know if you need more informations. Thank you for your help.
As #Yoshi said:
(new \DateTime('tomorrow'))->diff(new \DateTime('now'))->format("i's''") also gives "i's''", so I'd say this is not a twig-thing. From the manual:
"The following characters are recognized in the format parameter
string. Each format character must be prefixed by a percent sign (%)."
So to fix my proble I just had to do:
{{ event.endTime.diff(event.startTime)|date("%i'%s''") }}
When putting in the method format the parameters 'i' and 's' in lowercase, when the value to be displayed is less than 10, put a single digit.
Example if you need to display the time of 5 minutes and 2 seconds by showing a leading zero (05:02). You have to capitalize the parameters, it would be like this:
{{ event.endTime.diff(event.startTime)|date("%I:%S") }}
The allowed parameters can be found in: DateInterval format

how to display particular fields in form Symfony 2.6

I need display particular fields in form in Symfony 2.6. I use a class Form. I have the folowing fields: name, email, message, send, recet. I need display all of them except recet .I try like this:
{{form_start(form)}}
{{ form_errors(form) }}
{{form_row(form.name)}}
{{form_row(form.email)}}
{{form_row(form.message)}}
{{form_end(form)}}
But, it's displaying all fields in form, it is not what I want. Even if i leave only {{form_start(form)}} and {{form_end(form)}} - its display all fields. Can someone help me wit this problem?
remove form_end, just close form with HTML
</form>
but then you must handle CSRF token generation by adding:
{{ form_widget(form._token) }}
{{ form_widget(form._token) }}
or try setting field you do not want to show using:
{% do form.recet.setRendered %}
but probably best way is not to add this field in the first place, rather than hiding it, by form options or event listeners depending on some criterias
A better solution is to remove the field from the form type. If you only remove it from the view, it may be interpreted as a blank submission for that field and delete existing data.
If you only use the form in one place, then just remove the field from the type. If you use it in multiple places, then you can selectively remove the field in a FormEvents::POST_SET_DATA event listener.

Symfony 2.6.3 {{ form_errors( form ) }} is blank when isValid() returns false

I'm using Symfony V2.6.3
I have a simple form with three fields based on a Type class.
The Entity class that the Type class specifies via setDefaultOptions() has the use Symfony\Component\Validator\Constraints as Assert; statement.
Each field has an #Assert\NotBlank() constraint in the Entity class.
CSRF is enabled.
HTML5 validation is disabled.
If I submit the form with all fields blank, the following happens:
In the controller:
isValid() returns false
getErrors( true ) returns an error for each field
In the template:
{{ form_errors( form ) }} generates no text for the form or any of the fields.
I created a custom form theme and modified {% block form_errors %} to
dump the errors variable. The dump shows the errors property is a FormErrorIterator object with two properties: form, which is, I believe the field definition, and errors which is an empty array.
Oddly enough, drilling down into the form property reveals an errors property that is an array with a single FormError object that contains the error message.
This is not my first time using forms. It has worked just fine for me in the past. Could this be a new bug in 2.6?
I have searched for this and all I found were situations where getErrors() was also returning nothing.
Thanks in advance,
Dave
Okay. I finally found the answer to this question.
Many thanks to thenetimp on the Symfony forum. He had the same problem and figured it out (http://forum.symfony-project.org/viewtopic.php?f=23&t=42841&p=135003&hilit=form_errors#p135003).
Turns out that you have to call createView() after calling isValid(). That actually makes sense when you think about it.
Form errors are bound to a FormType, not to the complete Form. The RootType will only contain the errors that bubbled up from sub types. The sub types will only contain the errors of their type and the ones bubbled up from sub sub types, etc.
Doing {{ form_errors(form) }} will only render the errors of the root form. Assume there was an error on a form.name field, you can get this error by doing: {{ form_errors(form.name) }}.

Symfony 2, double error

Why i have double error when i want to customize my form ?
Template:
<div>
{{ form_errors(edit_form.wiek) }}
{{ form_widget(edit_form.wiek) }}
</div>
HTML:
You probably see the error on the field level, but also on the form level. It can look like you have two errors especially if there's only one field in your form.
Dig into the customizing error output of the form cookbook for more details.
Edit
The doc gives a much better explanation :)
You can also customize the error output for just one specific field type. For example, certain errors that are more global to your form (i.e. not specific to just one field) are rendered separately, usually at the top of your form.