Symfony empty date field is not required with single_text widget - symfony

I created form with date field using single_text widget.
I'm choose this one over choice because I'm using Bootstrap datepicker
Problem I have is that field is always populated with current date instead of being empty.
According to this it should work if i set required to false but id does not.
I tried setting empty_value to empty string same for data but in first case nothing happened probably because this option is for choice fields. In second case I'm getting exception "Expected argument of type "\DateTime", "string" given"
I tried using DataTransformer but didn't make any difference. I found out that for data fields value always goes through DateTimeToLocalizedStringTransformer and if I understand it correctly it is returning empty string if empty value so problem is somewhere further after datatransformers.
One more thing I tried is to set value using attr array and it worked unfortunately the side effect was that populating form with some values doesn't affect date field.
Is there any way to set default value of data field as empty with single_text widget?
Here goes a code
<?php
namespace Psw\AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
use Psw\AdminBundle\Form\DataTransformer\EmptyDateTransformer;
use Psw\AdminBundle\Form\DataTransformer\EmptyDateViewTransformer;
class OrdersFilterType extends AbstractType
{
private $admin;
public function __construct($admin=false) {
$this->admin = $admin;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('client', 'entity', array(
'class' => 'PswAdminBundle:User',
'required' => false,
'multiple' => false,
'label' => 'orders.client',
'empty_value' => 'orders.allclients',
'query_builder' => function(EntityRepository $er) {
$qb = $er->createQueryBuilder('u');
return $qb->where($qb->expr()->like('u.roles', '?0'))
->setParameters(array('%ROLE_CLIENT%'));
}
));
if($this->admin) {
$builder->add('staff', 'entity', array(
'class' => 'PswAdminBundle:User',
'required' => false,
'multiple' => false,
'label' => 'orders.staff',
'empty_value' => 'orders.allStaff',
'query_builder' => function(EntityRepository $er) {
$qb = $er->createQueryBuilder('u');
return $qb->where($qb->expr()->like('u.roles', '?0'))
->orWhere($qb->expr()->like('u.roles', '?1'))
->orWhere($qb->expr()->like('u.roles', '?2'))
->setParameters(array('%ROLE_STAFF%','%ROLE_ADMIN%','%ROLE_SUPER_ADMIN%'));
}
));
}
$builder->add('start', 'date', array(
'label' => 'orders.start',
'widget' => 'single_text',
'required' => false,
))
->add('end', 'date', array(
'label' => 'orders.end',
'widget' => 'single_text',
'required' => false,
))
->add('min', null, array(
'label' => 'orders.min',
'required' => false,
))
->add('max', null, array(
'label' => 'orders.max',
'required' => false,
));
}
public function getDefaultOptions(array $options)
{
$options = parent::getDefaultOptions($options);
$options['csrf_protection'] = false;
return $options;
}
public function getName()
{
return 'psw_adminbundle_ordersfiltertype';
}
}

Related

Add dynamically option to all fields of some type of symfony form

I have many fields of type subclass of EntityType, something like this:
$builder->add('managers', SubclassEntityType::class, [
'class' => 'User',
'choice_label' => 'Managers',
'required' => false,
'query_builder' => function (UserRepository $er) {
return $er->getManagersQueryBuilder();
},
'multiple' => true,
]);
$builder->add('types', SubclassEntityType::class, [
'class' => 'Type',
'choice_label' => 'Types',
'required' => false,
'query_builder' => function (TypesRepository $er) {
return $er->getManagersQueryBuilder();
},
'multiple' => true,
]);
Could I dynamically add option (option of fields in select, not option of form) to all fields of the same type like this
(Empty) => 'empty? I don't want to customize it for each field. I need this option to filter allow to add to field null values, for examples, find entities to which not manager is assigned.
Would it be easier to solve this if it were subclass of ChoiceType?
I tried to subclass ChoiceType и add empty option in buildView, but in this case validation fails as well. Does anybody know how to add option and make validation work? It looks like adding option in buildView doesn't solve the problem.
if isn't multiple ,You can achieve that by defining tow options placeholder and empty_data
class YourType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('managers', SubclassEntityType::class, [
'placeholder' => 'Please choose a manager..',
'empty_data' => null,
'class' => 'User',
'choice_label' => 'Managers',
'required' => false,
'query_builder' => function (UserRepository $er) {
return $er->getManagersQueryBuilder();
},
]);
$builder->add('types', SubclassEntityType::class, [
'placeholder' => 'Please choose a type..',
'empty_data' => null,
'class' => 'User',
'class' => 'Type',
'choice_label' => 'Types',
'required' => false,
'query_builder' => function (UserRepository $er) {
return $er->getManagersQueryBuilder();
},
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
// ...
]);
}
}
For multiple choices we can use finishView method for creating new choices and add them on children of your type.
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
class YourType extends AbstractType
{
// If you have in the future other type Entity Type and you want to add option empty
// You can just add it on this list
private const OPTION_EMPTY_ENTITIES_TYPE = [
// child name => 'Message on option empty',
'managers' => 'Select a manager..',
'types' => 'Select a type..',
];
private const OPTION_EMPTY_KEY ='option-empty';
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('managers', SubclassEntityType::class, [
'class' => 'User',
'choice_label' => 'Managers',
'required' => false,
'query_builder' => function (UserRepository $er) {
return $er->getManagersQueryBuilder();
},
'multiple' => true,
]);
$builder->add('types', SubclassEntityType::class, [
'class' => 'Type',
'choice_label' => 'Types',
'required' => false,
'query_builder' => function (UserRepository $er) {
return $er->getManagersQueryBuilder();
},
'multiple' => true,
]);
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
// remove all options we has add on finishView
foreach (self::OPTION_EMPTY_ENTITIES_TYPE as $childName => $optionMessage) {
if (false !== ($key = array_search(self::OPTION_EMPTY_KEY ,$data[$childName]))) {
unset($data[$childName][$key]); // example $data['managers'][0] => option-empty
}
}
$event->setData($data);
});
}
public function finishView(FormView $view, FormInterface $form, array $options)
{
foreach (self::OPTION_EMPTY_ENTITIES_TYPE as $childName => $optionMessage) {
// value option-empty is not a valid option of EntityType X ,otherwise if user select this option
// this form is invalid...
$newChoice = new ChoiceView(null, self::OPTION_EMPTY_KEY, $optionMessage);
array_unshift($view->children[$childName]->vars['choices'], $newChoice);
}
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
// ..
]);
}
}

add class to subset of symfony checkboxes

I've made a form in Symfony that is not bound to an entity. One of the form elements is a set of checkboxes, fieldtypes. Which fieldtype checkboxes show should depend on the value of the searchby field. I want to add a class to each checkbox to use as a show/hide hook for the javascript I will add later. But so far as I can see, the choices form element only allows for an overall class to be applied.
Here's the relevant bit of my form:
$form = $this->createFormBuilder()
->add('searchby', 'choice', array(
'label' => 'browse.label.searchby',
'choices'=>array(
'name' => 'browse.searchby.name',
'location' => 'browse.searchby.location',
'classification' => 'browse.searchby.classification'
),
'required' => true,
'multiple' => false,
'expanded' => true,
))
->add('fieldtypes', 'choice', array(
'label' => 'browse.label.fieldtypes',
'choices' => array(
'extensionAttribute12' => 'person.label.position.fr',
'title' => 'person.label.position.eng',
'l' => 'person.label.city',
'st' => 'person.label.province',
'co' => 'person.label.country',
'givenname' => 'person.label.firstname',
'sn' => 'person.label.lastname',
'name' => 'person.label.fullname',
),
'required' => true,
'multiple' => true,
'expanded' => true
));
If I want to add the class 'searchbyname' to the radiobuttons created from $options['choices']['givenname'], $options['choices']['sn'], $options['choices']['name'], how would I go about it?
After seeing that choices could be declared like this:
'choices' => array(
'classification' => array(
'extensionAttribute12' => 'person.label.position.fr',
'title' => 'person.label.position.eng',
),
'location' => array(
'l' => 'person.label.city',
'st' => 'person.label.province',
'co' => 'person.label.country',
),
'name' => array(
'givenname' => 'person.label.firstname',
'sn' => 'person.label.lastname',
'name' => 'person.label.fullname',
)
),
where the key to each sub array would be used as an <optgroup> label in a <select>; and after attempting to modify the Twig template (which was every bit a painful as #Cerad said it would be), I tried extending the ChoiceType class by creating a form type extension.
My solution is inefficient since I'm modifying the child views after they are created and because I had to include all the code from ChoiceType::finishView. I can't see how a child view is created. There is a line in ChoiceType::buildForm that reads $remainingViews = $options['choice_list']->getRemainingViews();, but since $options['choices'] was input as an array, I don't know what class getRemainingViews() is being called from.
At any rate, here it is:
<?php
namespace Expertise\DefaultBundle\Form\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ChoiceTypeExtension extends AbstractTypeExtension
{
/**
* #return string The name of the type being extended
*/
public function getExtendedType()
{
return 'choice';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setOptional(array('optgroup_as_class'));
$resolver->setDefaults(array('optgroup_as_class'=>false));
}
public function finishView(FormView $view, FormInterface $form, array $options)
{
if ($options['expanded']) {
// Radio buttons should have the same name as the parent
$childName = $view->vars['full_name'];
// Checkboxes should append "[]" to allow multiple selection
if ($options['multiple']) {
$childName .= '[]';
}
foreach ($view as $childView) {
$childView->vars['full_name'] = $childName;
if($options['optgroup_as_class']){
foreach($options['choices'] as $optclass => $choices){
if(!is_array($choices)) continue;
foreach($choices as $value => $label){
if($childView->vars['value'] == $value && $childView->vars['label'] == $label) {
$childView->vars['attr']['class'] = $optclass;
break 2;
}
}
}
}
}
}
}
}
Add it as a service, use the "optgroup" choices format and set optgroup_as_class to true.
I'd love to see a more efficient method.

Label not replaced with correct value in prototype from sonata admin bundle collection field

My collection is made of this type
<?php
namespace Gustaw\AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class AttributeValueType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('value')
->add('translations', 'a2lix_translations');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Gustaw\AdminBundle\Entity\AttributeValue',
'label' => false,
));
}
public function getName()
{
return 'attribute_value_type';
}
}
And this is my form
public function configureFormFields(FormMapper $formMapper) {
$formMapper->with('General')
->add('name', null, array('required' => true))
->add('translations', 'a2lix_translations', array(
'by_reference' => false,
'fields' => array(
'name' => array()
)
))
->add('custom', null, array(
'required' => false,
))
->add('category', 'entity', array(
'required' => true,
'class' => 'GustawAdminBundle:Category',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->orderBy('c.root', 'ASC')
->addOrderBy('c.lft', 'ASC');
},))
->end()
->with('Values')
//->add('values', 'sonata_type_collection')
->add('notcustomvalues', 'collection', array(
'type' => new AttributeValueType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => false,
'required' => false,
))
->end();
}
Problem is when adding new elements to collection. Each single AttributeValueType gets a label "__name__label__ *" when I don't want to have any label for this field as I set it to false.
I tried setting "prototype_name" hoping it will change something just to make it worse.
The only ideas that came to my mind are:
1st - create custom theme without label just for this one collection
2nd - edit base.js in SonataAdminBundle
2nd obviously is not really good option so I am left just with the first one.
Question is: Are there any other options I have?
Try to add: 'options' => array(label => 'Some Label');
Like this:
->add('notcustomvalues', 'collection', array(
'type' => new AttributeValueType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => false,
'required' => false,
'options' => array(
'label' => 'Some Label'
),
))

Symfony2 choice field not working

I asked a question here How to use Repository custom functions in a FormType but nobody anwsered, so i did a little digging and advanced a little but i still get this error:
Notice: Object of class Proxies\__CG__\Kpr\CentarZdravljaBundle\Entity\Category
could not be converted to int in /home/kprhr/public_html/CZ_Symfony/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php line 457
Now this is how my CategoryType looks like:
<?php
namespace Kpr\CentarZdravljaBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Bridge\Doctrine\RegistryInterface;
class CategoryType extends AbstractType
{
private $doctrine;
public function __construct(RegistryInterface $doctrine)
{
$this->doctrine = $doctrine;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Kpr\CentarZdravljaBundle\Entity\Category',
'catID' => null,
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$someId = $builder->getData()->getId();
$param = ($someId) ? $someId : 0;
$catID = $options['catID'];
$builder->add('name', 'text', array('attr' => array('class' => 'span6')));
$builder->add('file', 'file', array('image_path' => 'webPath', 'required' => false));
$builder->add('parent', 'choice', array(
'choices' => $this->getAllChildren($catID),
'required' => false,
'attr' => array('data-placeholder' => '--Izaberite Opciju--'),
));
$builder->add('tags', 'tag_selector', array(
'required' => false,
));
$builder->add('status', 'choice', array(
'choices' => array('1' => 'Aktivna', '0' => 'Neaktivna'),
'required' => true,
));
$builder->add('queue', 'text', array('attr' => array('class' => 'span3')));
}
private function getAllChildren($catID)
{
$choices = array();
$children = $this->doctrine->getRepository('KprCentarZdravljaBundle:Category')->findByParenting($catID);
foreach ($children as $child) {
$choices[$child->getId()] = $child->getName();
}
return $choices;
}
public function getName()
{
return 'category';
}
}
I am accessing the CategoryRepository function findByParenting($parent) from the CategoryType and I am getting the array populated with accurate data back from the function getAllChildren($catID) but the error is there, i think that Symfony framework is expecting an entity field instead of choice field, but dont know how to fix it.
I also changet the formCreate call in the controller giving $this->getDoctrine() as an argument to CategoryType():
$form = $this->createForm(new CategoryType($this->getDoctrine()), $cat, array('catID' => $id));
Ok i managed to resolve the dilemma. The answer was easy all I had to do is change
$builder->add('parent', 'choice', array(
'choices' => $this->getAllChildren($catID),
'required' => false,
'attr' => array('data-placeholder' => '--Izaberite Opciju--'),
));
to this:
$builder->add('parent', 'entity', array(
'class' => 'KprCentarZdravljaBundle:Category',
'choices' => $this->getAllChildren($catID),
'property' => 'name',
'required' => false,
'attr' => array('data-placeholder' => '--Izaberite Opciju--'),
));
And change the getAllChildren(..) function so that it returns objects
private function getAllChildren($catID)
{
$choices = array();
$children = $this->doctrine->getRepository('KprCentarZdravljaBundle:Category')->findByParenting($catID);
foreach ($children as $child) {
$choices[$child->getId()] = $child->getName();
}
return $choices;
}
I changed it to:
private function getAllChildren($catID)
{
$children = $this->doctrine->getRepository('KprCentarZdravljaBundle:Category')->findByParenting($catID)
return $children;
}
Lots of thanks to user redbirdo for pointing out the choices option on an entity field.
It seems like you are doing something too much complicated.
You are on the right way when you write Symfony framework is expecting an entity field instead of choice field.
To do this, replace:
$builder->add('parent', 'choice', array(
'choices' => $this->getAllChildren($catID),
'required' => false,
'attr' => array('data-placeholder' => '--Izaberite Opciju--'),
));
by:
$builder->add('users', 'entity', array(
'class' => 'KprCentarZdravljaBundle:Category',
'property' => 'name',
'query_builder' => function(EntityRepository $er) use($catID) {
return $er->findByParenting($catID);
},
'required' => false,
'empty_value' => '--Izaberite Opciju--'
));
(and you don't need getAllChildren($catID) anymore unless used somewhere else)
http://symfony.com/doc/current/reference/forms/types/entity.html

Building Symfony 2 Custom Validator that Uses Multiple Fields

I'm building a custom validator that needs to validate the value from TWO form fields in the db in order to get this constraint to pass.
My question is this: the ContractValidator's validate method only has one $value in it's signature so how do I get access to the values from more than just a single field to do the validation?
Here is a typical custom validator:
namespace Acme\WebsiteBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class MyCustomValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
// check $value and return an error
// but in my case, i want the value from more than one form field to do a validation
// why? i'm checking that two pieces of information (ssn + dob year) match
// the account the user is registering for
}
}
Here's an example of a form class with some validations set:
namespace ACME\WebsiteBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Regex;
use ACME\WebsiteBundle\Validator\Constraints\UsernameAvailable;
class AccountRegistration extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('ssn', 'number', array(
'max_length' => 9,
'required' => true,
'error_bubbling' => true)
);
$builder->add('year_of_birth', 'choice', array(
'choices' => range(date("Y") - 100, date("Y")),
'required' => true,
'empty_value' => 'Select ...',
'label' => 'Year of Birth',
'error_bubbling' => true)
);
$builder->add('username', 'text', array(
'required' => true,
'error_bubbling' => true)
);
$builder->add('password', 'password', array(
'max_length' => 25,
'required' => true,
'error_bubbling' => true)
);
$builder->add('security_question', 'choice', array(
'empty_value' => 'Select ...',
'choices' => array(),
'label' => 'Security Question',
'required' => true,
'error_bubbling' => true)
);
$builder->add('security_question_answer', 'text', array(
'label' => 'Answer',
'required' => true,
'error_bubbling' => true)
);
}
public function getName()
{
return 'account_registration';
}
public function getDefaultOptions(array $options)
{
$collectionConstraint = new Collection(array(
'allowExtraFields' => true,
'fields' => array(
'ssn' => array(new MinLength(array('limit' => 9, 'message' => 'too short.')), new NotBlank()),
'year_of_birth' => array(new NotBlank()),
'username' => array(new NotBlank(), new UsernameAvailable()),
'password' => array(new NotBlank(), new Regex(array(
'message' => 'password must be min 8 chars, contain at least 1 digit',
'pattern' => "((?=.*\d)(?=.*[a-z]).{8,25})"))
),
'security_question' => array(new NotBlank()),
'security_question_answer' => array(new NotBlank()))
)
);
return array(
'csrf_protection' => true,
'csrf_field_name' => '_token',
'intention' => 'account_registration',
'validation_constraint' => $collectionConstraint
);
}
}
Any custom validator that extends ConstraintValidator has access to the $context property. $context is an instance of ExecutionContext that gives you access to submitted data:
Example:
<?php
namespace My\Bundle\MyBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class AppointorRoleValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
$values = $this->context->getRoot()->getData();
/* ... */
}
}
You need to use CLASS_CONSTRAINT as described in the cookbooks. You then get passed the whole class and can use any property of this class. In your example above, this would mean that instead of the $value beeing one string/integer, it would be the whole object you want to validate.
The only thing you need to change is getTargets() functions, which has to return self::CLASS_CONSTRAINT.
Also make sure that you define your validator on class level, not on property level. If you use annotations this means that the validator must be described above the class defnition, not above one specific attribute definition:
/**
* #MyValidator\SomeValidator
*/
class MyClass {
}

Resources