Symfony form's i18n and add_empty - symfony1

I have this widget:
$this->setWidget('slug', new sfWidgetFormDoctrineChoice(array('model'
=> 'MyTable', 'method' => 'myMethod', 'key_method' => 'myMethod',
'add_empty' => 'Select option')));
Ok, what should I do to translate the "Select option"?
I can't use the __() helper inside the form, and adding that string
into my XLIFF file doesn't translate it automatically.
If it can't be done, what workaround should I implement? I can't find
any way, and neither can't find any tip in the official documentation.
Thanks!

I think I have solved it:
$translated_text = $this->widgetSchema->getFormFormatter()->translate('String to translate');

You can use helper inside form, try this:
public function setup()
{
sfContext::getInstance()->getConfiguration()->loadHelpers(array('I18n'));
$this->setWidget('slug', new sfWidgetFormDoctrineChoice(array('model'
=> 'MyTable', 'method' => 'myMethod', 'key_method' => 'myMethod',
'add_empty' => __('Select option')));
...
}

Related

Trim String In Automapper

Mapper.CreateMap<DataViewModel, DataSource>()
My Source Here Contains String Values Coming from user interface. I want to trim all the string before i map it to my destination object. Couldn't find a solution for this. Anyone Knows how this is to be done
This can be done using the ForMember method, like so:
Mapper.CreateMap<DataViewModel, DataSource>()
.ForMember(x => x.YourString, opt => opt.MapFrom(y => y.YourString.Trim()));
If you want to trim more than one property you can chain the .ForMember() method like this:
Mapper.CreateMap<DataViewModel, DataSource>()
.ForMember(x => x.YourString, opt => opt.MapFrom(y => y.YourString.Trim()))
.ForMember(x => x.YourString1, opt => opt.MapFrom(y => y.YourString1.Trim()))
.ForMember(x => x.YourString2, opt => opt.MapFrom(y => y.YourString2.Trim()));
Whilst this would get the job done, I would suggest performing input sanitisation elsewhere in your application as it doesn't belong in the mapping.
You can also use AddTransform
CreateMap<DataViewModel, DataSource>()
.AddTransform<string>(s => string.IsNullOrWhiteSpace(s) ? "" : s.Trim());

Laravel generate urls with parameters

I want to generate url with parameters in Laravel like this:
http://example.com/validate_email?user_id=31&confirmation_code=ihlasfsafe
I tried
URL::to('validate_email', array('user_id' => $user_id, 'confirmation_code' => $confirmation_code));
But it gives me:
http://example.com/validate_email/31/ihlasfsafe
So how can I do it correctly?
You need to use:
URL::to('validate_email').'?'.http_build_query(array('user_id' => $user_id, 'confirmation_code' => $confirmation_code));
to achieve this

Symfony sfWidgetFormInputFile deleted my file

Sorry for the many questions but I'm new to Symfony. I made a form and all it working well. I a few fields for pictures (Picture1, Picture2, Picture3, etc). I used te sfWidgetFormInputFile and all is working well with that as well. It uploads the file where it needs to and it gives it a unique name.
The problem I'm having is that those fields are set in mySQL to have a default value of "no-pic.png" so that if the user doesn't upload a picture, it will still have something to show in the post. But every time someone uploads a picture, it deletes my no-pic.png file and the posts without a picture show with a blank square. I'm thinking it's because it's set to "delete previous image" before uploading the new one to reduce excess unused pictures on the server.
I'm wondering if there's a way to have it check to see if the value is "no-pic.png" before deleting it from the folder. In case it helps, this is the code I'm using in the form.class file.
$this->widgetSchema['picture1'] = new sfWidgetFormInputFile(array('label' => 'Pictures', ));
$this->validatorSchema['picture1'] = new sfValidatorFile(array('required' => false, 'path' => sfConfig::get('sf_upload_dir').'/car', 'mime_types' => 'web_images', ));
$this->widgetSchema['picture1'] = new sfWidgetFormInputFileEditable(array(
'label' => 'Pictures',
'file_src' => '/uploads/car/'.$this->getObject()->getPicture1(),
'is_image' => true,
'edit_mode' => !$this->isNew(),
'template' => '<div>%file%<br />%input%<br />%delete%%delete_label%</div>',
));
$this->validatorSchema['picture_delete'] = new sfValidatorPass();
Just for reference here is the code I used.
$this->setWidget('logo_image', new sfWidgetFormInputFileEditable(array(
'file_src' => 'uploads/logos/'.$this->getObject()->getFilename(),
'edit_mode' => !$this->isNew()
)));
$validatorOptions = array(
'max_size' => 1024*1024*10,
'path' => sfConfig::get('sf_upload_dir').'/logos',
'mime_type_guessers' => array(),
'required' => false
);
if ($this->isNew())
{
$validatorOptions['required'] = true;
}
$this->setValidator('logo_image', new sfValidatorFile($validatorOptions));
$this->setValidator('logo_image_delete', new sfValidatorString(array('required' => false)));
I found a workaround. I had the page check to see if it was set to no-pic.png and then it displays a no-pic.png in another location. That way when it goes to upload a picture, it won't delete it. :) Thanks for your help though.

using ahDoctrineEasyEmbeddedRelationsPlugin to embed i18n Translation

I'm using ahDoctrineEasyEmbeddedRelationsPlugin to add dynamic i18n translations to my object, so I wrote this in my object Form class
$this->embedRelations(array(
'Translation' => array(
'considerNewFormEmptyFields' => array('content', 'lang')
));
The result I got is only one input per record, "content".
I've tried this in the FormTranslation class, but no luck:
$this->useFields(array('content', 'lang'));
So what I did was to create a manual doctrine relation with a foreignAlias called "translations", and then:
$this->embedRelations(array(
'translations' => array(
'considerNewFormEmptyFields' => array('content', 'lang')
));
this almost worked, I get the lang field now, but only in the list of existing tranlations, not in the new translation form
Any ideas if I can archieve this? Thanks!
Hm , I always use for example for 'en' and 'uk' culture :
considerNewFormEmptyFields' => array('en','uk')

How to pass variables to render_to_string?

Trying to do the following
#message = render_to_string ( :sender => sender, :template => "template" )
But when accessing #sender in template it turns out to be nil:NilClass. Double checked if I pass the right variable and it's totally fine. Maybe there are other way to pass variables to render_to_string?
It might be the syntax you're using. Try using the :locals argument:
#m = render_to_string :template => "template", :locals => {:sender => sender}
Then you just need to access sender (without an #) as a local variable inside the template.
Here's Jason Kim's solution he wrote in a comment which worked for me:
ActionController::Base.new.render_to_string(
"user_mailer/welcome_email.html.erb", locals: { :#user => user}
)
Please mind the :#user => value bit.
In Rails 5 (atm in beta):
ApplicationController.render(
file: 'path',
assigns: { foo: 'bar' }
)
More here
Try this:
ac = ActionController::Base.new()
ac.render_to_string(:partial => 'path to your partial',:locals => {:varable => your variables})
In rails 4.0.2 this worked:
render_to_string(partial: 'path/to/partial', locals: { argument: 'value'}
I was trying to render a different format of partial in render_to_string. The thing which really worked for me was:
render_to_string(:partial => 'partial_file.html', :locals => {:variable => variable}, :format => :html)
where the name of the file was _partial_file.html.erb.

Resources