/** * Required validator * * @param array $attr * @param array $item * * @return bool */ function validator_required(array $attr, array &$item) : bool { if ($attr['required'] && empty($item[$attr['id']]) && !$attr['opt'] && !ignorable($attr, $item)) { $item['_error'][$attr['id']] = _('%s is a mandatory field', $attr['name']); return false; } return true; }
/** * Editor * * @param array $attr * @param array $item * * @return string */ function editor(array $attr, array $item) : string { if (!in_array('edit', $attr['actions'])) { return ''; } $item[$attr['id']] = $item[$attr['id']] ?? $attr['val']; $attr['opt'] = opt($attr); $attr['context'] = 'edit'; $attr['html']['id'] = html_id($attr, $item); $attr['html']['name'] = html_name($attr, $item); $attr['html']['data-type'] = $attr['type']; if ($attr['required'] && !ignorable($attr, $item)) { $attr['html']['required'] = true; } if (!empty($attr['multiple'])) { $attr['html']['multiple'] = true; } if (!empty($item['_error'][$attr['id']])) { $attr['html']['class'] = empty($attr['html']['class']) ? 'invalid' : $attr['html']['class'] . ' invalid'; } $html = ''; $callback = fqn('editor_' . $attr['type']); if (is_callable($callback)) { $html = $callback($attr, $item); } else { // @todo switch ($attr['frontend']) { case 'select': $html = editor_select($attr, $item); break; case 'checkbox': case 'radio': $html = editor_opt($attr, $item); break; case 'color': case 'email': case 'url': $html = editor_text($attr, $item); break; case 'number': case 'range': $html = editor_int($attr, $item); break; case 'date': case 'time': $html = editor_datetime($attr, $item); break; case 'file': $html = editor_file($attr, $item); break; case 'textarea': $html = editor_textarea($attr, $item); break; } } return $html ? html_label($attr, $item) . $html . html_message($attr, $item) : ''; }
/** * Label * * @param array $attr * @param array $item * * @return string */ function html_label(array $attr, array $item) : string { $label = $attr['name']; if ($attr['required'] && !ignorable($attr, $item)) { $label .= ' ' . html_tag('em', ['class' => 'required'], _('Required')); } if (!empty($attr['uniq'])) { $label .= ' ' . html_tag('em', ['class' => 'uniq'], _('Unique')); } return html_tag('label', ['for' => html_id($attr, $item)], $label); }