简体   繁体   中英

zend framework - confirm save after validation

My challenge is to build a form where data gets validated before being saved, and if it fails validation the user should confirm if he want to save or not.

Practical example:

There is a registration form with 2 fields - username and is_manager. the name requires no validation, but the is_manager should be validated using Zend_Db_NoRecordExists (make sure another user isn't already manager - has is_manager = true) and if another user is already manager the the user should be prompted to confirm overwriting the manager.

The target DB table would be users(name, username, is_manager).

Any ideas are wellcomed! THank you!

You can do AJAX check for that one field, When someone change status of is_manager check box. I use JQuery in this example.

Set on Zend_Form_Element_Checkbox:

$is_manager->setAttrib('onchange', 'checkValue(this)');

Than you will have JS:

<script language = "Javascript">
function checkValue(object){
    var is_manager = $(object).is(':checked') ? 1 : 0;
    jQuery.ajax({
        url: '/default/index/ajax',  //modul/controller/action
        type: 'POST',
        data: {is_manager: is_manager},
        dataType: "json",
        success: function(result){
            if(result.status != 'OK')
                alert(result.message);
        }
    });
}
</script>

In IndexController the ajaxAction() should get request and send JSON response:

public function ajaxAction(){
    $this->view->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);

    $is_manager = $this->_getParam('is_manager');

    // DO THE OTHER STUFF/validation AND LOGIC HERE
    // ... 

    // OR set success message and status = OK
    $results = array(
        'status'  => 'error',
        'message' => 'You will overwriting existing manager!'
    );

    $this->_response->setBody(json_encode($results));
}

One approach - which doesn't depend upon javascript - is to have two forms:

  1. Your existing form with the same validations.
  2. An overwrite-confirmation form that contains little more than a submit button.

The flow would be as follows:

  1. On first entry, load the standard form.

  2. On submit of the form, if validation fails, inspect the validation errors.

    2.1 If the only validation error is the manager-overwrite, then stash his submitted info in the session and redirect him to an action in which the confirmation form is displayed.

    2.2 If not, then redisplay his current form with the errors.

  3. If the validation succeeds, then just write the record as normal.

  4. In the action for the confirmation form, just make sure that the session contains the data that you expect. On submit, write the record as normal and clear the session data.

Certainly not as slick as a client-side-script-assisted approach (as suggested by @tasmaniski), but at least there is no javascript dependence. Perhaps the optimal mix is to design your forms according to the default flow above, but to write your javascript as a progressive enhancement, allowing the flow to degrade gracefully to the above when javascript is not available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM