How to 'validate' a Symfony form in steps - instead of calling $form->isValid()

后端 未结 2 606
野性不改
野性不改 2021-02-04 22:07

I am using Symfony 1.3.6 on Ubuntu.

I have a form with a lot of fields on it - rather than showing all the fields in one go (which may intimidate the user), I want to br

2条回答
  •  北荒
    北荒 (楼主)
    2021-02-04 22:49

    The function isValid() doesn't really do anything, except check if the associated form has been bound and that the total number of validator errors is 0. The actual validation is done during the "binding" stage ($form->bind()).

    After binding, validator errors are stored in each field of the form(sfFormField). So, to get the individual errors of form fields, you can do something like this:

    hasError()) {
          // do something
        }
      }
    ?>
    

    Or, since in your case you need to deal with only a restricted set of fields, try to iterate over an array of field names instead:

    hasError()) {
          // do something
        }
      }
    ?>
    

    This can easily be adapted into a function, say validateFields($fieldNames) that meets DRY expectations.

    Check the documentation for sfFormField to see what other information you can get from a field.

提交回复
热议问题