Skip to content

Commit

Permalink
Adding Form Validation Rules
Browse files Browse the repository at this point in the history
  • Loading branch information
digitaldreams committed Sep 24, 2021
1 parent 74cf507 commit 302b1bf
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 47 deletions.
5 changes: 3 additions & 2 deletions resources/templates/reactjs/form.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React, {useState} from 'react';
import useForm from "../../hooks/useForm";
import useTranslation from "../../hooks/useTranslation";

export default function @@componentName@@(){
const {trans} = useTranslation();
const [registering, setRegistering] = useState();
const {errors, data, handleChange, validateAll} = useForm({
const {errors, data, handleChange, validateAll} = useForm(
@@validationRules@@
}, {@@initialData@@});
, {@@initialData@@});

const handleFinish = (event) => {
event.preventDefault();
Expand Down
49 changes: 45 additions & 4 deletions src/lara-crud/Crud/ReactJs/ReactJsFormCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class ReactJsFormCrud implements Crud
*/
protected $controller;

protected array $formRules = [];

/**
* ReactJsFormCrud constructor.
*
Expand All @@ -39,11 +41,11 @@ public function __construct(Model $model, Controller $controller)

public function template()
{
print_r($this->getValidationRules());
$this->generateFields();

return (new TemplateManager('reactjs/form.txt', [
'componentName' => $this->componentName,
'validationRules' => '',
'validationRules' => json_encode($this->formRules, JSON_PRETTY_PRINT),
'initialData' => '',
'fields' => '',
]))->get();
Expand All @@ -56,20 +58,59 @@ public function save()
$migrationFile->fwrite($this->template());
}

protected function generateFields(): string
protected function generateFields()
{
$validationRules = $this->getValidationRules();
if (! empty($validationRules)) {
foreach ($validationRules as $column => $rule) {
$rule = is_string($rule) ? explode('|', $rule) : $rule;
$inputBuilder = new ReactJsFormInputBuilder($rule);
if (true !== $inputBuilder->isArray) {
$this->formRules[$column] = $this->formRules($inputBuilder);
}
}
}
}

public function fieldTemplate($column, $inputBuilder)
protected function formRules(ReactJsFormInputBuilder $inputBuilder)
{
$rules = [];
if ($inputBuilder->required) {
$rules['required'] = true;
}

if ($inputBuilder->min > 0) {
$key = 'text' == $inputBuilder->type ? 'minlength' : 'min';
$rules[$key] = $inputBuilder->min;
}

if ($inputBuilder->max > 0) {
$key = 'text' == $inputBuilder->type ? 'maxlength' : 'max';

$rules[$key] = $inputBuilder->max;
}

if (in_array($inputBuilder->type, ['radio', 'select'])) {
$rules['in'] = $inputBuilder->options;
}
if ('number' == $inputBuilder->type) {
$rules['numeric'] = true;
}
if ('url' == $inputBuilder->type) {
$rules['url'] = true;
}
if ('email' == $inputBuilder->type) {
$rules['email'] = true;
}
if ('file' == $inputBuilder->type) {
$rules['mimes'] = [];
}

return $rules;
}

public function fieldTemplate($column, $inputBuilder)
{
}

protected function getValidationRules(): array
Expand Down
92 changes: 51 additions & 41 deletions src/lara-crud/Crud/ReactJs/ReactJsFormInputBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@

class ReactJsFormInputBuilder
{
/**
* @var array
*/
protected array $rules;

public bool $required = false;

public string $type;
public string $type = 'text';

public int $min;
public int $min = 0;

public int $max;
public int $max = 0;

public bool $isArray = false;

Expand All @@ -28,10 +23,10 @@ class ReactJsFormInputBuilder
*/
public function __construct(array $rules = [])
{
$this->rules = $rules;
$this->parse($rules);
}

public function parse()
public function parse(array $rules)
{
$fileValidators = ['file', 'image', 'mimes', 'mimetypes', 'dimensions'];
$dateValidators = [
Expand All @@ -49,42 +44,57 @@ public function parse()
'integer',
'numeric',
];
foreach ($this->rules as $rule) {
$rule = (string) $rule;
$parts = explode(':', $rule);
foreach ($rules as $rule) {
try {
$rule = (string) $rule;
if ('array' === $rule) {
$this->isArray = true;
break;
}
$parts = explode(':', $rule);

if (in_array($parts[0], $fileValidators)) {
$this->type = 'file';
}
if (in_array($parts[0], $fileValidators)) {
$this->type = 'file';
}

if (in_array($parts[0], $dateValidators)) {
$this->type = 'date';
}
if (in_array($parts[0], $dateValidators)) {
$this->type = 'date';
}

if (in_array($parts[0], $numberValidators)) {
$this->type = 'number';
}
if ('in' === $parts[0]) {
$options = explode(',', $parts[1]);
if ($options >= 3) {
$this->type = 'select';
} else {
$this->type = 'radio';
if (in_array($parts[0], $numberValidators)) {
$this->type = 'number';
}
if ('in' === $parts[0]) {
$options = explode(',', $parts[1]);
if ($options >= 3) {
$this->type = 'select';
} else {
$this->type = 'radio';
}
$this->options = $options;
}
if (in_array($rule, ['accepted', 'boolean'])) {
$this->type = 'checkbox';
}
//active_url
if (in_array($rule, ['url', 'email'])) {
$this->type = $rule;
}
if (in_array($rule, ['alpha', 'alpha_num', 'alpha_dash', 'string'])) {
$this->type = 'text';
}
$this->options = $options;
}
if ('boolean' === $rule) {
$this->type = 'checkbox';
}

if ('required' === $rule) {
$this->required = true;
}
if ('min' === $parts[0]) {
$this->min = $parts[1] ?? 0;
}
if ('max' === $parts[0]) {
$this->max = $parts[1] ?? 0;
if ('required' === $rule) {
$this->required = true;
}
if ('min' === $parts[0]) {
$this->min = $parts[1] ?? 0;
}
if ('max' === $parts[0]) {
$this->max = $parts[1] ?? 0;
}
} catch (\Exception $e) {
continue;
}
}
}
Expand Down

0 comments on commit 302b1bf

Please sign in to comment.