Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to get entity constraints and message in FormBuilder class #16449

Closed
nicholasnet opened this issue Nov 3, 2015 · 5 comments
Closed

Comments

@nicholasnet
Copy link

I think there should be a way to access all the entity constraints in Form class so that we can pass those information to client side without manually entering those constraints all over again in form class.

For example if we have code like this.

class User implements UserInterface, \Serializable
{
  /**
   * @var string
   *
   * @ORM\Column(name="first_name", type="string", length=64)
   * @Assert\NotBlank(message="First name cannot be blank")
   * @Assert\Length(max=64, maxMessage="First name cannot more than {{ limit }} characters long")
   */ 
   private $firstName;
}

I would like to access these constraints with messages in form class so that we pass down these information to twig. Like this

<input type="text" required="required" data-required-msg="First name cannot be blank" name="firstname" data-max-length="64" data-max-length-msg="First name cannot be more than 64 characters long">

Here data attribute should be customizable.

Currently this cannot be achieved without code duplication in either twig or in form class directly.

@jakzal
Copy link
Contributor

jakzal commented Nov 3, 2015

It's already possible to get this kind of information from the metadata factory:

$metadata = $this->container->get('validator')
    ->getMetadataFactory()
    ->getMetadataFor('AppBundle\Entity\Post');

You could expose it as javascript, or decorate it in a view object and pass it to twig.

Do we really need any additional support in core?

@nicholasnet
Copy link
Author

I think we do because form should be able to gather all the constraints if data class is used. Furthermore, your code does not consider the following

  • Retrieval of formatted message (in case of length, message have placeholder) including custom message.
  • Internationalization of message based on selected locale

Also I think code will be messy if same entity is used in different forms using different validation group since metadata retrieval process will have to be repeated.

These are the few reasons to have this feature. Having said that I can understand that this feature may not be desired 100% of the time so there should be some mechanism to skip this if we want to.

@jakzal
Copy link
Contributor

jakzal commented Nov 4, 2015

@nicholasnet I'm not saying you should write messy code and retrieve metadata directly from every form you create. You can abstract it away. I'm just saying it's possible.

Since you've got a use case, perhaps you should try implementing this outside of a framework. It can always be integrated back later. It'd be easier to decide if you had something working.

@nicholasnet
Copy link
Author

@jakzal I am sure it is possible right now but how can we go about achieving this using form class alone is a question. I know extracting metadata is not a problem but how about extracting formatted message and leveraging Symfony internationalization for those messages including constraints?

I am more than happy to work on this feature I just need some guidance regarding how we can go about achieving this.

It seems like issue somewhat like this was brought up before #11200 but it was turned down.

But the feature that I am asking is bit different there is no json or js involved in this only PHP.

@webmozart
Copy link
Contributor

@nicholasnet Thank you for your suggestion! As was said before, this is possible in userland already. If there's a very good implementation that we could simply add to the core, I'd consider it. But otherwise I don't think we will implement and support this ourselves.

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

No branches or pull requests

3 participants