In my symfony 1.4 project I want to change label of form text. I can do that by following code:
$this->setWidget('first_name', new sfWidgetFormInputText(array('label' => 'First Name')));
But I can't do that if I add 'label' in the following code, as probably sfValidatorString doesn't accept 'label'.
$this->setValidator('banner_id', new sfValidatorString(array('min_length' => 9)));
How can I solve this?
Try:
$this->widgetSchema['feildname']->setLabel('Label Text');
You can also change all your fields label by using
$this->widgetSchema->setLabels(array(
"widget_name_first" => "my widget label",
"widget_name_second" => "my widget label",
));
Try this one:
$this->getWidgetSchema()->setLabel('fieldName', 'Custom label');
Or:
$this->getWidget('fieldName')->setOption('label', 'Custom label');
You can also configure your labels from the generator.yml file.
For Example:
config:
actions: ~
fields:
date:
label: Date of birth
date_format: dd/MM/yy
address:
label: Work address
Related
I have a base extension so i can version my website. That means i have not a controller or a repository on the extension. So what i want to do, is to create my own settings on existing elements. I was experimenting around with a text align values on the header content element.
Keep in mind, there is already a setting for this, but i am just
experimenting.
I figured out how to add them and the values are saved on the database.
What i now want to do, is to take the values and add them as a class on FLUID. This is where i stuck. I can not get the values. Any idea how to do it?
After this guide How to enable header_position in TYPO3 7.6
i manage to get my code that far:
On the folder /Configuration/TCA/Overrides/tt_content.php
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
ExtensionManagementUtility::addTCAcolumns('tt_content',[
'header_position_custom' => [
'exclude' => 1,
'label' => 'header position',
'config' => [
'type' => 'select',
'renderType' => 'selectSingle',
'items' => [
['left', 'left'],
['right', 'right'],
['center', 'center']
]
]
]
]);
ExtensionManagementUtility::addFieldsToPalette('tt_content', 'header', '--linebreak--,header_position_custom', 'after:header_layout');
ExtensionManagementUtility::addFieldsToPalette('tt_content', 'headers', '--linebreak--,header_position_custom', 'after:header_layout');
On the folder /Configuration/Typoscript/Constants/Base.typoscript
styles.templates.templateRootPath = EXT:my_website_base/Resources/Private/Extensions/Fluid_styled_content/Resources/Private/Templates/
styles.templates.partialRootPath = EXT:my_website_base/Resources/Private/Extensions/Fluid_styled_content/Resources/Private/Partials/
styles.templates.layoutRootPath = EXT:my_website_base/Resources/Private/Extensions/Fluid_styled_content/Resources/Private/Layouts/
On the /Resources/Private/Extensions/Fluid_styled_content/Resourcs/Private/Partials/Header.html
<h1 class="{positionClass} {header_position_custom} {data.header_position_custom} showed">
<f:link.typolink parameter="{link}">{header}</f:link.typolink>
</h1>
I 've put the class showed just to make sure that i am reading the
file from the path i gave on the constants
File ext_tables.php
TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile($_EXTKEY,'Configuration/TypoScript', 'Website Base');
File ext_tables.sql
CREATE TABLE tt_content (
header_position_custom varchar(255) DEFAULT '' NOT NULL,
);
With all these i get my selectbox where i wanted to be and i get the values on the database. That means that if i select the value "Center" in the selectbox, then it will be saved on the database. How can i get this value and use it as class on the FLUID?
Thanks in advance,
You will find your field in the data object.
For inspecting your fluid variables you can use the f:debug-VH:
<f:debug title="the data">{data}</f:debug>
for inspecting all (in the current context) available variables you can debug _all:
<f:debug title="all data">{_all}</f:debug>
Hint: use the title attribute to identify the output
and don't forget to write a get* and set* function for new fields!
I want to implement custom helper messages (html data attribute) for every form element. Helper messages are defined in YAML lang file (messages.en.yml) and not all form elements have helper messages.
Problem is that I am not sure can it be done using Symfony FormType or Symfony Form events.
I was thinking to create Form event as a service and inject translator service and from there manipulate data and add helper classes but I didn't find any solid example how can it be done.
Other option I was thinking is to use Trait and inject translator service in trait and from there to start developing my code but it doesn't feel so right.
Can someone share their experience and clue how to resolve this particular problem?
Here is my setup
messages.en.yml
intro:
created: Intro created!
edited: Intro has been edited successfully!
deleted: Intro has been has been deleted!
index:
title: List of all intros
new:
title: New intro
show:
title: Details of intro
edit:
title: Edit intro
form:
title: Title
content: Content
isEnabled: Is active
tooltip:
title: Please enter title
My form type:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, array(
'label_format' => 'admin.intro.form.%name%',
'attr' => [
'data-helper' => 'Please enter title'
]
)
)
->add('content', TextareaType::class, array(
'label_format' => 'admin.intro.form.%name%',
'required' => false,
'attr' => [
'class' => 'mceEditor'
]
)
)
->add('isEnabled', CheckboxType::class, array(
'label_format' => 'admin.intro.form.%name%',
'required' => false,
)
You can do so by registering the form as a service and injecting the YAML configuration into the form.
config.yml
message_config:
intro:
created: Intro created!
edited: Intro has been edited successfully!
deleted: Intro has been has been deleted!
index:
title: List of all intros
new:
title: New intro
show:
title: Details of intro
edit:
title: Edit intro
form:
title: Title
content: Content
isEnabled: Is active
tooltip:
title: Please enter title
services.yml
services:
app.form.type.my_form:
class: AppBundle\Form\Type\MyForm
arguments:
- '%message_config%'
tags:
- { name: form.type }
Now you can work with the array in your FormType.
<?php
namespace AppBundle\Form\Type;
class MyForm
{
protected $messages;
public function __construct(array $messages)
{
$this->messages = $messages;
}
}
If the YAML configuration is being used by a Translator service, you would inject the translator service instead.
Update: Symfony does this with the Translator component and Twig form themes see the below comment.
I have user.yml file in my validation folder in symfony and I am using it to validate the data. eg:
\APIBundle\Entity\User:
properties:
startDate:
- NotBlank: { message: 'Start date cannot be blank.' }
- Date: { message: 'Enter the valid start date.' }
I wanted to validate "startDate" field if user have passed it which is working now. But if user is not passed the "startDate" then I wanted to skip this validation. i.e Wanted the this validation as optional .
How to achieve this ?
I tried to set required false as following:
\APIBundle\Entity\User:
properties:
startDate:
- Required: false
- NotBlank: { message: 'Start date cannot be blank.' }
- Date: { message: 'Enter the valid start date.' }
Which is not working symfony return the error
required just affects rendering see :
If true, an HTML5 required attribute will be rendered. The corresponding label will also render with a required class.
This is superficial and independent from validation. At best, if you
let Symfony guess your field type, then the value of this option will
be guessed from your validation information.
you should use empty_data in your form like so :
$builder->add('gender', ChoiceType::class, array(
'choices' => array(
'm' => 'Male',
'f' => 'Female'
),
'required' => false,
'placeholder' => 'Choose your gender',
'empty_data' => null
));
Your entity in this case should have the property nullable=true and of course like mentionned NotBlank shouldn't be used
Just remove NotBlank
\APIBundle\Entity\User:
properties:
startDate:
- Date: { message: 'Enter the valid start date.' }
NotBlank is equivalent of reqired.
Since you're using NotBlank, that field is always required. However, you only want to validate the field if it is actually set.
I think that you won't accomplish that with Symfony's built-in validation, but you could write your custom Validation Constraint that handles this case: http://symfony.com/doc/current/cookbook/validation/custom_constraint.html
I use symfony 1.4.15 with doctrine. I have module and there are two i18n field. So in my form class I make:
$this->languages = sfConfig::get('app_cultures_enabled');
$langs = array_keys($this->languages);
$this->embedI18n($langs);
foreach($this->languages as $lang => $label)
{
$this->widgetSchema[$lang]['name'] = new sfWidgetFormTextarea(array(), array('cols'=>40,'rows'=>2));
$this->widgetSchema[$lang]['description'] = new sfWidgetFormTextarea(array(), array('cols'=>80,'rows'=>5));
}
And it is work perfect!
But I have many fields in my form, so I need to make some "groups" in my form. So I make next in my generator.yml:
config:
form:
display:
Main info:[active,position,сoverage_id,basis_id,durability_id,comfort_id,weight_coating,thickness_coating,height_of_pile,segment_id,width_rolls_one,width_rolls_two,width_rolls_three,standart_width_rolls,max_width_rolls,min_width_rolls]
Price: [price,margin_on_roll,margin_on_cutting,special_discount,discount_for_residues]
Market: [certificate]
And I can not display two i18n field "name" and "description". I try
Market: [name_i18n] and name_i18n_uk and many other. So is is to possible to make?If not are there other way to group filed in form?
Thank you!
What you can do is something like this in the generator:
form:
display:
Market: [certificate, pt, en, es, ...]
I can not manage to have both i18n and tinyMCE widgets on internationalised fields.
If i put both, i will have internationalised fields for all my objects' fields, but no tinyMCE for them. I will have as much tinyMCE fields as i declared, but thew will not correspond to any language, they will be at the beginning or at the end.
It worked perfectly before i internationalized the objects
Here is an example of code :
// config/doctrine/schema.yml
MyObject:
actAs:
I18n:
fields: [title, subtitle, intro, text]
columns:
title: {type: string(500)}
subtitle: {type: string(500)}
intro: {type: string(4000)}
text: {type: string(16000)}
// lib/form/doctrine/MyObject.class.php
public function configure()
{
$this->embedI18n(array('en', 'fr', 'es'));
$this->widgetSchema->setLabel('fr', 'Français');
$this->widgetSchema->setLabel('en', 'Anglais');
$this->widgetSchema->setLabel('es', 'Español');
$this->widgetSchema['intro'] = new sfWidgetFormTextareaTinyMCE(
array(
'width'=>600,
'height'=>100,
'config'=>'theme_advanced_disable: "anchor,image,cleanup,help"',
'theme' => sfConfig::get('app_tinymce_theme','simple'),
),
array(
'class' => 'tiny_mce'
)
);
$this->widgetSchema['text'] = new sfWidgetFormTextareaTinyMCE(
array(
'width'=>600,
'height'=>100,
'config'=>'theme_advanced_disable: "anchor,image,cleanup,help"',
'theme' => sfConfig::get('app_tinymce_theme','simple'),
),
array(
'class' => 'tiny_mce'
)
);
$js_path = sfConfig::get('sf_rich_text_js_dir') ? '/'.sfConfig::get('sf_rich_text_js_dir').'/tiny_mce.js' : '/sf/tinymce/js/tiny_mce.js';
sfContext::getInstance()->getResponse()->addJavascript($js_path);
}
So i guess when i use $this->widgetSchema['intro'], the "intro" name does not correspond to all the i18n "intro" fields. I tried both 'en_intro' and 'intro_en', but it doesn't do any magic.
So maybe you could help me ?
So i found how to do this and i thought it might interest somebody :
Instead of
$this->widgetSchema['intro'] = ...
Put
$this->widgetSchema['en']['intro'] = ...
with all the languages.
also you can use :
$this->widgetSchema->moveField('en',sfWidgetFormSchema::BEFORE,'intro');
move the i18n label and field to before intro field.