0

Working on MVC applications has made me realize that A LOT of some strings are repeated in every single form class. For instance take Symfony Forms, it is common to see stuff like this:

<?php 
class TaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('task')
            ->add('dueDate', null, ['required' => false]) // repeat for n fields
            ->add('save', SubmitType::class)
        ;
    }
}

Or laravel validators:

<?php
public function myMethod() {
    $request->validate([
        'title' => 'bail|required|unique:posts|max:255',
        'body' => 'required', // repeat for n fields
    ]);
}

Now, the keyword here is required, (though there could be other strings that repeat a lot), imagine a big form with lots of required fields, you have to basically copy-paste your string every single time.

Direct approaches

  1. Class constants: so the first thing that came to me to avoid this repetition, and possible copy-paste/grammar errors while creating a new required field, was to create a class constant. const REQUIRED = 'required'; However the downside of this approach is that you would still have to duplicate some class constants across files. (The task form, the user form, the whatever-you-like form). And this does not seem ideal.

  2. One big class constants file: What about creating some classes where the only contents would be constants? For example:

<?php
class FormConstants {
    const REQUIRED = 'required';
    const LABEL = 'label';
    ...
}
  1. A properties file: Simple, to the point, but kind of old school, in the sense that I don't know if this kind of approach would still be used, also kind of getting in the terrain of translations right?

  2. Other:

What approach/other approach would you take for this problem?

Note: I use Forms and Validation as examples here for the sake of simplicity, however there could be other case scenarios where this happens.

Me.
  • 1
  • 4
  • Excessive cleverness leads to confusing and unmaintainable code. Be careful not to try to DRY up too much. If you really are repeating these fields like 100+ times, there's always a for loop. – Brad Feb 03 '20 at 16:01
  • No matter if you put the string into a constant or a variable, you will still need to add that constant or variable everywhere. What issue are you actually trying to solve? With constants, you're just changing `'required'` to `FormConstants::REQUIRED`. Not sure how that would "solve" anything? – M. Eriksson Feb 03 '20 at 16:29
  • @MagnusEriksson Well, for starters with a `FormConstants::REQUIRED`, the problem of making a mistake in several parts of the application would be already solved, because if you realize you wrote `requird` instead of requir`e`d, you would have to modify in only one place. The core of my question is really how to deal with strings used at multiple places: should we just go with that as @Brad suggests or is there a more elegant/standard way to do it? – Me. Feb 04 '20 at 09:58

0 Answers0