converted form renderer validation to Parsley

This commit is contained in:
Jonathan Labreuille
2015-11-06 12:59:50 +01:00
parent 31305a04c0
commit dfadda2d12
2 changed files with 32 additions and 37 deletions

View File

@ -4,60 +4,57 @@ namespace MailPoet\Form\Block;
abstract class Base { abstract class Base {
protected static function getInputValidation($block) { protected static function getInputValidation($block) {
$rules = array(); $rules = array();
$is_required = false;
if($block['id'] === 'email') { if($block['id'] === 'email') {
$is_required = true; $rules['required'] = true;
$rules[] = 'custom[email]'; $rules['error-message'] = __('You need to specify a valid email address');
} }
if($block['id'] === 'segments') { if($block['id'] === 'segments') {
$is_required = true; $rules['required'] = true;
$rules['mincheck'] = 1;
$rules['error-message'] = __('You need to select a list');
} }
if(!empty($block['params']['required'])) { if(!empty($block['params']['required'])) {
$is_required = true; $rules['required'] = true;
} }
if(isset($block['params']['validate'])) { if(!empty($block['params']['validate'])) {
if(is_array($block['params']['validate'])) { if($block['params']['validate'] === 'phone') {
// handle multiple validation rules $rules['pattern'] = "^[\d\+\-\.\(\)\/\s]*$";
foreach($block['params']['validate'] as $rule) { $rules['error-message'] = __('You need to specify a valid phone number');
$rules[] = 'custom['.$rule.']'; } else {
} $rules['type'] = $block['params']['validate'];
} else if(strlen(trim($block['params']['validate'])) > 0) {
// handle single validation rule
$rules[] = 'custom['.$block['params']['validate'].']';
} }
} }
// generate string if there is at least one rule to validate against
$validation = ''; $validation = '';
if(!empty($rules)) { if(!empty($rules)) {
$rules = array_unique($rules); $rules = array_unique($rules);
//return 'validate['.join(',', $rules).']'; foreach($rules as $rule => $value) {
// TODO: convert to Parsley format! if(is_bool($value)) {
$value = ($value) ? 'true' : 'false';
}
$validation .= 'data-parsley-'.$rule.'="'.$value.'"';
}
} }
if($is_required === true) {
$validation .= ' required';
}
return $validation; return $validation;
} }
protected static function renderLabel($block) { protected static function renderLabel($block) {
$html = ''; $html = '';
// if the label is displayed as a placeholder, we don't display a label outside if(
if(isset($block['params']['label_within']) isset($block['params']['label_within'])
&& $block['params']['label_within']) { && $block['params']['label_within']
) {
return $html; return $html;
} }
if(isset($block['params']['label']) if(isset($block['params']['label'])
&& strlen(trim($block['params']['label'])) > 0) { && strlen(trim($block['params']['label'])) > 0) {
$html .= '<label class="mailpoet_'.$block['type'].'_label">'.$block['params']['label']; $html .= '<label class="mailpoet_'.$block['type'].'_label">';
$html .= $block['params']['label'];
if(isset($block['params']['required']) && $block['params']['required']) { if(isset($block['params']['required']) && $block['params']['required']) {
$html .= ' <span class="mailpoet_required">*</span>'; $html .= ' <span class="mailpoet_required">*</span>';
@ -71,7 +68,10 @@ abstract class Base {
protected static function renderInputPlaceholder($block) { protected static function renderInputPlaceholder($block) {
$html = ''; $html = '';
// if the label is displayed as a placeholder, // if the label is displayed as a placeholder,
if(isset($block['params']['label_within']) && $block['params']['label_within']) { if(
isset($block['params']['label_within'])
&& $block['params']['label_within']
) {
// display only label // display only label
$html .= ' placeholder="'; $html .= ' placeholder="';
$html .= static::getFieldLabel($block); $html .= static::getFieldLabel($block);

View File

@ -6,18 +6,13 @@
<%= __('Nothing') %> <%= __('Nothing') %>
</option> </option>
<option {{#ifCond params.validate '==' 'onlyNumberSp'}}selected="selected"{{/ifCond}} <option {{#ifCond params.validate '==' 'number'}}selected="selected"{{/ifCond}}
value="onlyNumberSp"> value="number">
<%= __('Numbers only') %> <%= __('Numbers only') %>
</option> </option>
<option {{#ifCond params.validate '==' 'onlyLetterSp'}}selected="selected"{{/ifCond}} <option {{#ifCond params.validate '==' 'alphanum'}}selected="selected"{{/ifCond}}
value="onlyLetterSp"> value="alphanum">
<%= __('Letters only') %>
</option>
<option {{#ifCond params.validate '==' 'onlyLetterNumber'}}selected="selected"{{/ifCond}}
value="onlyLetterNumber">
<%= __('Alphanumerical') %> <%= __('Alphanumerical') %>
</option> </option>