Reputation: 6098
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?
Upvotes: 0
Views: 181
Reputation: 92
May be you can add custom validator on post data on one of the field to validate against and then you can just show a general message.
There are two possible way you can achieve this.
add validator/custom validator based on your requirement on runtime in controller
public function fooAction() {
$form = new Dog_Form();
if ($this->getRequest()->isPost()) {
// all post data is valid now we validate for dog/owner thing
$form->getElement('star_tag')->addValidator('Db_NoRecordExists',false,
[
'table' => 'table name',
'field' => 'field to check duplicate',
'messages' => array('recordFound' => 'already_linked')
]
);
// redirect after successful insert
}
$this->view->form = $form;
}
you can use isValidPartial method of zend form and do your validation over there. check http://framework.zend.com/manual/1.12/en/zend.form.quickstart.html#zend.form.quickstart.validate to see how it works.
Upvotes: 1
Reputation: 5679
Transmit variable hasOwner
to view and add onclick
if hasOwner
is not empty.
<?php if (!empty($hasOwner)) $addDog->setAttribute('onclick', 'if (confirm("Are you sure?")) { document.form.submit(); } return false;'); ?>
If dog is selected in form, you can make ajax request to check for owner. Remove onclick attribute and add js code to check.
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
$('form').submit(function() {
var hasOwner = 0;
$.ajax('/url/to/check', {
'async':false,
'data':{'dog':$('#<?php echo AddDogForm::KEY_DOG_ID; ?>').val()},
'dataType': 'json',
'success':function(data){
hasOwner = data['hasOwner'];
}
})
if (hasOwner) {
if (confirm("Are you sure?")) return true;
else return false;
} else {
return true;
}
});
</script>
/url/to/check
should return json response if dog has owner
<?php
....
$hasOwner = hasOwner($_REQUEST['dog']); // function
echo \json_encode(['hasOwner'=>$hasOwner]);
exit;
....
?>
Upvotes: 0