Symfony2 Form Builder custom unmapped field constraint - php

I am simply trying to add an email constraint to the unmapped field below and for some reason in the controller action it isn't picking it up. Is there something else I need to be doing?
Form Class
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', null, [
'label' => false,
'attr' => ['placeholder' => 'Name*']
]);
$builder->add('email', null, [
'mapped' => false,
'label' => false,
'attr' => ['placeholder' => 'Email*'],
'constraints' => [
new Email(["message" => "Please enter a valid Email Address"])
]
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Test\AppBundle\Entity\Feedback',
'cascade_validation' => true,
'validation_groups' => function(Form $form)
{
$feedback = $form->getData(); /** #var Feedback $feedback */
if ($feedback->getType() == Feedback::Type_Feedback)
return Feedback::ValidationGroup_Feedback;
else if ($feedback->getType() == Feedback::Type_Link)
return Feedback::ValidationGroup_Link;
throw new \Exception("Couldn't generate a valid Validation Group for FeedbackForm.php");
},
));
}
/**
* #return string
*/
public function getName()
{
return 'site_feedback_form';
}
Controller action
public function feedbackSiteSubmitAction(Request $request)
{
// The form award
$feedback = new Feedback();
// Create the form so we can bind send form values to it
$form = $this->createNewFeedbackForm($feedback);
// Bind form values
$form->handleRequest($request);
// Save
if ($form->isValid())
{
// Add the submission to a member
$email = $form->get('email')->getData();
$email = strtolower($email);
$member = $this->getMemberRepository()->loadByEmail($email);
if (!$member)
$member = $this->generateNewPersistedMember($email);
// Update the mail property
$member->setReceiveEmail(!$form->get('dontReceiveAlerts')->getData());
// Add the feedback to the member
$feedback->setMember($member);
// Persist the Feedback
$this->getEntityManager()->persist($feedback);
// Commit
$this->getEntityManager()->flush();
// Response
return $this->jsonSuccess([
'html' => $this->renderView('TestAppBundle:Site/partials:feedback_form_success.html.twig', [
'typeString' => $feedback->getType() == Feedback::Type_Feedback ? "Feedback" : "Resource suggestion"
])
]);
}
// Return errors
return $this->jsonError($this->getAllFormErrors($form));
}

In Symfony 2.8 upwards you do the following:
$builder->add('email', Symfony\Component\Form\Extension\Core\Type\EmailType::class);
Versions before that use:
$builder->add('email', 'email');
Then you add all your validation criteria via annotations in your Feedback class. See the options for EmailType here.

Related

Validation is skipping when submit the form with rest api

I have this form :
class RegistrationFormType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class, [
'constraints' => [
new NotBlank(),
]
])
->add('username')
->add('password')
;
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'app_user_register';
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
'allow_extra_fields' => true,
'csrf_protection' => false,
]);
}
}
My api in controller :
/**
* #Route("/api/register")
* #Method("POST")
* #param Request $request
*
* #return JsonResponse
*/
public function register(
UserService $userService,
Request $request
)
{
try {
return $userService->register(json_decode($request->getContent(), true));
} catch (\Exception $e) {
return $this->json([
'status' => Response::HTTP_INTERNAL_SERVER_ERROR,
'result' => $e->getMessage()
]);
}
}
And my function in service :
public function register($formData)
{
$user = new User();
$form = $this->formFactory->create(RegistrationFormType::class, $user);
$form->submit($formData);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($user);
$this->entityManager->flush();
return new JsonResponse([
'status' => Response::HTTP_OK,
'result' => true
]);
}
return new JsonResponse([
'status' => Response::HTTP_BAD_REQUEST,
'result' => FormErrorFormatter::getErrorsFromForm($form)
]);
}
When I tried to call the api /api/register in postman with
{
"username": "test1",
"email": "test1",
"password": "123456"
}
I get 200 code, but normally should drop an error because the email is not valid, as I put in form creation that the field email should be in the email format, even if I put an empty string in email I get the 200 code. So seems the validations is not working.
EmailType, as far as I can tell, has no default constraints. However, you override the constraints by demanding it's NotBlank which is definitely not the same as the Email constraint. the Form does add type="email" to the html, which the browser will enforce (which is technically unreliable, because the user can just turn it into a text field).
Solution is probably to use the Email constraint and set the required property to true.
Try :
->add('email', EmailType::class, [
'constraints' => [
new NotBlank(),
new Email(),
]
])
and add to your Entity :
/**
* #Assert\Email(
* message = "The email '{{ value }}' is not a valid email.",
* checkMX = true
* )
*/
protected $email;

How to add dynamic error for form validation symfony using createnamebuilder?

I create a my own form builder using createNamebuilder(). Just like this
$builder = $this->formFactory->createNamedBuilder($formName, $phoneNumberType, $data, $formOptions);
I have PhoneNumberType. inside of it I add to builder countryNumber and number.
$builder->add('countryNumber', CountryCodeType::class);
$builder->add('number', NumberType::class);
How to add constraint in number that need to be required? I try to use this
public function buildForm(FormBuilderInterface $builder, array $options)
{
$contactNumber = $builder->getData();
$countryCode = null;
if ($contactNumber instanceof ContactNumber) {
$countryCode = $contactNumber->getCountryNumber();
}
$builder->add('countryNumber', CountryCodeType::class, ['data' => $countryCode]);
$builder->add('number', NumberType::class, [
'required' => true,
'constraints' => [new NotBlank(['message' => 'Phone number is required.'])]
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => ContactNumber::class,
'error_bubbling' => true
]);
}
When the time I submit my form. It doesn't show the error message.
You are trying to add form after posting,
You need to use events
https://symfony.com/doc/current/form/dynamic_form_modification.html
or you can add constraints to entity after submitting empty data form will return errors.
use Symfony\Component\Validator\Constraints as Assert;
class ContactNumber
{
/**
* #Assert\NotBlank()
* ..
*/
private $number
}
and framework.yaml
framework:
validation: { enable_annotations: true }

Symfony checkbox for some entity

I have list developer, not all developer, developer who have some parameters by filter and I need create check box for this some developer and post developer in another action, in this action identification developer who have true checkbox, I try this but have null check develoepr
class SelectByIdentityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('id', 'entity', array(
'required' => false,
'class' => 'ArtelProfileBundle:Developer',
'property' => 'id',
'property_path' => '[id]', # in square brackets!
'multiple' => true,
'expanded' => true
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => null,
'csrf_protection' => false
));
}
/**
* #return string
*/
public function getName()
{
return 'select';
}
}
and action where I visible my developers
/**
* Finds and displays a Project entity.
*
* #Route("/{id}/show", name="admin_project_show")
* #Method({"GET", "POST"})
* #Template()
* #ParamConverter("entity", class="ArtelProfileBundle:Project")
*/
public function showAction($entity, Request $request)
{
//some logic
$developers = $profileRepository->findBySkillsAndTag($developer, $skill_form);
$form = $this
->createForm(
new SelectByIdentityType(),
$developers
)
->createView()
return array(
'entity' => $entity,
'form' => $form,
'developers' => $developers,
);
}
and if I check developer click SEND and then in the controller action which receives the POSTed data:
/**
* Send email for Developers.
*
* #Route("/{id}/send", name="admin_project_send_email")
* #Method({"GET", "POST"})
* #Template()
* #ParamConverter("entity", class="ArtelProfileBundle:Project")
*/
public function sendAction($entity, Request $request)
{
$em = $this->getDoctrine()->getManager();
$developer = $request->getSession()->get('developer');
$form = $this
->createForm(new SelectByIdentityType())
;
$form->bind($request);
if ($form->isValid()) {
$data = $form->getData();//$data['id'] I have 0
$ids = array();
foreach ($data['id'] as $developer) {
$ids[] = $entity->getId();
}
$request->getSession()->set('admin/foo_list/batch', $ids);
}
Why in $data['id'] I have 0 ??
and my template
{% for entity in developers %}
<tr>
<td>{{ form_widget(form.id[entity.id]) }}</td>
Any ideas ??
You can filter the entities displayed in an entity field by using the query_builder option. That way you can just normally render the whole form and you get a checkbox list of only the entities that your query returns. If you need to display it in a special way, you can override the form templates.
Unfortunately, as far as I know you can't use DQL or repository methods in the query_builder option. You have to write the query-builder code directly into the form definition.
An example:
$builder->add('id', 'entity', array(
'required' => false,
'class' => 'ArtelProfileBundle:Developer',
'multiple' => true,
'expanded' => true
'query_builder' => function($repo)
{
return $repo->createQueryBuilder('d')
->where('x = y');
}
));

symfony 2 how to pass array collection to input select

Hi i am tying pass array collection (method getProjects() returns it) to form (select input) and fail. This code returns exception - A "__toString()" method was not found on the objects of type "Tasker\WebBundle\Entity\Project" passed to the choice field.
Can anybody help? Is needed transformer? Or what is right way?
Controller:
/**
* #Route("/pridaj", name="web.task.add")
* #Template()
*/
public function addAction(Request $request)
{
$task = new Task;
/** #var User $loggedUser */
$loggedUser = $this->get('security.token_storage')->getToken()->getUser();
$form = $this->createForm(new AddTaskType(), $task, ['user' => $loggedUser]);
if ($form->handleRequest($request) && $form->isValid()) {
// some stuff
}
return [
'form' => $form->createView()
];
}
Form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('project', 'entity', [
'label' => 'Projekt:',
'class' => 'TaskerWebBundle:Project',
'choices' => $options['user']->getProjects(),
'placeholder' => 'Označte projekt',
])
// ....
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setRequired(array(
'user',
));
$resolver->setDefaults(array(
'user' => null,
));
}
just add __ToString() to your Project class
Tasker\WebBundle\Entity\Project
class Project
{
....
function __toString() {
return $this->getName(); //or whatever string you have
}
}
I wanted to add another answer, because you do not have to add __toString() to your Project class. The Symfony entity field type allows you to specify which property/field to use for displaying. So instead of __toString() you could specify the property in the form configuration like so:
$builder
->add('project', 'entity', [
'label' => 'Projekt:',
'class' => 'TaskerWebBundle:Project',
'choices' => $options['user']->getProjects(),
'placeholder' => 'Označte projekt',
'property' => 'name'
])
If you check this part of the Symfony documentation you will see that __toString() is automatically called only if you do not specify the property.

how to make form ignore password fields if blank (symfony2 forms)

I have a simple user manager in my backend, and I want to be able to edit the user without setting a new password/repeating the old password every time.
Right now if I leave the password fields blank when editing a user, symfony2 complains that a password must be entered, and of course I want this functionality when I register new users, but when I edit them, I'd like for the form to just ignore the password boxes if they aren't filled out.
How is this accomplished?
For someone else's reference, I worked this one out this way.
My formType:
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('username', 'text', array('label' => 'Servernamn '))
->add('plainPassword', 'repeated', array('type' => 'password', 'first_name' => 'Lösenord för server ', 'second_name' => 'Upprepa lösenord för server',));
$builder-> addValidator(new CallbackValidator(function(FormInterface $form){
$username = $form->get('username')->getData();
if (empty($username)) {
$form['username']->addError(new FormError("Du måste ange ett namn för servern"));
}
}));
}
My updateAction:
public function updateServerAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('BizTVUserBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Container entity.');
}
$originalPassword = $entity->getPassword();
$editForm = $this->createForm(new editServerType(), $entity);
$request = $this->getRequest();
$editForm->bindRequest($request);
if ($editForm->isValid()) {
$plainPassword = $editForm->get('plainPassword')->getData();
if (!empty($plainPassword)) {
//encode the password
$encoder = $this->container->get('security.encoder_factory')->getEncoder($entity); //get encoder for hashing pwd later
$tempPassword = $encoder->encodePassword($entity->getPassword(), $entity->getSalt());
$entity->setPassword($tempPassword);
}
else {
$entity->setPassword($originalPassword);
}
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('Server'));
}
Thus updating my users password should it be set, otherwise keep the original password.
On your entity setter for the password property make it like this:
/**
* Set password
*
* #param string $password
* #return User
*/
public function setPassword($password)
{
if (!is_null($password)) {
$this->password = $password;
}
return $this;
}
The trick is to check if the parameter being passed is empty and only setting if it's not.
If you need add required option to $options array. Example:
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('username', 'text', array('label' => 'Servernamn '))
->add('plainPassword', 'repeated', array(
'type' => 'password',
'first_name' => 'password',
'second_name' => 'repeat_password',
'required' => false,
));
}
What I did was to leave the UserType as is and only remove the password field in the controller:
/**
* #Route("/users/edit/{username}", name="user_edit")
*/
public function editAction(Request $request, User $user)
{
$form = $this->createForm(UserType::class, $user);
// Remove the password field when editing the user
// Otherwise it must be entered each time the user is edited
// We can change the password via a special edit-user-password page
$form->remove('password');
$form->handleRequest($request);
if ($form->isValid()) {
// ...
}
}
This way you can have a single UserType, reusable in the editAction and the newAction.
Be sure to add the line $form->remove('password'); before $form->handleRequest($request);.
There is an article gives many options to achieve your request, from the Symfony2 cookbook, in particular, the section below worked for me:
Customizing your Form based on the underlying Data
Below is the implementation:
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
$product = $event->getData();
$form = $event->getForm();
// check if the Product object is "new"
// If no data is passed to the form, the data is "null".
// This should be considered a new "Product"
if (!$product || null === $product->getId()) {
$form->add('name', 'text');
}
});
}
For the validation use 2 different validation groups and set the correct one by the callable function.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'oldPassword',
PasswordType::class,
[
'constraints' => new UserPassword([
'groups' => 'profile_password',
]),
'mapped' => false,
'required' => false,
]
);
$builder->add(
'plainPassword',
PasswordType::class,
[
'constraints' => new NotBlank([
'groups' => 'profile_password',
]),
'mapped' => false,
'required' => false,
]
);
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'validation_groups' => function (FormInterface $form) {
$newPassword = $form->get('plainPassword')->getData();
$oldPassword = $form->get('oldPassword')->getData();
if ($oldPassword || $newPassword) {
return ['profile', 'profile_password'];
} else {
return ['profile'];
}
},
]
);
}
Symfony 5 method:
Inside a controller class:
/**
* #Route("/user/{id}/edit")
*/
public function updateUser(
$id, // See 'Route' annot., needed to `find()` User, adjust to your needs
User $user,
Request $request,
UserPasswordEncoderInterface $passwordEncoder,
UserRepository $userRepository
): Response {
$form = $this->createForm(UserFormType::class, $user);
// if password field is required by default, we have to `add()` it again
// with `required` set to `false`
$form->add(
'plainPassword',
PasswordType::class,
[
// do not use Constraint NotBlank()
'required' => false,
'mapped' => false
]
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entity = $userRepository->find($id); // as above, adjust to your needs
if (!$entity) {
throw $this->createNotFoundException('User does not exist!');
}
$originalPassword = $entity->getPassword();
/** #var User $user */
$user = $form->getData();
// keep original password if new one isn't provided
if (!empty($form['plainPassword']->getData())) {
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$form['plainPassword']->getData()
)
);
} else {
// set old password
$user->setPassword($originalPassword);
}
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return new Response('User updated!');
}
return $this->render(
'admin/user_edit.html.twig',
[
'requestForm' => $form->createView(),
]
);
}
You can do something with
if (empty($password)) {
//edit the password
}
Try fixing that into your form's PHP.

Categories