質問

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.

役に立ちましたか?

解決

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
    } 
}

他のヒント

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.

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top