Jelajahi Sumber

merged branch jseverson/formnotboundexception (PR #1465)

Commits
-------

49af102 Throwing FormNotBoundException when calling form isValid

Discussion
----------

Throwing FormNotBoundException when calling form isValid

---------------------------------------------------------------------------

by Seldaek at 2011/06/28 12:20:04 -0700

I'd have made that a `\LogicException`, but that's just me hating on custom exceptions. Otherwise as said on IRC, :+1:.

---------------------------------------------------------------------------

by stloyd at 2011/06/28 12:27:27 -0700

+1 but IMO `FormException` would be here good enough.
Fabien Potencier 14 tahun lalu
induk
melakukan
c36f8d6459

+ 16 - 0
src/Symfony/Component/Form/Exception/FormNotBoundException.php

@@ -0,0 +1,16 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\Exception;
+
+class FormNotBoundException extends FormException
+{
+}

+ 6 - 1
src/Symfony/Component/Form/Form.php

@@ -14,6 +14,7 @@ namespace Symfony\Component\Form;
 use Symfony\Component\Form\Event\DataEvent;
 use Symfony\Component\Form\Event\FilterDataEvent;
 use Symfony\Component\Form\Exception\FormException;
+use Symfony\Component\Form\Exception\FormNotBoundException;
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
 use Symfony\Component\Form\Exception\TransformationFailedException;
 use Symfony\Component\HttpFoundation\Request;
@@ -674,7 +675,11 @@ class Form implements \IteratorAggregate, FormInterface
      */
     public function isValid()
     {
-        if (!$this->isBound() || $this->hasErrors()) {
+        if (!$this->isBound()) {
+            throw new FormNotBoundException('Can not validate a form that is not bound');
+        }
+
+        if ($this->hasErrors()) {
 
             return false;
         }