How do I translate form validation in Symfony 3? - yaml

Got everything in my form validating using i18n just fine but cant figure out how to translate the form validation. For example right now when I try and submit the form with an empty field I get the pop-up validation message "Please fill out this field."
Translation is turned on and annotations enabled:
// config_dev.yml
framework:
router:
resource: "%kernel.root_dir%/config/routing_dev.yml"
strict_requirements: true
profiler: { only_exceptions: false }
translator: { fallback: en }
validation: { enable_annotations: true }
Entity is rigged for validation annotations:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
....
/**
* #var string
* #Assert\NotBlank(message = "Custom validation error message")
* #ORM\Column(name="url", type="string", length=512)
*/
private $url;
I also created the file validators.en.yml in my app/Resources/translations folder but not sure where to go from here. Can anyone help?

You could translate validation messages in app/Resources/translations/validators.fr.xlf (example for french xlf translation file)
Here the Docs you are looking for: http://symfony.com/doc/current/validation/translations.html

Related

Validate form using Entity asserts in Symfony

I want to validate form using asserts in Entity class but when I submit the form, it says $form->isValid() is true.
Here is how I got it setup:
// config.yml
validation: { enabled: true, enable_annotations: false }
// validation.yml
Software\Bundle\Entity\Program:
properties:
name:
- NotBlank: ~
// MyForm
...
$builder
->add('name', 'text', [
'label' => 'word.name'
])
;
...
// Program.php
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
I tried also via Annotations but it did not help.
I know I can put 'constraints' property to my form and there set new NotBlank() but I want to have that validation on Entity level since I am going to use an API and I want to have validation in one place instead of two.
Is my validation.yml file ignored or what?
EDIT
I did not mention one important thing that my form is embedded into another one. in this case you must use 'cascade_validation' property in your form options.
This answer helped me a lot Symfony2 - Validation not working for embedded Form Type
Your configuration seems correct but your yml is ignored, try to clear your cache.
Also where is your validation.yml ? It must be in a path like : src/AppBundle/Resources/config/validation.yml
In your annotation example, there is no validation constraint you must add #Assert\NotBlank on your property. Nullable = false is only for your database schema.
Symfony doc about validation : http://symfony.com/doc/current/cookbook/validation/custom_constraint.html#using-the-new-validator
Same question about using validation.yml : Symfony2 how to load validation.yml

symfony2 (doctrine) currency constraint not working

I have an entity set up like this:
use Symfony\Component\Validator\Constraints as Assert;
...
/**
* #var decimal $amount
* #Assert\Currency
* #ORM\Column(name="amount", type="decimal")
*/
private $amount;
If I submit my form with blank in the amount field nothing happens. Shouldn't my form automatically throw an error or what am I missing?
You need a NotBlank assertion (#Assert\NotBlank) to validate that that field wasn't submitted with empty data. If that passes, then your other assertions should run on any actual data submitted.

Validation asserts being ignored in symfony2?

I'm trying to get a form validated but its behavior is strange, I'm a newbie in symfony2 so I must be missing something.
I use SonataAdminBundle to create forms and CRUD controller. My ResponsableDato entity has this property:
/**
* #var string $contacto
* #Assert\NotBlank(message="Please enter your name.")
* #Assert\Length(min="3", minMessage="too short."))
* #ORM\Column(name="contacto", type="string", length=100, nullable=true)
*/
private $contacto;
If I leave contacto field blank it gives me "Complete this field" message instead of "Please enter your name". If I type one character it passes NotBlank validation but ignores Length validation.
What could I be missing? It sounds as if I had to override something to get it to work
also you're talking about html5 validation or isValid() to form_errors() case?
i'm quite sure that "Complete this field" is related to "required" option in your form field
$builder->add('contacto', 'text', array('required' => false));
set required to false and see your custom error message :)
edit:
use Symfony\Component\Validator\Constraints as Assert;
[...]
/**
* #Assert\NotNull()
*/
private $contacto;
this will fire validation error after submit, though if there's no data for this field in the form, if you need to avoid an empty string use #Assert\NotBlank()

Symfony2 custom validation

I have a "product" entity and i want to validate a property(for example price) of this class with a custom callback function.
My custom validation is more complex than the defaults validation provided by sf2(minLength, max, etc). I wish to do something like this:
class Product
{
/**
* #Assert\NotBlank()
* #Assert\CallbackValidationFunction('validatePrice', 'Your price is not the expected')
*/
private $price;
}
function validatePrice($priceValue){
$x = " i want";
return $priceValue == "the value".$x;
}
then, in the errors the message 'Your price is not the expected' is related withthe property $price in Product after a $form->isValid() or a product validation via $this->get('validator');
You'd be better off writing a custom validation constraint. See http://symfony.com/doc/current/cookbook/validation/custom_constraint.html for instructions.

Symfony2 custom validator

i am trying to build an custom validator with symfony2 but something strange happens:
i have created both Password and PasswordValidate by following the steps in symfony2 cookbook but first time when i load the page i get this error:
AnnotationException: [Semantical Error] The annotation "#Symfony\Component\Validator\Constraints\Password" in property NMSP\MyBundle\Entity\User::$password does not exist, or could not be auto-loaded.
after reloading the error disappears and the validation still not fires and it return the code is valid.
here is the relevant code:
//annotation declaration:
/**
* #ORM\Column(type="string", length="32", unique="true")
*
* #Assert\MinLength(3)
* #Assert\Password2()
*/
protected $password;
//load files with the following in the code
services:
validator.password:
class: NMSP\MyBundle\Validator\PasswordValidator
tags:
- { name: validator.constraint_validator, alias: password }
can`t figure this one out:(
Assuming your custom validator constraint is not in the Symfony\Component\Validator\Constraints namespace, but your own namespace: NMSP\MyBundle\Validator.
You should add the following use statement:
use NMSP\MyBundle\Validator as NMSPAssert;
Then use the following annotation on the $username property:
#NMSPAssert\Password()
That should do it.

Resources