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.
Related
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()
I have created extbase extension, there for some reason I need to create object manually in create action.
My create action looks like this,
/**
* action create
*
* #return void
*/
public function createAction() {
$newObj = new \TYPO3\Myext\Domain\Model\Modelname();
$newObj->setMyval('value');
$this->myrepository->add($newObj);
}
Here the problem is its not validating for require field, captcha etc even if I mention #validate NotEmpty in model.
So how to make the validation of manually created object ?
It should throw error to form like out-of-the-box features.
Thank you.
Out of the box validation is only triggered on constructing model objects from GET/POST parameters according to your controller actions signature.
It should look something like this:
/**
* action create
* #param \TYPO3\Myext\Domain\Model\Modelname $newObject
* #return void
*/
public function createAction(\TYPO3\Myext\Domain\Model\Modelname $newObject) {
$this->myrepository->add($newObj);
}
Take a look at the extension_builder, create a model and let the new/create action be generated for you. That will give you a good example on the create action as well as on the new action where the form is being rendered.
I have written one extbase plugin, that creates the FE users from front end form.
The create action is something like this
/**
* action create
*
* #param \TYPO3\Usermanagement\Domain\Model\Users $newUsers
* #return void
*/
public function createAction(\TYPO3\Usermanagement\Domain\Model\Users $newUsers) {
$this->usersRepository->add($newUsers);
}
Here I want to validate for same username or email already exists or not.
How can I do this ?
Any suggestions ?
Thank you.
You don't need to bind a $newUser as an action's param, instead you can just read some fields using $this->request->hasArgument('something') and $this->request->getArgument('something') to validate properties yourself, and create new user object manually like.
public function createAction() {
$newUsers = new \TYPO3\Usermanagement\Domain\Model\Users();
// do something with $newUsers object...
$this->usersRepository->add($newUsers);
}
It will not throw an exception in case when there's no valid user object in the request, so it will allow you to handle form's error as you want/need.
It will also allow you to use some preprocessing before saving ie hashing/salting passwords.
I have a Symfony 2/Doctrine 2 entity with a UniqueEntity constraint. As show in the documentation, it should be possible to set a custom error message. I tied the following syntax, but that dose not work:
/**
* #ORM\Entity
* #ORM\Table(name="User")
* #UniqueEntity("email", message="Your E-Mail adress has already been registered")
*/
class User
What is the correct notation for the UniqueEntity constraint message? Or is the documentation simply wrong?
If you use only fields option in this annotaion, it can be used as the default option (the only option without name). However when you specify additional settings, you have to specify fields property.
/**
* #ORM\Entity
* #ORM\Table(name="User")
* #UniqueEntity(
* fields={"email"},
* message="Your E-Mail adress has already been registered"
* )
*/
class User
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.