App\Validator\Constraints\ExistsValidator::__construct(), 0 passed in (...)vendor/symfony/validator/ConstraintValidatorFactory.php on line 43 and exactly 1 expected<br />
Yea, this is a pretty ancient tutorial now :). From the error, it sounds like your ExistsValidator has a constructor argument? What is that constructor argument? And is your code working normally (i.e. outside of phpunit)?
Although not entirely up to date anymore, I love the way you show how to handle this kind of problems. As it turned out, I exactly needed this logic so I could reuse it easily. Thanks
One question though: Why did you rewrite the query to a much complexer syntax using orX and andX? /* return $this->createQueryBuilder('e') ->andWhere('(e.startDate < :endDate AND e.endDate > :startDate) OR (e.endDate > :startDate AND e.startDate < :endDate)') ->setParameter('startDate', $startDate) ->setParameter('endDate', $endDate) ->getQuery() ->execute() ; */
Lol, yes, this is now ancient! But I'm glad it was still somewhat useful for you!
One question though: Why did you rewrite the query to a much complexer syntax using orX and andX?
Wait, do you mean, why didn't I use orX and andX? If I've understood correctly (I don't always!) I've always found the expressions stuff super unreadable. In other words:
// Yay! I like this! Yes, there are some parentheses - it *is* a bit complex, but it makes sense to me
// I might break this into multiple lines and indent it to help make the groups easier to read, but like this
->andWhere('(e.startDate < :endDate AND e.endDate > :startDate) OR (e.endDate > :startDate AND e.startDate < :endDate)')
// this actually confuses me :)
$expr1 = $qb->expr()->andX('e.startDate < :endDate AND e.endDate > :startDate');
$expr2 = $qb->expr()->andX('e.endDate > :startDate AND e.startDate < :endDate');
$orExpr = $qb->expr()->orX($expr1, $expr2);
``
So, totally subjective, and maybe cause I did write raw SQL queries for enough time that the string version feels better. I think part of the problem might be that the idea behind the expression stuff is solid, but the way it's implemented... or just doesn't ultimately "read" very clearly to me.
Or... maybe I answered the wrong question 😂 - let me know!
Cheers!
LOL, indeed in the text it use SQL expressions which are much clearer to me as well. But in the downloaded code you? replaced it with the andX and orX expressions. So I wondered why that was. The latter confuses me as well.
Ah! So I did! I had no idea! The way we built the code downloads back then was not as systematic as it is now, but I can't think of why I would have used that format instead. This is one of those "choose whichever one you like" situations where I am not aware of any technical disadvantage of either. So for you and I, stick with the SQL-style :).
My goto for this situation is exactly what I'm talking about this post: the Callback constraint. The callback function will be passed your object (e.g. Event) and you can get the values from the two fields and compare them.
'constraints' => $collectionConstraint //or validation_constraint ?? was this removed ??
);
}
public function getBlockPrefix()
{
return 'add_chapitre';
}
But it's not working ? I think this option might have been removed in favour of validation constraints on the entity class somewhere around symfony 2.3 ?
Sorry for the slow reply - for some reason your comment was put into the spam filter!
I don't know why it's not working, but using the "constraints" option is still as valid in Symfony 2.8, as it was in Symfony 2.3 - and the option *is* called "constraints". So, I'm not sure what could be going wrong :/
Cheers!
Please, log in to vote for this comment
|
Share Comment
"Houston: no signs of life" Start the conversation!
11 Comments
Not working in phpunit test :(
App\Validator\Constraints\ExistsValidator::__construct(), 0 passed in (...)vendor/symfony/validator/ConstraintValidatorFactory.php on line 43 and exactly 1 expected<br />Hey PM!
Yea, this is a pretty ancient tutorial now :). From the error, it sounds like your
ExistsValidatorhas a constructor argument? What is that constructor argument? And is your code working normally (i.e. outside of phpunit)?Cheers!
Although not entirely up to date anymore, I love the way you show how to handle this kind of problems. As it turned out, I exactly needed this logic so I could reuse it easily. Thanks
One question though: Why did you rewrite the query to a much complexer syntax using orX and andX?
/*
return $this->createQueryBuilder('e')
->andWhere('(e.startDate < :endDate AND e.endDate > :startDate) OR (e.endDate > :startDate AND e.startDate < :endDate)')
->setParameter('startDate', $startDate)
->setParameter('endDate', $endDate)
->getQuery()
->execute()
;
*/
$qb = $this->createQueryBuilder('e');
$expr1 = $qb->expr()->andX('e.startDate < :endDate AND e.endDate > :startDate');
$expr2 = $qb->expr()->andX('e.endDate > :startDate AND e.startDate < :endDate');
$orExpr = $qb->expr()->orX($expr1, $expr2);
$qb->andWhere($orExpr)
->setParameter('startDate', $startDate)
->setParameter('endDate', $endDate)
;
return $qb
->getQuery()
->execute()
;
Hey Paul Rijke!
Lol, yes, this is now ancient! But I'm glad it was still somewhat useful for you!
Wait, do you mean, why didn't I use orX and andX? If I've understood correctly (I don't always!) I've always found the expressions stuff super unreadable. In other words:
LOL, indeed in the text it use SQL expressions which are much clearer to me as well. But in the downloaded code you? replaced it with the andX and orX expressions. So I wondered why that was. The latter confuses me as well.
Ah! So I did! I had no idea! The way we built the code downloads back then was not as systematic as it is now, but I can't think of why I would have used that format instead. This is one of those "choose whichever one you like" situations where I am not aware of any technical disadvantage of either. So for you and I, stick with the SQL-style :).
Cheers!
Hi Ryan,
What if I want to validate two form fields against each other?
For example there are two fields on a form number 1 and number 2, i want to check if number 2 is not greater than number 1, how would I do this?
Regards,
Baig
Hey Shairyar!
My goto for this situation is exactly what I'm talking about this post: the Callback constraint. The callback function will be passed your object (e.g. Event) and you can get the values from the two fields and compare them.
Is that what you're looking for?
Thanks Ryan,
I was trying to do the validation via custom Validator, this helped alot. Thanks
Hello , I'm trying this in symfony 2.8 but apparently it's not working anymore ??
I want to set constraints with a collection in a form class like so
public function configureOptions(OptionsResolver $resolver)
{
//TODO move constraints in the entity
$collectionConstraint = new Collection(array(
'titre' => array(
new Assert\NotNull(),
new Assert\Length(array('min'=>2, 'max'=>255))
),
'livre' => array(
new Assert\NotBlank()
),
'type' => array(
new Assert\NotBlank(),
),
'prix' => array(
new Assert\NotNull(),
new Assert\Type('numeric')
),
'ordre' => array(
new Assert\NotNull(),
new Assert\Type('integer')
)
));
return array(
'data_class' => 'DP\SkoazellBundle\Entity\Chapitres',
'constraints' => $collectionConstraint //or validation_constraint ?? was this removed ??
);
}
public function getBlockPrefix()
{
return 'add_chapitre';
}
But it's not working ? I think this option might have been removed in favour of validation constraints on the entity class somewhere around symfony 2.3 ?
Hi there!
Sorry for the slow reply - for some reason your comment was put into the spam filter!
I don't know why it's not working, but using the "constraints" option is still as valid in Symfony 2.8, as it was in Symfony 2.3 - and the option *is* called "constraints". So, I'm not sure what could be going wrong :/
Cheers!
"Houston: no signs of life"
Start the conversation!