Monday, 15 July 2013

How to dynamically control the validation of a form? -



How to dynamically control the validation of a form? -

i've got issues symfony's form validation handling. i'd validate form bound entity based on data. there quite bunch of info how dynamically modify form fields using formevents. i'm missing on topic how control/modify validation.

my simplified utilize case is:

a user can add together event calendar. the validation checks if there's event. if there's collision, validation throw error. the user should able ignore error/warning.

the validation implemented validator constraint::class_constraint target (as it's taking more stuff account).

i tried to:

hack around validation groups, couldn't find access entity wide validators. hack around formevents , add together field "ignore date warning". hack around submit button alter "force submit".

... never found working solution. simpler hacks single property based validator didn't work out. :(

is there symfony way dynamically command validation?

edit: code looks this:

use doctrine\orm\mapping orm; utilize acme\bundle\validator\constraints acmeassert; /** * appointment * * @orm\entity * @acmeassert\dateisvalid */ class appointment { /** * @orm\column(name="title", type="string", length=255) * * @var string */ protected $title; /** * @orm\column(name="date", type="date") * * @var \datetime */ protected $date; }

the validator used service:

use symfony\component\validator\constraint; utilize symfony\component\validator\constraintvalidator; /** * validates date of appointment. */ class dateisvalidvalidator extends constraintvalidator { /** * {@inheritdoc} */ public function validate($appointment, constraint $constraint) { if (null === $date = $appointment->getdate()) { return; } /* magic validate date */ if (!$valid) { $this->context->addviolationat('date', $constraint->message); } } }

the corresponding constraint class set target entity class.

use symfony\component\validator\constraint; /** * @annotation */ class dateisvalid extends constraint { public $message = 'the date not valid!'; /** * {@inheritdoc} */ public function gettargets() { homecoming self::class_constraint; } /** * {@inheritdoc} */ public function validatedby() { homecoming 'acme.validator.appointment.date'; } }

edit 2: seek formevents... tried different events.

$form = $formfactory->createbuilder() ->add('title', 'text') ->add('date', 'date') ->addeventlistener(formevents::whichone?, function(formevent $event) { $form = $event->getform(); // here? $form->geterrors(); // empty events run before validation? // need if (!$dateisvalid) { $form->setvalidationgroup('ignorewarning'); } });

edit 3: constraint correctly declared. that's not issue:

services: validator.acme.date: class: acmebundle\validator\constraints\datevalidator arguments: ["@acme.other_service"] tags: - { name: validator.constraint_validator, alias: acme.validator.appointment.date }

validation done on entity, forms execute object's validations. can choose groups based on submitted data

public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'validation_groups' => function(forminterface $form) { $data = $form->getdata(); if (entity\client::type_person == $data->gettype()) { homecoming array('person'); } else { homecoming array('company'); } }, )); }

i have had issues when using approach on embedded forms && cascade-validation

edit: using flash determine if validation must take place.

// service definition <service id="app.form.type.callendar" class="%app.form.type.callendar.class%"> <argument type="service" id="session" /> <tag name="form.type" alias="my_callendar" /> </service> // controller public function somavtion() { $form = $this->get('app.form.type.callendar'); ... } // in form public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'validation_groups' => function(forminterface $form) { $session = $form->getsession(); if ($session->getflashbag()->get('callendar_warning', false)) { homecoming array(false); } else { homecoming array('validate_callendar'); } }, )); }

forms validation symfony2 symfony-2.5

No comments:

Post a Comment