I want to create a validator similar to the way GitHub handles deleting repositories. To confirm the delete, I need to enter the repo name. Here I want to confirm delete by entering the entity property "name". I will either need to pass the name to the constraint or access it in some way, how do I do that?
you could indeed use a validator constraint to do that:
1: Create a delete form (directy or using a type):
return $this->createFormBuilder($objectToDelete)
->add('comparisonName', 'text')
->setAttribute('validation_groups', array('delete'))
->getForm()
;
2: Add a public property comparisonName
into your entity. (or use a proxy object), that will be mapped to the corresponding form field above.
3: Define a class level, callback validator constraint to compare both values:
/**
* @Assert\Callback(methods={"isComparisonNameValid"}, groups={"delete"})
*/
class Entity
{
public $comparisonName;
public $name;
public function isComparisonNameValid(ExecutionContext $context)
{
if ($this->name !== $this->comparisonName) {
$propertyPath = $context->getPropertyPath() . '.comparisonName';
$context->addViolationAtPath(
$propertyPath,
'Invalid delete name', array(), null
);
}
}
}
4: Display your form in your view:
<form action="{{ path('entity_delete', {'id': entity.id }) }}">
{{ form_rest(deleteForm) }}
<input type="hidden" name="_method value="DELETE" />
<input type="submit" value="delete" />
</form>
5: To verify that the delete query is valid, use this in your controller:
$form = $this->createDeleteForm($object);
$request = $this->getRequest();
$form->bindRequest($request);
if ($form->isValid()) {
$this->removeObject($object);
$this->getSession()->setFlash('success',
$this->getDeleteFlashMessage($object)
);
}
return $this->redirect($this->getListRoute());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With