Reputation: 171
I created code to create input form that i can edit a value in a certain database column put i want to but a limit to this value for example i have a row i named "credit" and other column I named it "less than credit" so when i edit my row "less than credit" so the row "less than credit" will only accept input on it if the value less than the credit value if i have 5 coins on the credit row value i can input on the "less than credit" input form if the value 5 coins or less than 5 coins so how I do that my full code
<?php
namespace site\blogBundle\Controller;
use AppBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use site\blogBundle\Form\TaskType;
class DefaultController extends Controller
{
public function indexAction(Request $request)
{
//$task = new User();
$user = $this->container->get('security.context')->getToken()->getUser();
$investor = $this->getDoctrine()->getRepository('AppBundle:User')->findOneBy(array('id' => $user->getId()));
$less_than_credit = $investor->getlessthancredit();
$form = $this->createForm(new TaskType(), $investor);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if(!empty($form->get('less_than_credit')->getData())){
$investor->setlessthancredit($form->get('less_than_credit')->getData());
}
else{
$investor->setlessthancredit($less_than_credit);
}
$em = $this->getDoctrine()->getManager();
$em->persist($investor);
$em->flush();
$session = $this->getRequest()->getSession();
/**
* @Route("/siteblog_homepage/")
*/
return $this->redirectToRoute('siteblog_homepage');
}
return $this->render('siteblogBundle:Default:index.html.twig', array(
'form' => $form->createView(),
));
}
}
Upvotes: 1
Views: 704
Reputation: 3618
You can use expression language:
/**
* @Assert\Expression(
* "this.credit >= value",
* message="lessthancredit should be less or equal to credit"
* )
*/
private $lessthancredit;
Upvotes: 2
Reputation: 381
I think you need something like the Callback validator available in Symfony. You can view it on ; http://symfony.com/doc/current/reference/constraints/Callback.html
Lets assume your entity
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
Class User
{
...
/**
* @Assert\Callback
*/
public function validateLessThanCredit(ExecutionContextInterface $context, $payload)
{
if(null !== $this->lessThanCredit && $this->lessThanCredit >= $this->credit) {
$context
->buildViolation('Less than credit is not less than credit')
->atPath('lessThanCredit')
->addViolation();
}
}
}
I think you can do something like this
Upvotes: 1