Symfony (Silex) radio choice form builder does not render attr - symfony

I'm trying to add attributes to my rendered radios
$builder
->add('myRadios', 'choice', array(
'choices' => array(
'one' => 'uno',
'two' => 'due'),
'multiple' => false,
'attr' => array('class' => 'testClass'),
'expanded' => true
the output is:
<div class="control-group">
<label class="control-label required">Myradios</label>
<div class="controls">
<label for="form_one_0" class="required radio">
<input type="radio" id="form_one_0" name="form[one]" required="required" value="uno" />
<span>Uno</span>
</label>
<label for="form_two_1" class="required radio">
<input type="radio" id="form_two_1" name="form[two]" required="required" value="due" />
<span>Due</span>
</label>
</div>
</div>
no references to class='testClass'
I can't find any issue online

Try this way Adam,
$form = $app['form.factory']->createBuilder('form')
->add('myRadios', 'choice', array(
'choices' => array(
'one' => 'uno',
'two' => 'due'),
'multiple' => false,
'expanded' => true,
'attr' => array('class' => 'testClass'),
))
->getForm();
it works:
<div id="form_myRadios" class="testClass">
<input type="radio" id="form_myRadios_0" name="form[myRadios]" required="required" value="one">
<label for="form_myRadios_0" class="required">uno</label>
<input type="radio" id="form_myRadios_1" name="form[myRadios]" required="required" value="two">
<label for="form_myRadios_1" class="required">due</label>
</div>

How does your twig code look like? And which form template do you use?
You use a custom form template because
<div class="control-group">
<label class="control-label required">Myradios</label>
<div class="controls">
is definitely not standard Symfony!

Related

How to add parent div with form builder in symfony

form created by form builder
<form name="post" method="post">
<div id="post">
<div class="form-group">
<input type="text" id="post_title" name="post[title]" required="required" class="form-control"/>
</div>
<div class="form-group">
<textarea id="post_content" name="post[content]" required="required" class="form-control"></textarea>
</div>
<div>
<button type="submit" id="post_postAdd" name="post[postAdd]" class="form-control btn-primary">Create</button>
</div>
<input type="hidden" id="post__token" name="post[_token]" value="ab35508c9.6MnqGe6WDVVVtkdRga_Ahtt5kDssuXri3JBx4HcVdOw.gKCQYdugYSw2_x88wN2y6Z5P_mtJyxaqm95GhkRDP52qoYFjucJOGyTuIA" />
</div>
</form>
i want this. How i create parent (div) elements one or more like below with form builder. Not in twig.
<div class="oneMore"> <!-- I need this element -->
<div class="iWantToAddThis"> <!-- I need this element -->
<div class="form-group">
...input...
</div>
<div class="form-group">
...input...
</div>
</div>
</div>
PostType.php. I created form like below.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'attr' => [
'class' => 'form-control'
],
'row_attr' => [
'class' => 'form-group'
]
])
->add('content',TextareaType::class, [
'attr' => [
'class' => 'form-control'
],
'row_attr' => [
'class' => 'form-group'
]
])
->add('postAdd', SubmitType::class, [
'attr' => [
'class' => 'form-control btn-primary'
]
]);
}
_form.html.twig (I don't want to add anything in twig)
{{ form(form) }}

Symfony - Asterisk on required label, bug on ChoiceType field

in my project Symfony 4 I want to put asterisk next to the labels whose fields are mandatory.
According to the doc ', there are several ways to do this: https://symfony.com/doc/4.0/form/form_customization.html#adding-a-required-asterisk-to-field-labels
But the CSS method is the easier.
So in my CSS file, I added this code :
label.required:after {
content: " *";
color: red;
font-weight: bold;
}
It works to a detail. In forms, when I have a ChoiceType field, the asterisk does not go on the main label but on all possible choices.
With this HTML render :
<div class="col-2">
<fieldset class="form-group">
<legend class="col-form-label required">Ajouter/Supprimer</legend>
<div id="role_choix">
<div class="form-check">
<input type="radio" id="role_choix_0" name="role[choix]" required="required" class="form-check-input" value="ajouter">
<label class="form-check-label required" for="role_choix_0">Ajouter</label>
</div>
<div class="form-check">
<input type="radio" id="role_choix_1" name="role[choix]" required="required" class="form-check-input" value="supprimer">
<label class="form-check-label required" for="role_choix_1">Supprimer</label>
</div>
</div>
</fieldset>
</div>
This is my formType :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('choix', ChoiceType::class, [
'label' => 'Ajouter/Supprimer',
'choices' => [
'Ajouter' => 'ajouter',
'Supprimer' => 'supprimer',
],
'multiple' => false,
'expanded' => true,
])
->add('role', ChoiceType::class, [
'label' => "Ajouter un rôle à cet utilisateur",
'choices' => [
'Admin' => 'admin',
'Direction' => 'direction',
'RH' => 'rh',
]
])
;
}
And my Twig :
{{form_start(form)}}
<div class="row items-align-center">
<div class="col-md-2">{{form_row(form.choix)}}</div>
<div class="col-md-4">{{form_row(form.role)}}</div>
</div>
<button type="submit" class="btn btn-primary">Enregistrer</button>
{{form_end(form)}}
So the bug is only on radio buttons
Can someone help me please ?
I know this is a very old question, but I found that this solution works for me:
label.required:not(.form-check-label):after {
content: "\2000*";
color: red;
}
You set the css property on label and that's working. What you want for radios is to put it on legend. You have to adapt the css selector.
Does the radio group and the select label share one class attribute ?

Woocommerce - How to change the html markup of checkout fields on the checkout page?

I use this hook:
add_action( 'woocommerce_after_checkout_billing_form', 'add_custom_field' );
function add_custom_field( $checkout ) {
echo '<div id="custom_field_wrapper">';
woocommerce_form_field( 'custom_field', array(
'type' => 'text',
'class' => array('custom_field_class'),
'label' => __('Custom Field'),
'placeholder' => __('Please input value ...'),
'required' => false, ),
$checkout->get_value( 'custom_field' ) );
echo '</div>';
}
and i get this markup:
<div id="custom_field_wrapper">
<p class="form-row custom_field_class" id="custom_field_field" data-priority="">
<label for="custom_field" class="">Custom Field</label>
<input type="text" class="input-text " name="custom_field" id="custom_field" placeholder="Please input value ..." value="">
</p>
</div>
But the required markup looks like this:
<div class="row order-form__row">
<div class="col-lg-3 col-md-4">
<label class="label" for="index-field">Индекс:</label>
</div>
<div class="col-lg-6 col-md-6">
<input class="input" type="text" name="index" id="index-field"/>
</div>
<div class="col-lg-3 col-md-2"><a class="field-after" href="javascript:void(0);" target="_blank">Забыли индекс?</a></div>
</div>
Kind smart people help solve the dilemma ... How to change the html markup according to the requirements?

bootstrap cakephp help needed

i am creating a form with cakephp2 and bootstrap everything is working fine but im stuck in little confusion and i cant fix this kindly any bootstrap or cake expert help me thanks,
Check this envelop its stretching upwards
here is my code
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">
<span class="glyphicon glyphicon-envelope"></span>
</span>
<?php echo $this->Form->input('email',
array(
'class' => 'form-control',
'type' => 'email',
'placeholder' => 'Enter Email'
)
); ?>
</div>
<!--<input type="email" class="form-control" id="email" placeholder="Enter email" required="required" /></div>-->
</div>
Pass the argument 'label' => false will prevent to display the label
Use
<?php echo $this->Form->input('email',
array(
'class' => 'form-control',
'type' => 'email',
'placeholder' => 'Enter Email',
'label' => false
)
); ?>
You should use label false in your code.
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">
<span class="glyphicon glyphicon-envelope"></span>
</span>
<?php echo $this->Form->input('email',
array(
'class' => 'form-control',
'type' => 'email',
'label'=>false,
'placeholder' => 'Enter Email'
)
); ?>
</div>
<!--<input type="email" class="form-control" id="email" placeholder="Enter email" required="required" /></div>-->
</div>

Wordpress wp_login_form Bootstrap

Is there a way to change the html output from wp_login_form(), so that is like the bootstrap form?
The wp_login_form looks like this:
<form class="form-grey" name="loginform" id="loginform" action="http://domain.loc/wp-login.php" method="post">
<p class="login-username">
<label for="user_login">Username</label>
<input type="text" name="log" id="user_login" class="input" value="" size="20">
</p>
<p class="login-password">
<label for="user_pass">Password</label>
<input type="password" name="pwd" id="user_pass" class="input" value="" size="20">
</p>
<p class="login-remember"><label><input name="rememberme" type="checkbox" id="rememberme" value="forever"> Remember Me</label></p>
<p class="login-submit">
<input type="submit" name="wp-submit" id="wp-submit" class="button-primary" value="Log In">
<input type="hidden" name="redirect_to" value="http://domain.loc/">
</p>
</form>
And Bootstrap Forms look like this:
<form class="form-inline">
<div class="form-group">
<label class="sr-only" for="exampleInputEmail3">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail3" placeholder="Email">
</div>
<div class="form-group">
<label class="sr-only" for="exampleInputPassword3">Password</label>
<input type="password" class="form-control" id="exampleInputPassword3" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
<button type="submit" class="btn btn-default">Sign in</button>
</form>
Try to use this :
Take your arguments as per the classes and IDs required :
$args = array(
'echo' => true,
'redirect' => property_list(),
'form_id' => 'loginform',
'label_username' => '',
'label_password' => '',
'label_remember' => __('Keep me logged in'),
'label_log_in' => __('SIGN IN'),
'id_username' => 'user_login',
'id_password' => 'user_pass',
'id_remember' => 'rememberme',
'id_submit' => 'wp-submit',
'remember' => true,
'value_username' => NULL,
'value_remember' => false
);
Now pass this $args array to function below :
<?php wp_login_form_custom($args);?>
Now you can use this function as defined below :
function wp_login_form_custom($args = array()) {
//you can use the values as $args['form_id'] and so on
//create you own HTML here with classes and IDs defined in the $args
}

Resources