Symfony2 (2.3) birthday field set default value in twig (form_widget) - forms

i can't figure out how to set a default vaule to a birthday field in symfony.
for text field this works:
{{ form_widget(form.name, {'value' : 'Max' }) }}
is it possible to set a date default value? something like:
{{ form_widget(form.birthday, {'value' : '02.03.1980' } )}}
symfony.com/doc/current/reference/forms/types/birthday.html
thank you!

Thank you for the hint (see data field). This works with choice. I set the default date in the controller and not in the twig template.
$formAntragssteller = $this->createForm(new StandardType(), $antragssteller)
->add('geburtsdatum', 'birthday', array(
'widget' => 'choice',
'attr' => array(
'class' => 'form-control'
),
'label' => 'Geburtsdatum',
'input' => 'string',
'format' => 'dd.MM.yyyy',
'empty_value' => array('year' => 'Jahr', 'month' => 'Monat', 'day' => 'Tag'),
'data' => '1984-04-04'
));

"value" has nothing to do with the form type, but only with the internal widget template handling
you need to match the right widget via the "widget" word in the add method of the form builder
i.e. if you set ['widget' => 'single_text', 'format' => 'dd-MM-yyyy'] value is for sure in the form of '02-03-1980'
birthday field tipe - widget paragraph

Related

Recaptcha google with EWZRecaptchaBundle for Symfony

I want to have a recaptcha system to my contact form symfony. For that I use a EWZRecaptchaBundle. But I try lot of stuff tricks for running recaptcha but my form submit run without testing validation recaptcha widget (invisible and visible)
Can you help me to run recaptcha correctly. My submit form run without deal with recaptcha but I have the widget display correctly.
config.yml
ewz_recaptcha:
public_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
locale_key: %kernel.default_locale%
locale_from_request: false
enabled: true
verify_host: true
ajax: false
ContactType
->add('recaptcha', EWZRecaptchaType::class, array(
'attr' => array(
'options' => array(
'theme' => 'light',
'type' => 'image',
'size' => 'invisible',
'defer' => true,
'async' => true,
'callback' => 'onReCaptchaSuccess',
'bind' => 'contact_submit',
)
),
'mapped' => false,
'constraints' => array(
new RecaptchaTrue()
)
)
)
// ->add('recaptcha', EWZRecaptchaType::class)
->add('submit', SubmitType::class, [
'label' => 'form.submit.send',
'attr' => ['class' => 'btn1 form_recaptcha_submit', 'id' => 'contact_submit']
])
Twig template
{{ form_widget(form.recaptcha, { 'attr': {
'options' : {
'theme': 'light',
'type': 'image',
'size': 'invisible'
},
} }) }}
contact (entity)
I have my field recaptcha
private $recaptcha;
I have the widget recaptcha google which display correctly but my submit form don't work with this. never.
I have registered my domain on google website recaptcha
You need to define the validation in your form (it is also possible to validate in the entity class). Also in my case I did not create a field in my eneity. I prefered to use what we call a "non mapped" field. To use a non mapped field you just need to use 'mapped' => false, in your contactType
I show you below the code I used:
My ContactType:
->add('recaptcha', EWZRecaptchaType::class, array(
'attr' => array(
'options' => array(
'theme' => 'light',
'type' => 'image',
'size' => 'normal',
'defer' => true,
'async' => true,
)
)
,
'mapped' => false,
'constraints' => array(
new RecaptchaTrue()
)
))
I give you my config.yml so you can see I don't need that much options to be defined to make recaptcha works:
ewz_recaptcha:
public_key: xxxx
private_key: xxxx
# Not needed as "%kernel.default_locale%" is the default value for the locale key
locale_key: %kernel.default_locale%
locale_from_request: true
edit: to display the recaptcha widget with twig in my template, I just use {{ form_end(form) }} and the recaptcha widget is on top of the submit button.
edit 2: You don't need to do any validation in your controller but this one:
if ($contactForm->isSubmitted() && $contactForm->isValid())
{
//your logic
}

symfony2.5 maxlength deprecated

I would like to know why was max_length in form type deprecated?
And how to achieve the desired effect the cleanest way now ?
See related issue on Github. This option only add html attribute to textarea. You can manually add it via attributes:
$builder->add('field', 'textarea', array(
'attr' => array('maxlength' => 255),
));
You can add it in builder:
$builder->add('field', 'textarea', array(
'attr' => array('maxlength' => 255),
));
or in twig:
{{ form_widget(form.field, {'attr': {'maxlength': 500}}) }}
IMPORTANT! Attribute value must be int, string will dont work:
$builder->add('field', 'textarea', array(
'attr' => array('maxlength' => '255'),
));

Laravel form with placeholder, class and input::old

I am trying to get used to work with Laravel's blade.
I would like to create a text input called company.
The input field needs to have an id and a class.
I also want to show a placeholder if there is no data in the database, or the data stored if already exists.
Finally, I would like to keep the introduced input in case of errors.
I would like to use something similar at this:
{{ Form::text(
'company',
isset($user->company)?$user->company:array('placeholder'=>'Your company'),
array('class' => 'field required', 'id' => 'company'),
Input::old('company')
) }}
Any help would be appreciated. Thanks!
The easy way, using form model binding:
{{ Form::model($user, [ ...options...]) }}
{{ Form::text(
'company', // refers to $user->company
null, // auto populated with old input in case of error OR $user->company
array('class' => 'field required', 'id' => 'company',
'placeholder' => 'Your company') // placeholder is html attribute, don't use model data here
) }}
And if you don't want form model binding, this is all you need:
{{ Form::text(
'company',
$user->company, // auto populated with old input in case of error
array('class' => 'field required', 'id' => 'company',
'placeholder' => 'Your company')
) }}
Laravel will handle re-populating inputs for you, so long as the key in the POST data is the same as your input’s name attribute.
With Form::text(), the first parameter is the field name, the second parameter is the default value you want, and the third parameter is an array of HTML attributes you want set. So, you would have:
{{ Form::text('company', null, array(
'class' => '',
'id' => '',
'placeholder' => '',
)) }}
Obviously replaced the class, id, and placeholder values with your desired values.
Found it!
It works fine for me if I do this:
{{ Form::text(
'company',
Input::old( 'company', $user -> company ) ,
array( 'class' => 'field required', 'id' => 'company', 'placeholder' => 'Your company' )
) }}

Symfony, Hidden Form Field

i have problem with hiding my form fields. For example:
->add('new_password', 'repeated', array(
'first_options' => array(
'label' => 'Nowe hasło',
'attr' => array('style'=>'display:none;')),
'second_options' => array(
'label' => 'Powtórz nowe hasło',
'attr' => array('style'=>'display:none;')),
'mapped' => false,
'required' => false,
));
Field is not visible but label is visible. I want to have hidden field but label should be hidden to. I want to show it in Jquery after clicking on the button. Any ideas guys ?
First, this is not a Hidden Field Type but a repeated type you want to hide by passing a style='display:none;' attribute.
In general, if you don't want to display a given label, you may need to customize your form rendering.
For example,
{{ form_row(yourForm.new_password) }} {# in case you're using the form_row helper #}
should be replaced by
{{form_widget(yourForm.new_password) }}
Because form_row(yourForm.yourField) is in fact a shortcut for,
{{ form_errors(yourForm.yourField) }}
{{ form_label(yourForm.yourField) }}
{{ form_widget(yourForm.yourField) }}
Also,
Why do you need to hide a repeated password field this way?
If you want to show it only when you click on a button why dont you wrap the newPassword widget in a div with display none?
But if you want to add attributes to the label you can use the option label_attr like this:
{{ form_row(form.name, {'label_attr ':{'class':'hidden'}}) }}
or
->add('new_password', 'repeated', array(
'first_options' => array(
'label' => 'Nowe hasło',
'label_attr' => array('style'=>'display:none;')),
'second_options' => array(
'label' => 'Powtórz nowe hasło',
'label_attr' => array('style'=>'display:none;')),
'mapped' => false,
'required' => false,
));

How to set place holder copy in date select menu in CakePHP

I am using CakePHP 1.3 to create a select menu for date of birth (as below). I can set the default starting values as either blank or a selected date, but ideally I would like to have DD-MM-YYYY as the starting displayed values:
echo $form->input('dob',
array(
'before' => '',
'between' => '',
'after' => '',
'label' => false,
'divider' => false,
'monthNames' => false,
'selected' => false,
'empty' => true,
'dateFormat' => 'DMY',
'minYear' => date('Y') - 70,
'maxYear' => date('Y') - 16,
'error' => array('wrap' => 'div', 'class' => 'error-copy')
));
What I get:
What I would like:
Thank you
I believe if you want to do this you will have to make your own date fields and not use the FormHelper since you can only set a default date to it, what you want involves adding an element to the fields.
You could also try JQuery's datepicker out, it's what I use in my project, and since it's a text field you can just set whatever you want as a placeholder.
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
echo $this->Form->dateTime('Contact.date', 'DMY', '12',
array(
'empty' => array(
'day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
)
)
);
I believe you can only set a default date on the date fields.
echo $this->Form->input('close_time', array('type' => 'time', 'selected' => '13:30:00'));
Cookbook entry
For the benefit of anyone Googling to find the answer to this question 1+ years after it was asked, we can now simply do the following to set default placeholder (temporary) values in CakePHP forms that are easily replaced when clicked:
<?php echo $this->Form->input('Email', array(
'placeholder'=>'Enter Your Email Here'
)); ?>
I like to include 'label' => false as well to make the forms really minimalist.