dongli4711 2012-07-31 10:09
浏览 45
已采纳

如何在表单元素中挂钩后验证函数

I am writing a more advanced file form element, more of a widget that displays the currently loaded uploaded image as well as validating image sizes etc.

If the file validates, I want the form element itself to copy the file to a publicly accessible folder. This will mean that a) the image can be displayed and b) if the whole form doesn't validate, the user does not need to re-upload the image.

Anyway that's the back story, but essentially I need to figure out how to get a function from within a form element to run when the form validates that element. Something like creating an onValidate() function in the form element class.

Most importantly I need a solution that doesn't spill out into the controller (or as little as possible). I am struggling to find a clean way to implement this.

I would like to do something like this...

class File extends Element implements InputProviderInterface
{

....

public function isValid()
{
    if ( $isValid = parent::isValid() ) {
        echo "Hi there, glad you called";
    }

    return $isValid;
}
  • 写回答

1条回答 默认 最新

  • douzhan1994 2012-08-06 17:19
    关注

    The Zend\Form\Element::getValidator() function will be called on each validation run.

    I think you should call a class which's concern it is to copy the file. Maybe you want to raise an event to make it more decoupled.

    Validation and Filtering are now part of the InputFilter class. Your goal is to write an dedicated Validator and hardwire it with your Element.

    An example of how to achieve this, look at Zend\Form\Element\Select which as a hardwired InArrayValidator in its onValidate() method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划