1

I am trying to validate the form that I have made myself in .twig file.I am not creating form using createFormBuilder. This is my Controller Code that is call for both case 1) for view 2) after submitting the form.

public function cart_newAction(Request $request)
 {      
    $entity = new Product();
    $errors = '';
    if ($request->getMethod() == 'POST') 
    {
        $validator = $this->get('validator');
        $errors = $validator->validate($entity);
        if (count($errors) > 0) {
            echo 'Error';
        } 
        else {
            echo 'Success';             
        }
    }
    return $this->render('CartCartBundle:Cart:Add.html.twig', array('errors' => $errors ));
}

this is view file and I am showing errors like this Add.html.twig

{% for error in errors %}
    {{error}}
{% endfor %}

I have set the error in validation.yml file for name that cannot be blank. So now when I run the view page it every times show the error after I submit the form. If no error it should not display me the error just show the blank error.

Note:Is there any better way that I can do this so please share it.Remember that I am doing it without createFormBuilder UPDATE It always show me Error.Even if my form is valid and don't missing any field.

9
  • What is your question? Commented Mar 11, 2014 at 9:13
  • @xdazz actually I want the form validation I am not using the createFormBuilder so how can I do this.The work that I have done is not working properly Commented Mar 11, 2014 at 9:17
  • not working properly is just not enough to describe your problem. Could you describe the detail. Commented Mar 11, 2014 at 9:22
  • Error is not disappeared when I don't miss the form fields.It should not show the errors but it is always giving the error even the first time when I view the page it show error there too. Commented Mar 11, 2014 at 9:33
  • 2
    Unless I am missing something, I cannot see where you are binding your entity to your request in your controller method, so as far as I can see from the above code you are validating an newly created (i.e: empty) entity. Commented Mar 11, 2014 at 10:41

3 Answers 3

1

If you want to make the form yourself then you can't validate it using the Syfmony form validator. You need to use a simple PHP validation Server-side. Something like this

if ($request->getMethod() == 'POST') 
{
    $username = $_POST['username'];
    if ($username == '')
    {
       // set error message here
    } 
}
Sign up to request clarification or add additional context in comments.

Comments

1

Ok let me be clear. I gonna give you tow solutions, the first is the best and the most proper way: 1) Generate your EntityForm Type: bin/console make:form or d:g:form command. 2) Then just add some few lines to submit and get the errors.

 public function cart_newAction(Request $request)
 {      
    $entity = new Product();
    $form = $this->createForm(EntityType::class, $entity);
    $form->submitForm($request->request->all(), false);

    if ($request->getMethod()->isPost()) 
    {
    if ($form->isValid()) {
            echo 'Error';
        } 
        else {
            echo 'Success';             
        }
    }
    return $this->render('CartCartBundle:Cart:Add.html.twig', [
         'errors' => $form->getErrors(),
    ]);
}

The second solution is bind your data into your entity object because we need to set the data into our object.

1) First step create a private fonction in your current class to bind all the submited data:

private function bindEntityValues(Product $entity, array $data) {
  foreach ($data as $key => $value){
    $funcName = 'set'+ucwords($key);
    if(method_exists($entity, $funcName)) $entity->$funcName($value);
  }
}

Then your cart_newAction should be like this:

 public function cart_newAction(Request $request)
 {      
    $entity = new Product();
    $this->bindEntityValues(entity, $request->request->all());
    $errors= $this->get('validator')->validate($entity)

    if (count($errors) > 0) {
            echo 'Error';
        } 
        else {
            echo 'Success';             
        }
    }
    return $this->render('CartCartBundle:Cart:Add.html.twig', ['errors' => $errors]);
}

Wish this helped you to have a clear vision.

Comments

0

You must check if $errors is empty or not :

if (count($errors) > 0) {
    return $this->render('CartCartBundle:Cart:Add.html.twig', array('errors' => $errors ));
} else {
    return $this->render('CartCartBundle:Cart:Add.html.twig');
}

See the doc here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.