Validate choice list in Symfony2 - php

I have a choice list where user can choose one value, but there I even set an empty value if the user doesn't select anything.
The form does not have model, to use #Assert annotation with it, and the choice field is optional, so in some case it will be hidden and need to be validated only if showed to user.
How I can validate this field? When I set it to required in my form type it didn't help (If I am right required equal to true by defaut). Where is my problem?

You need to add the NotBlank validator to your field.
You can add a validator directly to your field, like this:
$this->createFormBuilder()
->add('exampleField', 'choice', array(
'label' => 'Label',
'constraints' => array(
new NotBlank(),
),
))
[...]

Related

Add custom form field that is not in entity - Sonata admin

I'm using Sonata Admin in my project. I need to render a field that doesn't belong to entity.
Consider entity User with fields username & password. But I also need a extra field as hobby in form but it is not needed in User entity.
$formMapper
->add('username')
->add('password')
->add('hobby');
But I'm getting the symfony error as,
Neither the property "hobby" nor one of the methods "getHobby()", "hobby()", "isHobby()", "hasHobby()", "__get()" exist and have public access in class "App\Entity\User".
How can I solve this? Thanks in advance!!
This answer for Symfony2 should still hold true if I am not mistaken: How to add additional non-entity fields to entity form in Symfony2
In symfony 2.1+, use mapped:
$form = $this->createFormBuilder($promo)
->add('code', 'text')
->add('image', 'file', array(
"mapped" => false,
))
->getForm();
https://symfony.com/doc/current/reference/forms/types/entity.html#mapped
type: boolean default: true
If you wish the field to be ignored when reading or writing to the
object, you can set the mapped option to false.
So for your case it should be something like:
$formMapper
->add('username')
->add('password')
->add('hobby', null, [
'mapped' => false
]);

Zend2 InputFilter: not required as default setting

I wrote an input filter for a Form by inheriting from InputFilter (class CustomInputFilter extends Zend\InputFilter\InputFilter).
By default, each form element is required. This means that I have to set required to False explicitly for all optional elements. How can I change the InputFilter such that all form elements are optional by default?
I use the Zend 2 framework.
I am not sure about it is possible the way you want using InputFilter. I think this implies filtering and validating data. If any input field does not need to be required, then ZF have had the option ('required' => false). See another approach
$inputFilter->add(array(
'name' => 'title',
'required' => true,
'allow_empty' => true,
));
So why is this? This is something that input field must exist but you can keep it empty. If you then put invalid data in it can then validate data too.

Validating a date field in a sub-form

I have a main form that includes a number of sub-forms. One of the sub-forms contains a pair of date fields for entering a date range. I have created the entity classes and the form classes, and have updated services.yml appropriately.
The form renders fine. The problem is that the date fields are not being validated when the form is submitted. I can leave them blank or put anything in them that I like and I never get a validation error. I've tested validation of a date field in the top-level form and it worked as expected.
For testing I created a simple form and sub-form. The main test form has two fields: a text field and a sub-form field. The sub-form has two fields, a date field and a check box field.
As for the real case, I've created the entity and form classes and updated services.yml. The form displays fine. The date field fails to generate any errors when the form is submitted with an invalid date.
I have tried specifying validation with annotations in the entity classes, a constraints attribute in the $builder->add() method call, and both at the same time ;-)
The current add() call for the date field looks like this:
...
->add( 'date',
'date',
[
'attr' => [ 'placeholder' => 'a date (mm/dd/yyyy)' ],
'error_bubbling' => true,
'format' => 'MM/dd/yyyy',
'html5' => false,
'input' => 'datetime',
'invalid_message' => 'Invalid date (use mm/dd/yyyy)',
'label' => false,
'widget' => 'single_text',
'constraints' =>
[
new NotBlank(),
new Type( '\DateTime' )
]
] )
...
Suggestions?
Environment:
- PHP V5.5.9
- Symfony V2.7.4
- Twig V1.21.2
When you add SubFormType to MainForm do the following to validate sub forms:
$builder->add('sub_form', new SubFormType, array(
'constraints' => array(
new Valid()
));
I hope this helps :)
In addition to adding a Valid() constraint to the sub-form field in the main form, it comes down to the error_bubbling attributes.
The fields in the sub-form need to be set error_bubbling true to move any errors up to the sub-form field in the main form.
The sub-form field in the main form needs to set error_bubbling false to associate any sub-form errors with the sub-form field.
Through the use of a debugger and judicious {{ dump() }} tags, I finally realized that the sub-form errors were being added to the main form's global collection of errors.

Simple Registration on Symfony 2 wrong action

I used the Symfony 2 documentation to create a simple registration.
But now i have two little problems. The password fields, which I created with the Form Builder, be time in plain text.
The second problem is that the action of the form is not used the correct route. When I press the submit button, I get the standard page.
Has anyone of you an idea, which may be related?
If you still parts of the code needed, I like to add these.
Greetings
did you set it to a password type? eg
$builder->add('password', 'password)
Or can do repeated field
$builder->add('password', 'repeated', array(
'type' => 'password',
));
When you build the form do you set action?
$form = $this->createForm(new Type(), $type, array(
'action' => $this->generateUrl('your_route'),
));

How to disable validation from action in symfony?

Can anyone tell me how can we disable validation from action? Actually, I wanted to disable validation dynamically based on certain condition from action. I don't know how can we do that? Please help me.
My For validator as below:
$this->setValidators(
array(
'search_text' => new sfValidatorString(
array('required'=>true),
array('required' => 'Please enter keyword')),
'field_type' => new sfValidatorString(
array('required'=>true),
array('required' => 'Please select an option')),
)
);
I want to disable above validation in action dynamically.
Please help me.
You can create an enable_validation (or whatever name you want) option for your form. The form constructor accepts a $options array. In your action, you pass an array which would look like this : array('enable_validation option' => false)
Then, in your form, use the getOption() method to retrieve this option and set the validators accordingly.

Categories