Form editor update

- when a custom field is updated, the matching form field is now also updated
- ability to toggle "is_required" on First name & Last name
- fixed position of validation errors on segment selection, checkbox and radio
- fixed form subscription not working when using custom fields
- fixed sortable in segment selection after list update (add/remove)
- updated position of messages in form subscription
This commit is contained in:
Jonathan Labreuille
2016-01-18 17:23:10 +01:00
parent a6d802e2fa
commit cabe2d61b7
14 changed files with 82 additions and 47 deletions

View File

@ -617,6 +617,17 @@ var WysijaForm = {
// this is a url, so do not encode the protocol // this is a url, so do not encode the protocol
return encodeURI(str).replace(/[!'()*]/g, escape); return encodeURI(str).replace(/[!'()*]/g, escape);
} }
},
updateBlock: function(data) {
var hasUpdated = false;
WysijaForm.getBlocks().each(function(b) {
if(b.block.getData().id == data.id) {
hasUpdated = true;
b.block.redraw(data);
}
});
return hasUpdated;
} }
}; };

View File

@ -20,10 +20,7 @@ function(
$('form.mailpoet_form').each(function() { $('form.mailpoet_form').each(function() {
var form = $(this); var form = $(this);
form.parsley({ form.parsley().on('form:submit', function(parsley) {
errorsWrapper: '<p></p>',
errorTemplate: '<span></span>'
}).on('form:submit', function(parsley) {
var data = form.serializeObject() || {}; var data = form.serializeObject() || {};

View File

@ -13,7 +13,9 @@ abstract class Base {
if($block['id'] === 'segments') { if($block['id'] === 'segments') {
$rules['required'] = true; $rules['required'] = true;
$rules['mincheck'] = 1; $rules['mincheck'] = 1;
$rules['error-message'] = __('You need to select a list'); $rules['group'] = $block['id'];
$rules['errors-container'] = '.mailpoet_error_'.$block['id'];
$rules['required-message'] = __('You need to select a list');
} }
if(!empty($block['params']['required'])) { if(!empty($block['params']['required'])) {
@ -29,7 +31,7 @@ abstract class Base {
} }
} }
if($block['type'] === 'radio') { if(in_array($block['type'], array('radio', 'checkbox'))) {
$rules['group'] = 'custom_field_'.$block['id']; $rules['group'] = 'custom_field_'.$block['id'];
$rules['errors-container'] = '.mailpoet_error_'.$block['id']; $rules['errors-container'] = '.mailpoet_error_'.$block['id'];
$rules['required-message'] = __('You need to select at least one option.'); $rules['required-message'] = __('You need to select at least one option.');

View File

@ -9,10 +9,6 @@ class Checkbox extends Base {
$field_name = static::getFieldName($block); $field_name = static::getFieldName($block);
$field_validation = static::getInputValidation($block); $field_validation = static::getInputValidation($block);
// TODO: check if it still makes sense
// create hidden default value
// $html .= '<input type="hidden"name="'.$field_name.'" value="0" '.static::getInputValidation($block).'/>';
$html .= '<p class="mailpoet_paragraph">'; $html .= '<p class="mailpoet_paragraph">';
$html .= static::renderLabel($block); $html .= static::renderLabel($block);
@ -35,6 +31,8 @@ class Checkbox extends Base {
$html .= '</label>'; $html .= '</label>';
} }
$html .= '<span class="mailpoet_error_'.$block['id'].'"></span>';
$html .= '</p>'; $html .= '</p>';
return $html; return $html;

View File

@ -13,8 +13,6 @@ class Radio extends Base {
$html .= static::renderLabel($block); $html .= static::renderLabel($block);
$html .= '<span class="mailpoet_error_'.$block['id'].'"></span>';
foreach($block['params']['values'] as $option) { foreach($block['params']['values'] as $option) {
$html .= '<label class="mailpoet_radio_label">'; $html .= '<label class="mailpoet_radio_label">';
@ -33,6 +31,8 @@ class Radio extends Base {
$html .= '</label>'; $html .= '</label>';
} }
$html .= '<span class="mailpoet_error_'.$block['id'].'"></span>';
$html .= '</p>'; $html .= '</p>';
return $html; return $html;

View File

@ -30,6 +30,8 @@ class Segment extends Base {
} }
} }
$html .= '<span class="mailpoet_error_'.$block['id'].'"></span>';
$html .= '</p>'; $html .= '</p>';
return $html; return $html;

View File

@ -6,11 +6,11 @@ class Submit extends Base {
static function render($block) { static function render($block) {
$html = ''; $html = '';
$html .= '<input class="mailpoet_submit" type="submit" '; $html .= '<p class="mailpoet_submit"><input type="submit" ';
$html .= 'value="'.static::getFieldLabel($block).'" '; $html .= 'value="'.static::getFieldLabel($block).'" ';
$html .= '/>'; $html .= '/></p>';
return $html; return $html;
} }

View File

@ -237,6 +237,16 @@ class Subscriber extends Model {
} }
} }
// custom fields
$custom_fields = array();
foreach($data as $key => $value) {
if(strpos($key, 'cf_') === 0) {
$custom_fields[substr($key, 3)] = $value;
unset($data[$key]);
}
}
if($subscriber === false) { if($subscriber === false) {
$subscriber = static::create(); $subscriber = static::create();
$subscriber->hydrate($data); $subscriber->hydrate($data);

View File

@ -113,13 +113,19 @@ class Subscribers {
} }
if(!empty($errors)) { if(!empty($errors)) {
wp_send_json(array('errors' => $errors)); wp_send_json(array(
'result' => false,
'errors' => $errors
));
} }
$subscriber = Subscriber::subscribe($data, $segment_ids); $subscriber = Subscriber::subscribe($data, $segment_ids);
$result = false;
if($subscriber === false || !$subscriber->id()) { if($subscriber === false || !$subscriber->id()) {
$errors = array_merge($errors, $subscriber->getValidationErrors()); $errors = array_merge($errors, $subscriber->getValidationErrors());
} else {
$result = true;
} }
if(!empty($errors)) { if(!empty($errors)) {

View File

@ -66,7 +66,7 @@
type="radio" type="radio"
name="on_success" name="on_success"
value="message" value="message"
<% if(form.data.settings.on_success is empty or form.data.settings.on_success == 'message') %> <% if(form.settings.on_success is empty or form.settings.on_success == 'message') %>
checked="checked" checked="checked"
<% endif %> <% endif %>
/><%= __('Show message') %> /><%= __('Show message') %>
@ -77,15 +77,15 @@
type="radio" type="radio"
name="on_success" name="on_success"
value="page" value="page"
<% if(form.data.settings.on_success == 'page') %> <% if(form.settings.on_success == 'page') %>
checked="checked" checked="checked"
<% endif %> <% endif %>
/><%= __('Go to page') %> /><%= __('Go to page') %>
</label> </label>
</p> </p>
<!-- default success message --> <!-- default success message -->
<% if form.data.settings.success_message %> <% if form.settings.success_message %>
<% set success_message = form.data.settings.success_message %> <% set success_message = form.settings.success_message %>
<% else %> <% else %>
<% set success_message = __('Check your inbox now to confirm your subscription.') %> <% set success_message = __('Check your inbox now to confirm your subscription.') %>
<% endif %> <% endif %>
@ -103,7 +103,7 @@
<select name="success_page"> <select name="success_page">
<% for page in pages %> <% for page in pages %>
<option value="<%= page.id %>" <option value="<%= page.id %>"
<%- if form.data.settings.success_page != page.id %> <%- if form.settings.success_page == page.id %>
<%=- ' selected="selected"' %> <%=- ' selected="selected"' %>
<%- endif %>><%= page.title %></option> <%- endif %>><%= page.title %></option>
<% endfor %> <% endfor %>
@ -445,28 +445,31 @@
return false; return false;
} }
var message = null; if(callback !== false) {
var message = null;
if(response.is_widget === true) { if(response.is_widget === true) {
message = "<%= __('Saved! The changes are already active in your widget.') %>"; message = "<%= __('Saved! The changes are already active in your widget.') %>";
} else { } else {
message = "<%= __('Saved! Add this form to %1$sa widget%2$s.') | format("<a href='widgets.php' target='_blank'>", '</a>') | raw %>"; message = "<%= __('Saved! Add this form to %1$sa widget%2$s.') | format("<a href='widgets.php' target='_blank'>", '</a>') | raw %>";
} }
if(message !== null) { if(message !== null) {
MailPoet.Notice.hide(); MailPoet.Notice.hide();
MailPoet.Notice.success(message, { MailPoet.Notice.success(message, {
scroll: true, scroll: true,
static: (response.is_widget === false) static: (response.is_widget === false)
}); });
} }
// if there is a callback, call it! // if there is a callback, call it!
if(callback !== undefined) { if(callback !== undefined) {
callback(); callback();
}
} }
}); });
} }
window.mailpoet_form_save = mailpoet_form_save;
// toolbar: on success toggle // toolbar: on success toggle
$(document).on('change', 'input[name="on_success"]', toggleOnSuccessOptions); $(document).on('change', 'input[name="on_success"]', toggleOnSuccessOptions);
@ -506,7 +509,7 @@
// open popup // open popup
MailPoet.Modal.popup({ MailPoet.Modal.popup({
title: "<%= __('Add new field') %>", title: "<%= __('Add new field') %>",
template: $('#form_template_field_new').html() template: $('#form_template_field_form').html()
}); });
return false; return false;
@ -524,7 +527,7 @@
if(response.result !== false) { if(response.result !== false) {
MailPoet.Modal.popup({ MailPoet.Modal.popup({
title: "<%= __('Edit field') %>", title: "<%= __('Edit field') %>",
template: $('#form_template_field_new').html(), template: $('#form_template_field_form').html(),
data: response data: response
}); });
} }
@ -685,8 +688,8 @@
) %> ) %>
<!-- custom field: new --> <!-- custom field: new -->
<%= partial('form_template_field_new', <%= partial('form_template_field_form',
'form/templates/settings/field_new.hbs' 'form/templates/settings/field_form.hbs'
) %> ) %>
<!-- form preview --> <!-- form preview -->

View File

@ -6,7 +6,7 @@
{{#ifCond type '==' 'input'}} {{#ifCond type '==' 'input'}}
{{> _settings_label }} {{> _settings_label }}
{{> _settings_label_within }} {{> _settings_label_within }}
{{#ifCond field 'in' 'first_name,last_name' }} {{#ifCond id 'in' 'first_name,last_name' }}
{{> _settings_required }} {{> _settings_required }}
{{/ifCond}} {{/ifCond}}
{{/ifCond}} {{/ifCond}}

View File

@ -78,7 +78,16 @@
data: data data: data
}).done(function(response) { }).done(function(response) {
if(response.result === true) { if(response.result === true) {
// close popup
MailPoet.Modal.close();
if(WysijaForm.updateBlock(response.field) === true) {
// trigger save, if a block has been updated
mailpoet_form_save(false);
}
mailpoet_form_fields(); mailpoet_form_fields();
if(data.id) { if(data.id) {
MailPoet.Notice.success( MailPoet.Notice.success(
"<%= __('Updated custom field “"+data.name+"“') %>" "<%= __('Updated custom field “"+data.name+"“') %>"
@ -88,9 +97,6 @@
"<%= __('Added custom field “"+data.name+"“') %>" "<%= __('Added custom field “"+data.name+"“') %>"
); );
} }
// close popup
MailPoet.Modal.success();
} else { } else {
if(response.errors.length > 0) { if(response.errors.length > 0) {
$(response.errors).each(function(i, error) { $(response.errors).each(function(i, error) {

View File

@ -20,7 +20,6 @@
mailpoet_segment_selection_render(); mailpoet_segment_selection_render();
setInputNames(); setInputNames();
setupSortableSegments();
// add segment // add segment
$('.mailpoet_segment_add').on('click', function() { $('.mailpoet_segment_add').on('click', function() {
@ -125,6 +124,7 @@
$(item).find('.mailpoet_segment_id').attr('name', 'params[values]['+index+'][id]'); $(item).find('.mailpoet_segment_id').attr('name', 'params[values]['+index+'][id]');
$(item).find('.mailpoet_segment_name').attr('name', 'params[values]['+index+'][name]'); $(item).find('.mailpoet_segment_name').attr('name', 'params[values]['+index+'][name]');
}); });
setupSortableSegments();
} }
}); });
<{{!}}/script> <{{!}}/script>

View File

@ -16,8 +16,6 @@
class="mailpoet_form mailpoet_form_<%= form_type %>" class="mailpoet_form mailpoet_form_<%= form_type %>"
novalidate novalidate
> >
<div class="mailpoet_message"></div>
<input type="hidden" name="form_id" value="<%= form.id %>" /> <input type="hidden" name="form_id" value="<%= form.id %>" />
<% if not(form.settings.segments_selected_by == 'user') %> <% if not(form.settings.segments_selected_by == 'user') %>
@ -26,6 +24,8 @@
<% endfor %> <% endfor %>
<% endif %> <% endif %>
<%= html | raw %> <%= html | raw %>
<div class="mailpoet_message"></div>
</form> </form>
</div> </div>
<%= after_widget | raw %> <%= after_widget | raw %>