-6

(Sorry for this bad english)

I would like to know if it's possible to automatically check if variables from MySQL request are not empty in Symfony 3. I know that I can put {% if foo is defined %} in Twig or something like this but I didn't find if there's a way to do it automatically. I can test the request in the controller too. But with those solutions I have to do it for every request.

All my website uses "if not empty then show it" that's why I'm trying to find it.

Edit : I know how to check every fields of every request in Twig or in php (Controller) one by one but there is a lot of duplication code, which is "boring". Thats why I am asking you if something automatic exists to check my data. (parameter in Symfony, ...)

Thank you <3

Pierrou
  • 303
  • 3
  • 21
  • 1
    hi, please show you code in php. – Ivan Jun 09 '16 at 13:18
  • 1
    Show what you have tried. – Dipen Shah Jun 09 '16 at 13:23
  • Did you read the question ? I know how to check every fields of every request in Twig or in php (Controller) one by one but there is a lot of duplication code, which is "boring". Thats why I am asking you if something automatic exists to check my data. (parameter in Symfony, ...) – Pierrou Jun 09 '16 at 13:32
  • 2
    This is unclear - it's difficult for us to understand what you're trying to accomplish without seeing any code. As written it sounds like you want to check if a variable is empty without telling PHP/Symfony that you want to check it. PHP can't read your mind, you've got to tell it to check it somewhere - that's generally done with `empty()` or `is defined` in Twig. – HPierce Jun 09 '16 at 13:58
  • Yes but is that a way to not put it in Twig or Php? – Pierrou Jun 09 '16 at 14:01
  • 1
    Can you please show at least a number of line of your code where the problem might occur, you might need to show a couple of files (twig & controller, etc...) and then we "could" help you out. Did you realize we are willing to help you for free? – Alvin Bunk Jun 09 '16 at 15:25
  • Can you please understand that no code is needed? You're just saying "please send code, show something blah-blah-blah" but you don't need code to answer this. I know how to check manually every fields that's not the problem and I put it in the question. It's just boring to put it for every field. Understand that. If you know how Symfony works you can answer, if you don't you can't. That's all. So, no I won't show some code because it's useless. – Pierrou Jun 09 '16 at 15:48
  • Tell me where you see the code in this question with +34 : http://stackoverflow.com/questions/860954/examples-of-sql-injections-through-addslashes?lq=1 – Pierrou Jun 09 '16 at 15:50
  • @Pierrou code is needed because what you are asking doesn't make much sense.With your code we would better understand what is the real issue you are having. – Derek Jun 09 '16 at 16:02
  • This is a theoretical question. No code is needed to answer this. And I don't have any issue. As I said a thousand times, I know how to check a field but I would like to know how to automate this to not put `{% if foo is defined %}` (or anything else) everywhere to prevent any omission. – Pierrou Jun 09 '16 at 16:04
  • @Pierrou We already understood that. But the issue could be that you are relying on too many undefined variables in your code, that's why we need to see it. Do you really think we all put "if defined" statements everywhere in our code like you do? No, because most of the time we don't rely on variables that could exist or not. – Derek Jun 09 '16 at 16:11
  • @Derek : For example : You have 3 fields (X, Y, Z) in your MySQL database. You want to show it only if those fields are not empty. 2 ways : in Php or in Twig. But for both solutions you have to check every field manually. Something like : `{% if X is defined %} {% X %} {% endif %}`. I would like to only put `{% X %}` without the if statement. – Pierrou Jun 09 '16 at 16:21
  • I don't get why your empty fields would not be defined. Usually they only have a null value, but they are defined and don't need to be in a "if defined" statement. – Derek Jun 09 '16 at 16:45

2 Answers2

2

If I understand, your problem :

You want to iterate on each property in an entity. But you can't, then you are searching for a solution to not write :

{% if entity.property1 %}
    {{ entity.property1 }}
{% endif %}
{% if entity.property2 %}
    {{ entity.property2 }}
{% endif %}
{% if entity.property3 %}
    {{ entity.property3 }}
{% endif %}

You have two solutions to make your properties traversable :

So you can iterate on each property, and do something like that :

{# Where you get your entity as an array #}
{% for property in entity %}
   {% if not property  is null %}
        {{ property }}
   {% endif %}
{% endfor %}

{# where fields comes from the ReflectionClass #}
{% for field in fields %}
   {% if not attribute(entity,field) is null %}
        {{ attribute(entity,field)}}
   {% endif %}
{% endfor %}
D. Schreier
  • 1,700
  • 1
  • 22
  • 34
Alsatian
  • 3,086
  • 4
  • 25
  • 40
0

Use the Twig strict_variables config parameter and set it to false (should be the default value):

strict_variables boolean

If set to false, Twig will silently ignore invalid variables (variables and or attributes/methods that do not exist) and replace them with a null value. When set to true, Twig throws an exception instead (default to false).

Derek
  • 1,826
  • 18
  • 25