I'm new to symfony. I searched a lot especially in the symfony documentation:
http://symfony.com/doc/current/book/translation.html#translating-constraint-messages
But I found nothing about it.
I know how to use capitalize twig filter in a twig template.
http://twig.sensiolabs.org/doc/filters/capitalize.html
{{ 'my first car'|capitalize }}
But is it possible to capitalize a translation in these three scenarios:
In entities annotations Constraints Messages (this example does not work, translation is not translated if I add |capitalize)
class Author
{
/**
* #Assert\NotBlank(message = "author.name.not_blank|capitalize")
*/
public $name;
}
In a form builder (this example does not work, translation is not translated if I add |capitalize)
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'invalid_message' => 'user.password_confirm_mismatch|capitalize',
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
'required' => false
)
In a controller with the translator service. In this case it is not very important because I can use the php ucfirst function. But maybe there is another method more in line with symfony best practices.
//Not capitalized, any way to add twig capitalize filter in this code?
$tranlsation_test_1 = $this->get('translator')->trans('info.profile_updated_successfully',array(),null,$new_user_app_language_code);
//Capitalized but with the php ucfirst function
$tranlsation_test_2 = ucfirst($this->get('translator')->trans('info.profile_updated_successfully',array(),null,$new_user_app_language_code));
Many tanks for your advices.
Related
I'm searching how to implement a system of choosing languages dynamically using form builder.
so we must have two html array inputs field : name="languages[]" and the second will be name="languges_level[]"
So this system allows the user to set the language which he can speak with his level on it.
The User can add/remove Language dynamically before he submits the Form.
The Questions :
1- Form Level : what will be the field form type? I have to add 2 form fields which will be combined to create my result array which will be stored in the database. So this two field will be not mapped with ORM.
->add('langues', TYPEXXX:class)
->add('langues_level', TYPEXXX:class)
3- Twig Level: should i make some change in the twig as well?
So what will be the best solution in my case?
My first try is :
->add('languages', CollectionType::class, array(
'entry_type' => ChoiceType::class,
'entry_options' => array(
'choices' => array(
'Français' => 'Français',
'English' => 'English',
'Italien' => 'Italien',
'Espanish' => 'Espanish',
),
'label' => ' ',
),
))
->add('language_levels', CollectionType::class, array(
'entry_type' => ChoiceType::class,
'entry_options' => array(
'choices' => array(
'Beginner' => 'Beginner',
'Medium' => 'Medium',
'Good' => 'Good',
),
'label' => ' ',
),
));
but this don't work as I mentioned in the picture .. who had a perfect solution plz ?
I think you need a Collection of Forms.
so we must have two html array inputs field : name="langues[]" and the
second will be name="langes_level[]"
(..)
3- Twig Level: should i make some change in the twig as well?
(..)
4- Javascript Level, i can develop it when the inputs are clean
created in the html.
No, no and no. Describing how your 'array input fields' should be named exactly is not the right mentality if you're using a framework like Symfony. Describe your entity fields, describe your form fields and Symfony will give all form elements a name. Symfony Forms will render and handle the form for you, so there is (very likely) no need to be bothered what the form element names are exactly.
Your entity class:
class LanguageLevel
{
protected $user;
protected $language;
protected $level;
//getters and setters
}
Create a form type:
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('languages', CollectionType::class, array(
'entry_type' => LanguageLevelType::class,
'allow_add' => true,
));
}
}
And a LanguageLevelType:
class LanguageLevelType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('language', LanguageType::class)
->add('level', ChoiceType::class, array(
'choices' => array(
'Good' => 5,
'Bad' => 1,
),
));
}
}
If the rendered output is not what you want, check the documentation if you can configure the Form Types. Manually changing the twig template, your controller and/or javascripts for a specific case is possible, but I think the 'Collection of Forms' from above will cover your use case.
I actually did some tricks so i could be able to persist a user if its ID is passed by an url parameter. (Custom action from user list).
/admin/se/api/bundle/create?user=7
I actually could not find how to send the user entity returned by a findByOne(array('id' => $user_id)) so i guess i'll need to pass the $user_id through a hidden field and handle its value in a PrePersist
Otherwise passing the id that way
->add('user', 'hidden', array('data' => $user_id))
will return an error :
This value is not valid.
Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[user] = 7
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
Compound forms expect an array or NULL on submission.
This is my first attempt that is not working :
$container = $this->getConfigurationPool()->getContainer();
$request = $container->get('request');
$user_id = $request->get('user');
if(!empty($user_id)){
$em = $this->getModelManager()->getEntityManager($this->getClass());
$user = $em->getRepository('ApiBundle:User')->findOneBy(array('id' => $user_id));
if($user){
$formMapper
->with('User', array('description' => '<strong>User : </strong>'.$user->getDisplayName()))
->add('user', 'hidden', array('data' => $user_id))
// this of course doesn't work as explained above. How can i have my own hidden input not related to any property
->end();
}
So how would i do that? Any better solution is welcomed.
Well this is the best trick i found. I wish 'sonata_type_model_hidden' has more options. I guess i could do my own custom field to be able to do that. But i'm not sure how and anyway this solution is fast to implement.
$formMapper
->with('Guide', array('description' => '<strong>Guide : </strong>'.$guide->getDisplayName()))
->add('guide', 'sonata_type_model_autocomplete', array(
'property' => array('firstname', 'lastname', 'username', 'email'),
'data_class' => null, // IMPORTANT
'data' => $guide,
'attr' => array('class' => 'sonata-autocomplete-hidden'), // custom class
'label_attr' => array('class' => 'sonata-autocomplete-hidden'), // custom class
)
)
->end();
To hide the field :
.sonata-autocomplete-hidden{
display:none;
}
If you have any better solutions, you're welcome.
I am trying to implement IvoryCKEditor Bundle to my SonataAdmin entities and I am witnessing some very strange errors/bugs/mistakes... I dont even know..
So when I want to render a simple textarea field and add some longtext to it I simply do something like this:
/**
* #param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
// ->add('id', 'hidden')
->add('name')
->add('contentEn', 'ckeditor', array(
'attr' => array('cols' => '8', 'rows' => '8')))
->add('contentEs', 'ckeditor', array(
'attr' => array('cols' => '8', 'rows' => '8')))
->add('status')
;
}
This works like a charm.. However if I have mapped entities and I want to show its fields I use sonata collection:
->add('translations', 'sonata_type_collection',
array(
'required' => false,
'label' => false,
),
array(
'edit' => 'inline',
'inline' => 'standard',
)
)
And in the mapped entities admin I do this again:
$formMapper
->with('Item Info')
//->add('id')
->add('product_name')
->add('description_for_lbi', 'ckeditor')
->add('short_description', 'ckeditor')
->add('long_description', 'ckeditor')
->add('conditions', 'ckeditor')
->add('language', null, array('required' => true))
->end()
;
Now here is the problem. Its seems the ckeditor form the collection is rendering in a completely different way.
The first example renders an iframe and makes the ckeditor look "clean".
In the collection ckeditor is rendering in a completely different way, no iframes.. And for the editor to show up I have to click on the field first.. And the field has no borders... I really dont know how to explain this.
So I guess my question is, why the ckeditor is rendering completely different when I am using it in a collection. Am I doing something wrong?
If you guys dont understand what I mean I can post some screens...
UPDATE
I thing the problem is here:
'edit' => 'inline',
'inline' => 'standard',
this makes the editor look bad. However if I remove these lines I get error:
Error: Maximum function nesting level of '100' reached, aborting!
This error is when I am trying to edit an object
You are completely right with your edit. inline editing in ckeditor breaks if you have 2 or more instances of the ckeditor config.
My suggestion is to drop inline editing.
For the second part, the error comes from xdebug, which cannot follow your object nesting level because it exceeds this limit.
In order to fix this (which is recommended, as limit = 100 is way too low for symfony2), please see this
article.
What I want to do is add sonata_type_collection to my custom formType.
Normal way is add sonata_collection_type to $formMaper inside AdminClass like:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('elements, 'sonata_type_collection', array(
'some_options' => 'options'
))
}
It work perfect, but i have my custom form type, and when i defined it like:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$formMapper->add('elements, 'sonata_type_collection', array(
'some_options' => 'options'
))
}
It doesn't work (it appear only label of filed). Problem is wrong template, so I tried to set formAdminTemplate
I made it by set template in view
{% form_theme formElement 'SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig' %}
Problem is sonata_admin variable inside this 'formTheme'. This variable doesn't exist in my form.
Of course my form type is related to admin class but i don't know how could I I tell symfony about this relation
You need an admin class for your collection child :
$formMapper->add('customizations', 'sonata_type_collection',
array(
'required' => true,
'type_options' => array('delete' => true),
'by_reference' => false,
'mapped' => true
),
array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'targetEntity' => '/path/to/Entity/Customization',
'admin_code' => 'my.service.customization_admin'
)
);
I find solution. Instead using my custom type, I defined form using admin class. I need this form outside admin so it was little difficult.
First of all in my controller i get admin class from service. Inside admin class I override 3 methods which are use to create form
public function getFormBuilder()
public function defineFormBuilder(FormBuilder $formBuilder)
public function buildForm()
then i had to save my entity by sonata admin way. using create method instead handleRequest.
I work with Symfony2 and I would like to create a registration form.
I don't want to use FOSUserBundle.
So, I create an Entity Account (with fields : username, password, email...) and I create the form :
$account = new Account();
$form = $this->createFormBuilder($account)
->add('username', 'text', array('label' => 'Nom de compte :'))
->add('password', 'password', array('label' => 'Mot de passe :'))
->add('email', 'email', array('label' => 'Adresse email :'))
->getForm();
Now, I want to add a confirmation field for the password.
But, when I try to add a field with add() method, for example "password_confirmation"
I have this :
Neither property "password_confirmation" nor method "getPasswordConfirmation()" nor method "isPasswordConfirmation()" exists in class "App\FrontBundle\Entity\Account"
How can I add a custom field ? And after, how to valid it ?
Thank you.
BR.
An update for Symfony 2.1:
property_path has been deprecated and instead you should use mapped. The syntax remains the same:
->add('password_confirmation', 'password', array('mapped' => false))
In a normal situation, you'd need to explicitly specify that *password_confirmation* isn't part of the entity, using the property_path option.
->add('password_confirmation', 'password', array('property_path' => false))
And then to validate it with a CallBackValidator.
But, in this specific case, where you want to repeat a field, the repeated widget can do that for you.
->add('password_confirmation', 'repeated', array(
// See the docs :)
));