Fixed form error management

This commit is contained in:
Franck Allimant
2014-05-02 20:08:38 +02:00
parent 4bab223421
commit 621c17c97d
2 changed files with 44 additions and 46 deletions

View File

@@ -77,15 +77,10 @@ class CouponController extends BaseAdminController
// Parameters given to the template // Parameters given to the template
$args = []; $args = [];
$i18n = new I18n();
/** @var Lang $lang */
$lang = $this->getSession()->getLang();
$eventToDispatch = TheliaEvents::COUPON_CREATE; $eventToDispatch = TheliaEvents::COUPON_CREATE;
if ($this->getRequest()->isMethod('POST')) { if ($this->getRequest()->isMethod('POST')) {
$this->validateCreateOrUpdateForm( $this->validateCreateOrUpdateForm(
$i18n,
$lang,
$eventToDispatch, $eventToDispatch,
'created', 'created',
'creation' 'creation'
@@ -140,16 +135,11 @@ class CouponController extends BaseAdminController
// Parameters given to the template // Parameters given to the template
$args = []; $args = [];
$i18n = new I18n();
/** @var Lang $lang */
$lang = $this->getSession()->getLang();
$eventToDispatch = TheliaEvents::COUPON_UPDATE; $eventToDispatch = TheliaEvents::COUPON_UPDATE;
// Update // Update
if ($this->getRequest()->isMethod('POST')) { if ($this->getRequest()->isMethod('POST')) {
$this->validateCreateOrUpdateForm( $this->validateCreateOrUpdateForm(
$i18n,
$lang,
$eventToDispatch, $eventToDispatch,
'updated', 'updated',
'update', 'update',
@@ -189,6 +179,7 @@ class CouponController extends BaseAdminController
// Pass it to the parser // Pass it to the parser
$this->getParserContext()->addForm($changeForm); $this->getParserContext()->addForm($changeForm);
} }
$args['couponCode'] = $coupon->getCode(); $args['couponCode'] = $coupon->getCode();
$args['availableCoupons'] = $this->getAvailableCoupons(); $args['availableCoupons'] = $this->getAvailableCoupons();
$args['couponInputsHtml'] = $couponManager->drawBackOfficeInputs(); $args['couponInputsHtml'] = $couponManager->drawBackOfficeInputs();
@@ -456,8 +447,6 @@ class CouponController extends BaseAdminController
/** /**
* Validate the CreateOrUpdate form * Validate the CreateOrUpdate form
* *
* @param I18n $i18n Local code (fr_FR)
* @param Lang $lang Local variables container
* @param string $eventToDispatch Event which will activate actions * @param string $eventToDispatch Event which will activate actions
* @param string $log created|edited * @param string $log created|edited
* @param string $action creation|edition * @param string $action creation|edition
@@ -465,15 +454,15 @@ class CouponController extends BaseAdminController
* *
* @return $this * @return $this
*/ */
protected function validateCreateOrUpdateForm(I18n $i18n, Lang $lang, $eventToDispatch, $log, $action, Coupon $model = null) protected function validateCreateOrUpdateForm($eventToDispatch, $log, $action, Coupon $model = null)
{ {
// Create the form from the request // Create the form from the request
$creationForm = new CouponCreationForm($this->getRequest()); $couponForm = new CouponCreationForm($this->getRequest());
$message = false; $message = false;
try { try {
// Check the form against conditions violations // Check the form against conditions violations
$form = $this->validateForm($creationForm, 'POST'); $form = $this->validateForm($couponForm, 'POST');
$couponEvent = $this->feedCouponCreateOrUpdateEvent($form, $model); $couponEvent = $this->feedCouponCreateOrUpdateEvent($form, $model);
@@ -492,32 +481,39 @@ class CouponController extends BaseAdminController
) )
); );
if ($this->getRequest()->get('save_mode') == 'stay') {
$this->redirect( $this->redirect(
str_replace( str_replace(
'{id}', '{id}',
$couponEvent->getCouponModel()->getId(), $couponEvent->getCouponModel()->getId(),
$creationForm->getSuccessUrl() $couponForm->getSuccessUrl()
) )
); );
} catch (FormValidationException $e) { exit();
}
// Redirect to the success URL
$this->redirectToRoute('admin.coupon.list');
} catch (FormValidationException $ex) {
// Invalid data entered // Invalid data entered
$message = 'Please check your input:'; $message = $this->createStandardFormValidationErrorMessage($ex);
} catch (\Exception $e) { } catch (\Exception $ex) {
// Any other error // Any other error
$message = $this->getTranslator()->trans('Sorry, an error occurred: %err', ['%err' => $e->getMessage()]); $message = $this->getTranslator()->trans('Sorry, an error occurred: %err', ['%err' => $ex->getMessage()]);
$this->logError($action, $message, $e); $this->logError($action, $message, $ex);
} }
if ($message !== false) { if ($message !== false) {
// Mark the form as with error // Mark the form as with error
$creationForm->setErrorMessage($message); $couponForm->setErrorMessage($message);
// Send the form and the error to the parser // Send the form and the error to the parser
$this->getParserContext() $this->getParserContext()
->addForm($creationForm) ->addForm($couponForm)
->setGeneralError($message); ->setGeneralError($message);
} }

View File

@@ -56,7 +56,7 @@ $(function($){
$('#condition-list').html($.couponManager.intlPleaseSelectAnotherCondition); $('#condition-list').html($.couponManager.intlPleaseSelectAnotherCondition);
} }
} }
}).done(function(data) { }).done(function() {
// Reload condition summaries ajax // Reload condition summaries ajax
$.couponManager.displayConditionsSummary(); $.couponManager.displayConditionsSummary();
}); });
@@ -207,24 +207,9 @@ $(function($){
}; };
// *********************************************** // ***********************************************
// *************** Manager Coupon **************** // *************** Manager Coupon ****************
// *********************************************** // ***********************************************
// Reload effect inputs when changing effect
$.couponManager.onEffectChange = function() {
var mainDiv = $('#coupon-type');
var optionSelected = mainDiv.find('#type option:selected');
mainDiv.find('.typeToolTip').html(optionSelected.data('description'));
mainDiv.find('#type').on('change', function () {
var optionSelected = $('option:selected', this);
$.couponManager.displayEfffect(optionSelected);
});
};
$.couponManager.onEffectChange();
$.couponManager.displayEfffect = function(optionSelected) { $.couponManager.displayEfffect = function(optionSelected) {
var mainDiv = $('#coupon-type'); var mainDiv = $('#coupon-type');
@@ -251,6 +236,20 @@ $(function($){
}); });
}; };
// Reload effect inputs when changing effect
$.couponManager.onEffectChange = function() {
var mainDiv = $('#coupon-type');
var optionSelected = mainDiv.find('#type option:selected');
mainDiv.find('.typeToolTip').html(optionSelected.data('description'));
mainDiv.find('#type').on('change', function () {
var optionSelected = $('option:selected', this);
$.couponManager.displayEfffect(optionSelected);
});
};
$.couponManager.onEffectChange();
$.couponManager.displayConditionsSummary = function() { $.couponManager.displayConditionsSummary = function() {
var mainDiv = $('#condition-list'); var mainDiv = $('#condition-list');
mainDiv.html('<div class="loading" ></div>'); mainDiv.html('<div class="loading" ></div>');
@@ -277,7 +276,9 @@ $(function($){
// Set max usage to unlimited or not // Set max usage to unlimited or not
$.couponManager.onUsageUnlimitedChange = function() { $.couponManager.onUsageUnlimitedChange = function() {
var $isUnlimited = $('#is-unlimited'); var $isUnlimited = $('#is-unlimited');
$maxUsage = $('#max-usage');
var $maxUsage = $('#max-usage');
if ($maxUsage.val() == -1) { if ($maxUsage.val() == -1) {
$isUnlimited.prop('checked', true); $isUnlimited.prop('checked', true);
$maxUsage.hide(); $maxUsage.hide();
@@ -299,5 +300,6 @@ $(function($){
} }
}); });
}; };
$.couponManager.onUsageUnlimitedChange(); $.couponManager.onUsageUnlimitedChange();
}); });