UrlNaming - Add ID field to creation form

This commit is contained in:
Olivier PEREZ 2015-12-05 16:03:01 +01:00
parent 86fb49d51e
commit fac20a5908
6 changed files with 80 additions and 6 deletions

View File

@ -22,6 +22,7 @@ class Form
{ {
public $title; public $title;
public $id;
public $description; public $description;
public $admin_name; public $admin_name;
public $admin_mail; public $admin_mail;

View File

@ -54,6 +54,11 @@ class InputService {
return $this->returnIfNotBlank($title); return $this->returnIfNotBlank($title);
} }
public function filterId($id) {
$filtered = filter_var($id, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => POLL_REGEX]]);
return $filtered ? substr($filtered, 0, 64) : false;
}
public function filterName($name) { public function filterName($name) {
$filtered = trim($name); $filtered = trim($name);
return $this->returnIfNotBlank($filtered); return $this->returnIfNotBlank($filtered);

View File

@ -21,7 +21,7 @@
const VERSION = '0.9'; const VERSION = '0.9';
// Regex // Regex
const POLL_REGEX = '/^[a-z0-9]+$/i'; const POLL_REGEX = '/^[a-z0-9-]*$/i';
const CHOICE_REGEX = '/^[012]$/'; const CHOICE_REGEX = '/^[012]$/';
const BOOLEAN_REGEX = '/^(on|off|true|false|1|0)$/i'; const BOOLEAN_REGEX = '/^(on|off|true|false|1|0)$/i';
const BOOLEAN_TRUE_REGEX = '/^(on|true|1)$/i'; const BOOLEAN_TRUE_REGEX = '/^(on|true|1)$/i';

View File

@ -18,10 +18,9 @@
*/ */
use Framadate\Form; use Framadate\Form;
use Framadate\Services\InputService;
use Framadate\Editable;
use Framadate\Utils;
use Framadate\Security\PasswordHasher; use Framadate\Security\PasswordHasher;
use Framadate\Services\InputService;
use Framadate\Utils;
include_once __DIR__ . '/app/inc/init.php'; include_once __DIR__ . '/app/inc/init.php';
@ -55,6 +54,7 @@ if (isset($_GET['type']) && $_GET['type'] == 'date' ||
$goToStep2 = filter_input(INPUT_POST, GO_TO_STEP_2, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^(date|classic)$/']]); $goToStep2 = filter_input(INPUT_POST, GO_TO_STEP_2, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^(date|classic)$/']]);
if ($goToStep2) { if ($goToStep2) {
$title = $inputService->filterTitle($_POST['title']); $title = $inputService->filterTitle($_POST['title']);
$id = $inputService->filterId($_POST['id']);
$name = $inputService->filterName($_POST['name']); $name = $inputService->filterName($_POST['name']);
$mail = $inputService->filterMail($_POST['mail']); $mail = $inputService->filterMail($_POST['mail']);
$description = $inputService->filterDescription($_POST['description']); $description = $inputService->filterDescription($_POST['description']);
@ -76,6 +76,7 @@ if ($goToStep2) {
$error_on_password_repeat = false; $error_on_password_repeat = false;
$_SESSION['form']->title = $title; $_SESSION['form']->title = $title;
$_SESSION['form']->id = $id;
$_SESSION['form']->admin_name = $name; $_SESSION['form']->admin_name = $name;
$_SESSION['form']->admin_mail = $mail; $_SESSION['form']->admin_mail = $mail;
$_SESSION['form']->description = $description; $_SESSION['form']->description = $description;
@ -97,6 +98,10 @@ if ($goToStep2) {
$error_on_title = true; $error_on_title = true;
} }
if ($id === false) {
$error_on_id = true;
}
if ($name !== $_POST['name']) { if ($name !== $_POST['name']) {
$error_on_name = true; $error_on_name = true;
} }
@ -120,8 +125,9 @@ if ($goToStep2) {
} }
} }
if ($title && $name && $email_OK && !$error_on_title && !$error_on_description && !$error_on_name if ($title && $name && $email_OK && !$error_on_title && !$error_on_id && !$error_on_description && !$error_on_name
&& !$error_on_password && !$error_on_password_repeat) { && !$error_on_password && !$error_on_password_repeat
) {
// If no errors, we hash the password if needed // If no errors, we hash the password if needed
if ($_SESSION['form']->use_password) { if ($_SESSION['form']->use_password) {
@ -157,6 +163,11 @@ $errors = array(
'aria' => '', 'aria' => '',
'class' => '' 'class' => ''
), ),
'id' => array(
'msg' => '',
'aria' => '',
'class' => ''
),
'description' => array( 'description' => array(
'msg' => '', 'msg' => '',
'aria' => '', 'aria' => '',
@ -195,6 +206,12 @@ if (!empty($_POST[GO_TO_STEP_2])) {
$errors['title']['msg'] = __('Error', 'Something is wrong with the format'); $errors['title']['msg'] = __('Error', 'Something is wrong with the format');
} }
if ($error_on_id) {
$errors['id']['aria'] = 'aria-describeby="poll_comment_error" ';
$errors['id']['class'] = ' has-error';
$errors['id']['msg'] = __('Error', 'Something is wrong with the format');
}
if ($error_on_description) { if ($error_on_description) {
$errors['description']['aria'] = 'aria-describeby="poll_comment_error" '; $errors['description']['aria'] = 'aria-describeby="poll_comment_error" ';
$errors['description']['class'] = ' has-error'; $errors['description']['class'] = ' has-error';
@ -243,6 +260,7 @@ $smarty->assign('goToStep2', GO_TO_STEP_2);
$smarty->assign('poll_type', $poll_type); $smarty->assign('poll_type', $poll_type);
$smarty->assign('poll_title', Utils::fromPostOrDefault('title', $_SESSION['form']->title)); $smarty->assign('poll_title', Utils::fromPostOrDefault('title', $_SESSION['form']->title));
$smarty->assign('poll_id', Utils::fromPostOrDefault('id', $_SESSION['form']->id));
$smarty->assign('poll_description', Utils::fromPostOrDefault('description', $_SESSION['form']->description)); $smarty->assign('poll_description', Utils::fromPostOrDefault('description', $_SESSION['form']->description));
$smarty->assign('poll_name', Utils::fromPostOrDefault('name', $_SESSION['form']->admin_name)); $smarty->assign('poll_name', Utils::fromPostOrDefault('name', $_SESSION['form']->admin_name));
$smarty->assign('poll_mail', Utils::fromPostOrDefault('mail', $_SESSION['form']->admin_mail)); $smarty->assign('poll_mail', Utils::fromPostOrDefault('mail', $_SESSION['form']->admin_mail));

View File

@ -33,6 +33,31 @@ $(document).ready(function () {
} }
}); });
/**
* Enable/Disable custom id options
*/
var $pollId = $("#poll_id");
var $customId = $("#custom_id");
// Init checkbox + input
if (($pollId.val() || $pollId.attr('value') || "").length > 0) {
$customId.attr('checked', 'checked');
$pollId.removeAttr("disabled");
}
// Listen for checkbox changes
$customId.change(function () {
if ($(this).prop("checked")) {
$pollId
.removeAttr("disabled")
.val($pollId.attr("tmp") || $pollId.attr('value'));
} else {
$pollId
.attr("disabled", "disabled")
.attr("tmp", $pollId.val())
.val("");
}
});
/** /**
* Hide/Show password options * Hide/Show password options
*/ */

View File

@ -16,6 +16,7 @@
{__('Step 1', 'Required fields cannot be left blank.')} {__('Step 1', 'Required fields cannot be left blank.')}
</p> </p>
</div> </div>
<div class="form-group {$errors['title']['class']}"> <div class="form-group {$errors['title']['class']}">
<label for="poll_title" class="col-sm-4 control-label">{__('Step 1', 'Poll title')} *</label> <label for="poll_title" class="col-sm-4 control-label">{__('Step 1', 'Poll title')} *</label>
@ -32,6 +33,30 @@
</div> </div>
{/if} {/if}
<div class="form-group {$errors['id']['class']}">
<label for="poll_id" class="col-sm-4 control-label">{__('Step 1', 'Poll id')} *</label>
<div class="col-sm-8">
<div class="input-group">
<span class="input-group-addon">
<input id="custom_id" type="checkbox"/>
</span>
<input id="poll_id" type="text" name="id" class="form-control" {$errors['id']['aria']}
value="{$poll_id}" aria-describedBy="pollIdDesc" disabled="disabled" maxlength="64"
pattern="[A-Za-z0-9-]+"/>
</div>
<span id="pollIdDesc" class="help-block">{__('Step 1', 'Poll id rules')}</span>
<span class="help-block text-warning">{__('Step 1', 'Poll id warning')}</span>
</div>
</div>
{if !empty($errors['id']['msg'])}
<div class="alert alert-danger">
<p id="poll_title_error">
{$errors['id']['msg']}
</p>
</div>
{/if}
<div class="form-group {$errors['description']['class']}"> <div class="form-group {$errors['description']['class']}">
<label for="poll_comments" class="col-sm-4 control-label">{__('Generic', 'Description')}</label> <label for="poll_comments" class="col-sm-4 control-label">{__('Generic', 'Description')}</label>