You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When submitting a form using Snowboard.request in WinterCMS, validation errors that should be caught in the responseError of the error handler are instead returned in the responseData of the error handler.
The expected behavior is that validation errors, when thrown as a ValidationException, should be returned in responseError. However, they are currently being returned in responseData, which is inconsistent with the expected Snowboard behavior for error handling.
Steps to replicate
Create a simple form in WinterCMS and trigger a validation error using the ValidationException class.
Use the following form partial and backend handler code to replicate the issue:
Form Partial (HTML + JS)
<formid="subscription-form" class="row mb-0"><!-- Name Input --><divclass="form-group"><labelfor="name">Name</label><inputtype="text"
name="name"
id="name"
class="required"
placeholder="John Doe"
required></div><!-- Email Input --><divclass="form-group"><labelfor="email">Email</label><inputtype="email"
name="email"
id="email"
class="required"
placeholder="user@company.com"
required></div><!-- Message Input --><divclass="form-group"><labelfor="message">Message</label><textareaname="message"
id="message"
class="required"
cols="30"
rows="5"
placeholder="Please let us know how we can help you..."
required></textarea></div><!-- Submit Button --><divclass="col-12"><buttontype="submit">Subscribe</button></div></form><divclass="form-result"></div><script>document.addEventListener('DOMContentLoaded',function(){constform=document.getElementById('subscription-form');constformResult=document.querySelector('.form-result');form.addEventListener('submit',function(e){e.preventDefault();// Trigger a Snowboard requestSnowboard.request(form,'onSubscribe',{loading: form.querySelector('button'),success: function(response){// Handle successformResult.innerHTML=`<div class="alert alert-success">${response.message}</div>`;form.reset();},error: function(response,request){// Expected: responseError contains the error details// Actual: validation errors are returned in request.responseDataconsole.log('Error: ',request.responseError);// This is nullconsole.log('Data: ',request.responseData);// This contains the validation errors// Display error messagesif(request.responseData&&request.responseData.X_WINTER_ERROR_FIELDS){leterrorMessage='<div class="alert alert-danger"><ul>';Object.keys(request.responseData.X_WINTER_ERROR_FIELDS).forEach(function(field){request.responseData.X_WINTER_ERROR_FIELDS[field].forEach(function(error){errorMessage+=`<li>${error}</li>`;});});errorMessage+='</ul></div>';formResult.innerHTML=errorMessage;}else{formResult.innerHTML=`<div class="alert alert-danger">${response.message}</div>`;}}});});});</script>
Backend Handler (PHP)
useWinter\Storm\Exception\ValidationException;
publicfunctiononSubscribe()
{
$data = post();
// Validation rules$rules = [
'name' => 'required|string|max:255',
'email' => 'required|email',
'message' => 'required|string|max:500',
];
// Validate input data$validator = Validator::make($data, $rules);
if ($validator->fails()) {
thrownewValidationException($validator); // Should return in responseError
}
// Simulate a successful save to the databasereturn [
'message' => 'Thank you for subscribing!',
];
}
Expected Behavior:
When a ValidationException is thrown, the error response should be captured in responseError in the error handler of the Snowboard.request.
Actual Behavior:
The validation errors are being returned in responseData instead of responseError.
responseError is null in the error function.
Possible Fix:
Ensure that validation errors, when thrown as ValidationException, are returned in the responseError instead of responseData.
Environment:
WinterCMS version: 1.2.x
Snowboard: Latest
PHP version: 8.1
Thank you for looking into this issue!
Workaround
No response
The text was updated successfully, but these errors were encountered:
@diegoflorez this is partially intended, based on the behavior of the previous AJAX framework.
Validation errors in the PHP side are returned with a response code of HTTP 406 Not Acceptable, which in the old framework was interpreted as a "successful" response, because it may include partial updates (such as displaying said validation errors in a specific way in a component, for example). Thus, the validation errors are simply returned as response data, and we have kept that behavior intact with Snowboard.
However, the responseError variable is reserved for the main exception message and should be passed through, but it appears it may not be. So that particular part of the functionality may have a bug.
bennothommo
changed the title
Validation Errors in Ajax Requests are Returned in responseData instead of responseError
The "responseError" variable in Snowboard is null when a ValidationException is thrown
Oct 3, 2024
Winter CMS Build
1.2
PHP Version
8.1
Database engine
MySQL/MariaDB
Plugins installed
No response
Issue description
When submitting a form using
Snowboard.request
in WinterCMS, validation errors that should be caught in theresponseError
of theerror
handler are instead returned in theresponseData
of theerror
handler.The expected behavior is that validation errors, when thrown as a
ValidationException
, should be returned inresponseError
. However, they are currently being returned inresponseData
, which is inconsistent with the expected Snowboard behavior for error handling.Steps to replicate
ValidationException
class.Form Partial (HTML + JS)
Backend Handler (PHP)
Expected Behavior:
When a
ValidationException
is thrown, the error response should be captured inresponseError
in theerror
handler of theSnowboard.request
.Actual Behavior:
responseData
instead ofresponseError
.responseError
isnull
in theerror
function.Possible Fix:
Ensure that validation errors, when thrown as
ValidationException
, are returned in theresponseError
instead ofresponseData
.Environment:
Thank you for looking into this issue!
Workaround
No response
The text was updated successfully, but these errors were encountered: