私は、Doctrine 2のマニュアルで説明されているように、OrderCloseNotificationとOrderDelayNotificationでサブクラス化されたNotificationクラスをSymfony 2フレームワークを使用して構築しています別の目的(あなたがクラス名で推測できるように)。STI(単一テーブル継承)を使用したSymfony2フォームの検証
カスタムフォームとコントローラのそれぞれを作成するために必要なものとは異なるフォーム送信を検証する必要があります。私はOrderDelayNotificationを、検証が必要な通知のタイプとして使用します。ここに私の設定です:
スーパークラス:
# src/MyNamespace/MyBundle/Entity/Noticication.php
namespace MyNamespace\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Notification
{
# common attributes, getters and setters
}
サブクラス:
# src/MyNamespace/MyBundle/Entity/OrderDelayNotification.php
namespace MyNamespace\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class OrderDelayNotification extends Notification
{
private $message;
# getters and setters
}
サブクラスコントローラ:
namespace MyNamespace\MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use MyNamespace\MyBundle\Entity\OrderDelayNotification;
use MyNamespace\MyBundle\Form\Type\OrderDelayNotificationType;
class OrderDelayNotificationController extends Controller
{
public function createAction() {
$entity = new OrderDelayNotification();
$request = $this->getRequest();
$form = $this->createForm(new OrderDelayNotificationType(), $entity);
$form->bindRequest($request);
if ($form->isValid()) {
//$em = $this->getDoctrine()->getEntityManager();
//$em->persist($entity);
//$em->flush();
} else {
}
// I'm rendering javascript that gets eval'ed on the client-side. At the moment, the js file is only displaying the errors for validation purposes
if ($request->isXmlHttpRequest()) {
return $this->render('LfmCorporateDashboardBundle:Notification:new.js.twig', array('form' => $form->createView()));
} else {
return $this->redirect($this->generateUrl('orders_list'));
}
}
}
マイカスタムフォームタイプ
# src/MyNamespace/MyBundle/Form/Type/OrderDelayNotificationType.php
class OrderDelayNotificationType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('message')
->add('will_finish_at', 'date')
->add('order', 'order_selector'); //*1
return $builder;
}
public function getName()
{
return 'orderDelayNotification';
}
}
* 1:order_selector私はデータトランスフォーマと一緒に私が注文をそのプライマリキーにマップするカスタムタイプです。そのため、通知は指定された注文セットのテーブルビューで作成されます。
最後に、私はここにされて何が起こるvalidation.yml(私はすべてのコンフィギュレーションのためのYAMLを使用)
# src/MyNamespace/MyBundle/Resources/config.validation.yml
MyNamespace\MyBundle\Entity\OrderDelayNotification:
properties:
message:
- NotBlank: ~
持っている:私は、AJAXを通じてOrderDelayNotificationを作成しようとすると、(HTML要求を試していないが)メッセージが空白の場合でも、注文は常に有効と見なされます。私はまた、最小の長さを課そうとしましたが、運がありません。私はsymfonyのドキュメンテーションを読み、検証はデフォルトで有効になっていると言います。 validation.ymlの属性名を無効なものに変更しようとしましたが、symfonyはファイルがロードされていることを訴えていますが、検証は行われていません。
誰にもこの点に関する指針はありますか?
EDIT:AJAX呼び出しは次のように構成されています生み出す
$('form[data-remote="true"]').submit(function(event){
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
success: function(response) {
eval(response)
}
});
event.preventDefault();
});
:
# src/MyNamespace/MyBundle/Resources/views/Notification/new.js.twig
alert("{{ form_errors(form) }}");
をそして、それはされた私はエラーが間接的で呼び出されるsymfonyのバリデータサービス(によってスローされないされて見ることができます私のAbstractTypeサブクラス、symfonyのドキュメントによる)