PDA

View Full Version : Login box/ Multiple errors with success: false?



alienwebz
16 Nov 2009, 10:58 PM
Hello, I am creating a login box using a Ext Form.

I am not sure how to get the response from the server about user validation.

I dont quite understand the difference between success: false and failure handlers, if success: false, does that mean it should read the failure handler? If so how can I get it to read different errors depending on the server response?

I am trying to get the form to alert the user on different login errors such as 'User does not exist', 'Invalid password'.

How can I have my form respond based on multiple errors?

omermx
17 Nov 2009, 4:14 AM
You could handle it like this:

User enters, username/password, submits to server. If login is successful you would send back a "success"=>true message, put whatever extra message you want in the data part of the response e.g. from PHP:



$a = array("success"=>true,"data"=>"login_ok");
$JSON_response = json_encode($a);
echo $JSON_response;


Likewise if the login failed, replace success with failed:



$a = array("failure"=>true,"data"=>"Some error message or $variable");
$JSON_response = json_encode($a);
echo $JSON_response;


When you are writing the logic to handle a failure event, you could put what was returned in the data part of the response in a MessageBox.

Hope that helps.

jsemmanuel
17 Nov 2009, 5:41 AM
You could set the success value to false and in an array return error values for each input element that failed validation

$response['success']=false,
$response['errors']['userinputfield']="Username does not exist";
json_encode($response);

or something similar. I don't remember the exact syntax and am new to ExtJS, but you could try experimenting a bit.

The error type returned by the ajax call is different if such an error is received than when just the success is returned as false with no errors. Just to return an error which can be shown i a message box, I use

$response['success']=false,
$response['msg']="Error to show in dialog";

Again, I am new to extjs, but you could try it out.

Mike Robinson
17 Nov 2009, 9:52 AM
If you dive into the ExtJS code, you'll see that when the browser notifies us that an "Asynchronous HTML Request" has finished, ExtJS examines the HTTP status-code: "200 OK" is goodness; other codes are not. (All of the usual rules of the HTTP protocol standard apply in the usual way...)

It also looks for and examines a success property in the response.

The idea here is:


First, when we get our letter back from the post-office, we need to see if it was stamped "Return to Sender" or "Postage Due" or what-not. If it is, then we presume that the message did not get through.
Then, we need to open the envelope and see if the message is "I do!" =P~ or "Dear John ..." :((


There are three outcomes:


Request received, understood, and carried out.
Request received, understood, and (for whatever reason) declined.
Mechanical failure of the transport. The "round trip" between client and server somehow crashed-and-burned.


So, what I would probably do in your case is to treat login-failure as "Dear John." I'd return a packet with "success: false" and an appropriate explanation. The server received the request, the server understood it, and the response packet was successfully received ... but ... "the answer is 'no.'" This approach keeps the handler well-separated from those that (perhaps on the Connection-class level) must deal with mechanical failure.

alienwebz
17 Nov 2009, 9:50 PM
so lets say that the server is sending this back:

{success: false,
errors: {
pass: "Invalid Password!";
},
errormsg: "The provided password does not match our records."}

which handler will respond to the false success value? the success or the failure handler?

jsemmanuel
19 Nov 2009, 5:56 AM
Have a look at the section
Rounding up your validation strategy with server-side validation of form fields in page 2 of
http://www.packtpub.com/article/load-validate-submit-forms-ext-js-3.0-part2?utm_source=js_ext_js_cook_abr1_1009&utm_medium=content&utm_campaign=janice

CrazyEnigma
19 Nov 2009, 11:33 AM
The bottom line is that you will need to have the following response to your submit.

{success: true/false}

As someone already pointed out, the true or false essentially directs the response to the success or failure handler.

From here on in, you can design your message however appropriate.
The Login Tutorial (http://www.extjs.com/learn/Tutorial:Basic_Login) does give you starting point.

The key is to deserialize the message before you can reference it as an object.


// ...
failure: function(form, action) {
var obj = Ext.util.JSON.decode(action.response.responseText);
Ext.Msg.alert(obj.error.pass + ': ' + obj.errormsg);
}