Right now I have a form that adds an instance of Dog
to an Owner
. I want to make it so that if the Dog
already has an owner, a confirmation dialogue pops up. I can get the confirmation to pop up everytime the button is pressed, but am not quite sure how to add the conditional to it.
My code is as follows:
<div class="row-fluid">
<?php echo $this->form()->openTag($form); ?>
<div class="form-group ">
<?php echo ctrlGroup($this, AddDogForm::KEY_DOG_ID, !($this->uberAdmin)); ?>
<?php $addDog = $this->form->get(AddDogForm::KEY_ADD_BTN); ?>
<?php $addDog->setAttribute("class", "btn btn-info"); ?>
<?php $addDog->setAttribute('onclick', 'if (confirm("Are you sure?")) { document.form.submit(); } return false;'); ?>
<?php echo $this->formSubmit($addDog); ?>
</div>
<?php echo $this->form()->closeTag(); ?>
This will bring a popup up every time that the button is clicked, but I want it to check what's been typed before putting up the popup.
The following is my AddDogForm:
class AddDogForm extends Form{
const KEY_PROJECT_ID = "project_id";
const KEY_ADD_BTN = "project_add_btn";
public function __construct($name = null, $options = array()){
parent::__construct($name);
$this->setAttribute('method', 'post');
$this->setAttribute('class', 'form-inline');
$id = new Text(self::KEY_DOG_ID);
$id->setAttribute("id", self::KEY_DOG_ID);
$id->setLabel("dogid to add");
$this->add($id);
$add = new Button(self::KEY_ADD_BTN);
$add->setAttributes(array("id", self::KEY_ADD_BTN));
$add->setLabel("Add Dog");
$add->setValue("Add Dog");
$this->add($add);
}
}
UPDATE: I can add a custom validator by adding this to the AddDogForm:
public function hasNoOwner($dogId) {
$drm = DogResourceModel::create();
$dogs = $drm->find($dogId);
$dog = array_values($dogs)[0];
if($dog->getOwnerId()){
return false;
} else {
return true;
}
}
public function getInputFilterSpecification() {
$hasOwnerValidator = array(
'required' => true,
'validators' => array(
new Callback(
array(
$this,
'hasNoOwner'
)),
),
);
return array(
self::KEY_PROJECT_ID => $hasOwnerValidator
);
}
This will prevent the user from being able to input. Can I instead alter this so that it asks for confirmation rather than preventing the POST from happening?