Field with query should not be blank - symfony

How do I add validation constraints "Field is required" in symfony2 PostType class? Any suggestions? I am new in SF2 and I'm just editing what has been done by previous developer.
use Symfony\Component\Validator\Constraints\NotBlank;
class BlogPostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$blogPostCategories = BlogPostCategoryQuery::create()
->filterByBlogPost($builder->getData())
->find();
$categoryIds = array();
foreach ($blogPostCategories as $blogPostCategory) {
$categoryIds[] = $blogPostCategory->getCategory()->getId();
}
$queryOptions = array(
'option_status' => Categorypeer::STATUS_ACTIVE,
'option_category_ids' => $categoryIds
);
$categories = CategoryQuery::create()
->filterActiveCategoriesByOptions($queryOptions)
->find();
$builder->add('category_ids', 'model', array(
'label' => 'Category',
'mapped' => false,
'class' => 'Deal\MainBundle\Model\Category',
'query' => CategoryQuery::create()
->filterActiveCategoriesByOptions()
->orderByName(),
'property' => 'name',
'empty_value' => 'Select categories',
'empty_data' => null,
'required' => true,
'multiple' => true,
'data' => $categories,
'constraints' => array(
new NotBlank(array(
'message' => 'Your message can not be blank! Ouch!'
)),
)
));
Thank you

You should set required => true for this type from the parent form when calling (that's the form type that uses your BlogPostType):
$formBuilder->add('whatever', BlogPostType::class, ['required' => true]);
Also, you could set required = true by default for BlogPostType:
class BlogPostType extends AbstractType
{
// ...
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'required' => true
]);
}
}

You can do it with constraints key. Like this:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('message', TextareaType::class, array(
'required' => true,
'constraints' => array(
new NotBlank(array(
'message' => 'Your message can not be blank! Ouch!'
)),
)
))
;
}
do not forget to add uses:
use Symfony\Component\Validator\Constraints\NotBlank;

Related

Custom option in adding element of type

I have form and have two methods like this
/**
* #param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Justart\InvoiceBundle\Entity\InvoiceElement',
'validation_groups' => 'invoiceElementValidation',
'translation_domain' => 'JustartInvoiceBundle',
'invoice_revision' => false,
));
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
if (!empty($options['invoice_revision'])) {
$view->vars['invoice_revision'] = $options['invoice_revision'];
}
}
form name is InvoiceElementType
and I have form named InvoiceType which has elements so I wrote:
$builder->add('invoice_elements', CollectionType::class, array(
'entry_type' => InvoiceElementType::class,
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'label' => false,
'entry_options' => array(
'label' => false,
),
'required' => true,
'constraints' => array(
new NotBlank(array('groups' => array('STANDARD','PROFORM','FINISH','ADVANCE'))),
),
'invoice_revision' => $this->isRevision,
))
when I run it i get error
The option "invoice_revision" does not exist. Defined options are:
"action" ...
So what I do wrong?
And how can I send a variable from controller to type? I tried :
$form = $this->createForm(new Form\InvoiceType(1), $invoice);
construct of InvoiceType require integer value but I get error that createForm except string in first parameter
Try to put invoice_revision option under entry_options key:
'entry_options' => array(
'label' => false,
'invoice_revision' => $this->isRevision,
),

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

required don't work for repeated field type in EventSubscriber

I created form EditFormType with code:
// ...
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
$builder->addEventSubscriber(new UnsetReadOnlyForEmailField());
}
// ...
and UnsetReadOnlyForEmailField:
// ...
public static function getSubscribedEvents()
{
return array(
FormEvents::PRE_SET_DATA => 'preSetData'
);
}
public function preSetData(FormEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
if ($data === null) {
return;
}
if ($data->getId() === null) {
$form->add(
'plainPassword',
'repeated',
array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle', 'required' => true),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
)
);
}
}
// ...
Unfortunately required for repeated field doesn't work, field is't required. Any suggestions? If I do it wrong, then please write to set the fields in the form are required or not, depending on the form to edit or add. On add form required, on edit form not required.
I think you can do it in such way:
$form->add(
'plainPassword',
'repeated',
array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array(
'label' => 'form.password',
'required' => true
),
'second_options' => array(
'label' => 'form.password_confirmation',
'required' => true
),
'invalid_message' => 'fos_user.password.mismatch',
)
);

Update a entity/database record using form builder in Symfony 2

I have selected an entity/record from the database and pass it to my form. The form loads correctly, but when I hit save it adds a new record instead of updating the existing one.
PlanController.php
public function editPlanAction($id, Request $request)
{
$em = $this->getDoctrine()->getManager();
$plan = $em->getRepository('etrakOnlineOrderProcessingBundle:Plan')->find($id);
if (!$plan && !$request->isMethod('POST')) {
throw $this->createNotFoundException(
'No plan found for id ' . $id
);
}
$form = $this->createForm(new PlanType(), $plan);
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$editPlan = $form->getData();
$em->flush();
}
}
return $this->render('etrakCustomerServiceBundle:Plan:edit.html.twig', array('form' => $form->createView()));
}
routing.yml
etrak_customer_service_plan_edit:
pattern: /plan/edit/{id}
defaults: { _controller: etrakCustomerServiceBundle:Plan:editPlan }
PlanType.php
namespace etrak\CustomerServiceBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class PlanType extends AbstractType
{
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'etrak\OnlineOrderProcessingBundle\Entity\Plan',
'cascade_validation' => true,
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$termsConditionsArray = array("NONE" => "No Contract", "1 Year Contract" => "1 Year Contract", "2 Year Contract" => "2 Year Contract");
$billingFrequencyArray = array("1" => "Monthly", "6" => "6 Months", "12" => "Yearly");
// Create the form
$builder->add('name', 'text', array('label' => 'Plan Name: ', 'required' => false));
$builder->add('description', 'text', array('label' => 'Plan Description: ', 'required' => false));
$builder->add('termsConditions', 'choice', array('choices' => $termsConditionsArray, 'label' => 'Terms & Conditions', 'required' => false));
$builder->add('amount', 'text', array('label' => 'Plan Price: ', 'required' => false));
$builder->add('affinity', 'choice', array('choices' => array('0' => 'Yes', '1' => 'No'), 'label' => 'Affinity? ', 'expanded' => true, 'required' => false));
$builder->add('deactivationFee', 'text', array('label' => "Deactivation Fee: ", 'required' => false));
$builder->add('recurringInMonths', 'choice', array('choices' => $billingFrequencyArray, 'label' => 'Billing Frequency: ', 'required' => false));
$builder->add('pricingTier', 'entity', array(
'class' => 'etrakOnlineOrderProcessingBundle:pricingTier',
'property' => 'name',
'label' => "Select Pricing Tier: "
));
$builder->add('activeStartDate', 'datetime', array('label' => "Effective Start Date: ", 'required' => false));
$builder->add('activeEndDate', 'datetime', array('label' => "Effective End Date: ", 'required' => false));
}
public function getName()
{
return 'plan';
}
}
Any help would be greatly appreciated!
The problem is that you not posting the id of the plan object. Hence a new one is being created when you do the getData. Add id as a hidden field or add it to your post url in the template. Actually, I don't think the hidden field will work. Probably need to just update the post url.
if ($form->isValid()) {
$editPlan = $form->getData();
$em->persist($editPlan);
$em->flush();
}
Maybe you forget to persist entities.

Resources