In Symfony how to set default values to sfWidgetFormDateRange from action - symfony1

In Symfony how to set default values to sfWidgetFormDateRange from action

Basically you have to do the following:
$form->setDefault('field_name', array('from' => 'yesterday', 'to' => '+10 years'))
These yesterday and +10 years chants actually are anything suitable for strtotime function. Also, you could pass explicit dates without casting any magic:
$form->setDefault('field_name', array('from' => array('month' => 10, 'day' => 12, 'year' => 1984), 'to' => '+10 years'))

Related

Laravel 8 not translating validation attributes

I am trying to localise the messages in my form validation using Laravel's validate() function, as follows:
$validated = $request->validate([
'email' => 'bail|required|email',
'g-recaptcha-response' => 'required|captcha',
]);
In my lang/en.php etc files I have translations for 'required', as follows:
'required' => 'The :attribute field is required.',
And I have an entry in the attributes array, as follows:
'attributes' => [
'g-recaptcha-response' => 'Recaptcha',
],
However, the translated attribute does not get called in any of the languages.
What am I doing wrong?
This works if you change the validator to facade.
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'email' => 'bail|required|email',
'g-recaptcha-response' => 'required|captcha',
]);
And return the erros with $validator->messages()->getMessages();.

Collection type migration from sf2.8 to sf3

Let's take into account this example: I've got a form type where I embed another type in a "collection fashion" as follows
->add('foo', CollectionType::class, [
'entry_type' => new FooType(),
'error_bubbling' => false,
'allow_add' => true,
'allow_delete' => true,
'required' => false,
'label' => ' ',
'entry_options' => [
'bar' => $options['bar'],
],
'by_reference' => false,
])
and all works fine (I can add and remove elements easily).
Since I'm upgrading the framework from 2.8 to 3.0, I've read changelog and noticed
The option type of the CollectionType has been removed in favor of the
entry_type option. The value for the entry_type option must be the
fully-qualified class name (FQCN).
So I've changed my form to
->add('foo', CollectionType::class, [
'entry_type' => FooType::class,
'error_bubbling' => false,
'allow_add' => true,
'allow_delete' => true,
'required' => false,
'label' => ' ',
'entry_options' => [
'bar' => $options['bar'],
],
'by_reference' => false,
])
but when I add elements to collection, it seems that ModelData are collapsed into the same element (the last one of the collection).
So, imagine that I post entities with ids [1,2,3], ViewData is correct whereas ModelData has an array collection with three elements: three foo(s) with id 3, so the same foo.
Does anyone understand what's going on here?
Edit
This is FooType meaningful code
$resolver->setDefaults([
'data_class' => 'Vendor\Bundle\Entity\Foo',
'empty_data' => new Foo(),
]);
It seems that this is a symfony bug (unconfirmed; read more) or a misuse of combination of FQCN in entry_type and new on empty_data of embedded form.
I'll update my answer properly when and if SF core team reply me in the issue.

Set :selected value based on variableX -or- the user submitted value if variableX is nil

Have this select dropdown input field that is populated with state names.
I want to set the selected value to be the content of #state.id if this is not nil. If there is already a chosen option then I would like that to be selected instead.
How can this be done correctly? I tried several solution all of which fails, latest code example im using now:
= p.input :state,
:collection => ["State1","State2"]
:selected => (#state.id.to_s rescue nil) || (params[:state] rescue nil),
:id => "state",
:name => "state",
:prompt => t('forms.choose')
The problem is that the :selected option works based on two values.
The index of the element in the collection array, or
The value itself
So lets say you want the the last option to be selected, in your case you would need:
:selected => 1
or
:selected => "State2"
I hope it helps.

Elastica multi_field setup

I'm trying to use multi_field syntax of elasticsearch in combination with Elastica. I create an index and an mapping which looks like this:
$mapping->setProperties(array(
'id' => array('type' => 'string', 'include_in_all' => true),
'title' => array('type' => 'string', 'include_in_all' => true),
'publisher' => array('type' => 'multi_field', 'include_in_all' => TRUE, 'fields' =>
array('publisherName' => array('type' => 'string', 'index' => 'analyzed'),
'untouched' => array('type' => 'string', 'index' => 'not_analyzed')
)
));
So far, so good. I can run queries against the title field.
But when I try to query the field "publisher" in http://example.com:9200/_plugin/head/ I'm not able to select the field publisher or to create a structured query. I looks, that the field publisher is not in the index.
But I can build facets on publisher.untouched which works very well. Whats wrong in my mapping? I need to search for the publisher.
See the docs on multi_field mapping. Looks like you need to set a default field by changing 'publisherName' to just 'publisher'.

haml and no javascript? (rails: form_remote_tag and :with parameter)

i am trying to call a remote method to update page content via ajax/js.
either i am too tired already or haml is not parsing the following code correctly to send the value of the query field via prototype. any ideas?
- form_remote_tag(:url => {:controller => "search", :action => "line"},:with => "'query=' + $('query').value" ) do
%input{:type => 'text', :id => 'query'}
%input{:type => 'submit', :value => 'Search'}
thanks a lot!
t
Have you tried a
= form_remote_tag
instead of
- form_remote_tag
I'm new to HAML myself but I was under the impression that you'll need the form tag to be actually generated not just executed...
Try passing the :with as part of the options hash.
- form_remote_tag({ :url => {:controller => "search", :action => "line"}, :with => "'query=' + $('query').value" }) do
If that doesn't work, debug the problem: Look at the generated html. Is the text field with id query the only element in the page with that id? Is the js code correct? Use the Firebug console to ensure $('query').value returns whatever you've entered into the text field.
Still stuck? Add your generated html into your question so we can better help.
EDIT: Your query input tag does not have a name attribute. Without a name, the javascript helper code skips that field when serializing the form fields...also, you do not need the :with code.
%input{:type => 'text', :id => 'query', :name => 'query'}

Resources