Creating a credit card month/year input can be accomplished with a single database field but Symfony is smart enough to create multiple pull downs for each.  It is NOT smart enough to not require the programmer to exclude the “day” value.  So, what do you do?  There are many workarounds, including using some post rendering JQuery nujitsu, but that’s crazy.  Still, you have to include the “d” in the date’s format or you will get an error.

Since the day will be ignored, you have Symfony render the field then hide it with CSS.  Not as graceful as a solution you’d expect.  There are ways to create customized inputs, but something this simple I found this is a good enough solution in most cases.

Here’s how you add the credit card expiration date field to your form builder, which is usually created in the entity’s “form type”.


->add(
'ccExpirationDate',
'date',
array(
'format' =>'MMM-yyyy  d',
'years' => range(date('Y'), date('Y')+12),
'days' => array(1),
'empty_value' => array('year' => 'Select Year', 'month' => 'Select Month', 'day' => false)
)
)

// Example of outputting the form field in the TWIG template

{{  form_widget( form.payment.ccExpirationDate.day, { ‘attr’: { ‘style’: ‘display:none’ } } )  }}

Disclaimer — this was an idea used in a few different sites, not my original solution but I put it here because I am sure I’ll need to find a way to do this again.


If you are using the “FOSUserBundle” in Symfony, here is how you can check if a user is logged in:

if ( $this->get(‘security.context’)->isGranted(‘ROLE_USER’)) {
//User is logged in
}

if( $this->container->get(‘security.context’)->isGranted(‘IS_AUTHENTICATED_FULLY’) ){
// authenticated (NON anonymous)
}

For more information about user authentication in Symfony, visit the Friends of Symfony website at:  https://github.com/FriendsOfSymfony/FOSUserBundle.

If you are an organization in the Jacksonville Northeast Florida area looking for open source PHP web developers with experience in Symfony, Drupal, WordPress, Magento, or API integration experts, please contact me.  I have a list of competent web developers who I can recommend here in Jacksonville, Southwest Florida, and throughout the US, Canada, UK, Australia, and Germany as temporary web development consultants or remote web developers!


Every field in Symfony now is an instance of a form object.  To add custom errors in the controller upon form submission, you have to access the form object of the field then add the custom error to it.

$error = new formerror("Your field custom error message here.");
$form->get('fieldName')->addError($error);

//You will need to include the formerror class at the top of the file:

use Symfony\Component\Form\FormError


In Symfony, making a service means different classes and their functions can be called on as needed from the controller without cluttering up your controller and allowing you to separate useful code that can be called on from other places later.  That’s modularity, efficiency and the DRY principle (Don’t Repeat Yourself).

Here is a simple pattern to get you started in adding a service entry and then an example is provided how to call on the custom class.  This calls on a special class of methods meant to deal with processing payments, including cleaning credit card input of any characters except digits.

In this example, the file “ProcessPayment.php” is saved in the bundle’s /Form directory where I suggest making a special folder called “Handlers”.  Handlers, as in “form handlers,” actually signifies the opposite of what a repository does in Symfony.  A repository is meant to only accept parameters and return values, like a list of the 5 most current blog posts, etc.  Also, you don’t have to save your form handlers in a “/Handlers” folder like I have, but I find that helps organize code and segregate these more sensitive form functions being saved within the “/Forms” folder of the bundle.

Just a tip I find useful in remembering where things go.  When you first start Symfony, the file organization can be a little confusing.

In bundle services.yml:

parameters:
  process_payment.class:  Main\MainBundle\Form\Handlers\ProcessPayment
    process_payment.transport:  cleanCcInput  # This line is not necessary to work

services:
  process_payment:
    class:  "%process_payment.class%"
      calls:
        - [cleanCcInput,['']]

Code to call on service from controller:

$cc_full_num_cleaned = $this->get('process_payment')->cleanCcInput($cc_full_num);

It would be helpful if there were more solid examples of Symfony projects for those learning this powerful framework. If any developers have any recommendations, I would appreciate it, not just for me, but some friends and colleagues who are interested!

– Aaron Belchamber


Every field in a form in Symfony is an instance of the parent form. To add a custom error to the form, you must access the form object of the field then add an error into it. Here is how you can embed a custom error to a form field:

$error = new formerror("There is an error with the field");
$form->get('field')->addError($error);
// You will also need to include the formerror class at the top of your file: use Symfony\Component\Form\FormError