Add a php validation for forms #17

Open
cedriclombardot opened this Issue Sep 20, 2011 · 7 comments

Projects

None yet

3 participants

@cedriclombardot
Member

Actually the form validation is in HTML5 managed by the generator and in php managed by user like in http://symfony.com/doc/current/book/validation.html#constraint-configuration, i imagine to create a proxy class chain between bundle and real model class

  • BundleYourModel extends AdmingeneratedBundleYourModel
  • AdmingeneratedBundleYourModel extends YourModel

And in AdmingeneratedBundleModel something like :

public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        parent::loadValidatorMetadata(); //If is callable

        //And auto generated
        $metadata->addPropertyConstraint('gender', new Choice(array(
            'choices' => array('male', 'female'),
            'message' => 'Choose a valid gender.',
        )));
    }

What do you think about ?

@deepgreen

The alternative to php is using yaml file that define the constraints - as per Symfony. I do see the technical advantage with your proxy class, I guess it is consistent approach using code generation, but validation can be very "fussy" and quite likely to be changed, Using yaml as the default lowers the barrier to get a site up "without php code". i.e. A whole site generated without code, including form validation! From your link above

Acme\BlogBundle\Entity\Author:
properties:
gender:
- Choice: { choices: [male, female], message: Choose a valid gender. }

@cedriclombardot
Member

Yes i'm agree with the approch of yaml validators. thats why i've not already write code. I'm shared between extract automatically validations from model constraints (eg Unique column) -> so generate a php because can't do otherwise in cache. And lets devs write the validator yaml.

Do you know if a good men have do a generate validators command ? Could be a good alternative.

@deepgreen

+1 for yaml then. I believe that the first step is to clearly indicate to users how they can validate their model using existing validation constraints provided by symfony - without code change and of course coupled with html 5. i.e. Just extending the documentation as a start. This is why your generator approach is so powerful! When this recommended approach is written up this then it can be automated directly in the builders or commands. I think eventually the command approach fits better since this is decoupled from the builders and likely simpler. But can be deferred.
Put another way. In my opinion the generator should not in this early release do any more - you are generating symfony components which are very well documented, stable and extensible. Linking the generator to their documentation etc. Just a thought.

@cedriclombardot
Member

OK so agree for all to move into roadmap 1.1 and method will be a command line to init the yaml into the folder of the bundle ?

@deepgreen

Sounds like a good plan.

@loostro
Member
loostro commented Nov 8, 2012

@cedriclombardot @CayetanoSoriano i think this is no longer neccessary as you can define validation constraints directly in the entity useing Annotations,

i think we should close this issue

@cedriclombardot
Member

Yes and no for me validate an entity is not the same as validate a form. I can want to add differents constrainst depends if i'm in front or back for example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment