How to debug a Symfony2 form?

强颜欢笑 提交于 2021-01-27 07:49:19

问题


I'm creating a register page to create a new user based on FOSUserBundle.

I go this message on the top f the form every time I submit the new user form: This value should not be blank.

Apparently this is a global message and it's not linked to a particular field on my form, so I guess it must be related to any other atribute of my User class.

What mechanism Sf2 offers me in order to figure out what's wrong on my form? I am using Symfony 2.5.0

(I would post more details about my configuration, User class and form... but I really would like to learn how to find this problem on my own)

Thanks in advance, community!


回答1:


Check out debug toolbar on dev frontend controller, its good debug way for the start




回答2:


This is a tricky error that is hard to track. I was not able to track those type of errors myself, so I searched on Google and SO. I found the answer that explained the problem with my form. It was a validation rule on one of the fields that was not included in the form.

So, in your case that may also be the reason. There is probably a validation rule (most likely NotBlank) on a field that is not included in the form. Just try to disable them one by one to find the filed that is causing this error.




回答3:


I think you should try printing your $request->get('name_of_your_form'), and after binding it with the form, try printing $form->getData(). Should be enough to be clear about what field causes the issue.

Also make note of validation groups.




回答4:


Try printing the form errors out as a string using the $form->getErrorsAsString() or the $form->getErrors() methods.

Taken from the source

/**
 * Returns a string representation of all form errors (including children errors).
 *
 * This method should only be used to help debug a form.
 *
 * @param int     $level The indentation level (used internally)
 *
 * @return string A string representation of all errors
 *
 * @deprecated Deprecated since version 2.5, to be removed in 3.0. Use
 *             {@link getErrors()} instead and cast the result to a string.
 */
public function getErrorsAsString($level = 0)
{
    return self::indent((string) $this->getErrors(true, false), $level);
}



回答5:


you should see the error with more description in the logs of the /_profiler even maybe a SQL error which may lead to the root of the problem.



来源:https://stackoverflow.com/questions/24436073/how-to-debug-a-symfony2-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!