Here's a more explicit example, with some screenshots to illustrate the benefits.
I'll use my PHP client...
Say I have an HTML table with form fields for everything that's currently in a section (I'll use the /queue/simple section, limited for brevity).
Right now, I can create forms that accept and are prefilled with text, like so:
<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/autoload.php';
header('Content-Type: text/html;charset=UTF-8');
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>TEST</title>
</head>
<body>
<form action="formProcessor.php" method="post">
<?php
$client = new Client('192.168.0.1', 'apifull', 'apifull');
$request = new Request('/queue/simple/print');
$request->setArgument('.proplist',
'.id,name,disabled,dst-address,target-addresses,priority'
);
echo '<table>';
foreach ($client->sendSync($request) as $index => $response) {
echo '<tr>';
foreach ($response->getAllArguments() as $name => $value) {
echo '<td>';
echo '<input type="hidden" name="oldValues[',$index,'][', $name, ']" value="',
$value, '"/>';
echo '<input name="data[', $index, '][', $name, ']" ';
echo 'type="text" value="', $value, '" />';
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
<input type="submit" value="Submit changes" />
</form>
</body>
</html>
It would be rendered something like
typeless.png
This
works. So far, so good. It's not really a problem to leave it like that. As you said, if the user enters a wrong text value, there will be a !trap response. But it would be nice if I could instead render the form more like:
typed.png
I
could do this by explicitly enumerating all instances where I'm aware that a property is a boolean or ID, such as "disabled" and ".id". I could do this either within the client (the Response object) or outside of it, like so:
<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/autoload.php';
header('Content-Type: text/html;charset=UTF-8');
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>TEST</title>
</head>
<body>
<form action="formProcessor.php" method="post">
<?php
$client = new Client('192.168.0.1', 'apifull', 'apifull');
$request = new Request('/queue/simple/print');
$request->setArgument('.proplist',
'.id,name,disabled,dst-address,target-addresses,priority'
);
echo '<table>';
foreach ($client->sendSync($request) as $index => $response) {
echo '<tr>';
foreach ($response->getAllArguments() as $name => $value) {
if ($name === '.id') {
$value = hexdec(substr($value, 1));
echo '<input type="hidden" name="toID[', $index,
']" value="', $name, '" />';
}
echo '<td>';
echo '<input type="hidden" name="oldValues[',
$index, '][', $name,
']" value="', $value, '" />';
echo '<input name="data[', $index, '][', $name, ']" ';
if ($name === 'disabled') {
echo 'type="checkbox"';
if ($value === 'true') {
echo ' checked="checked"';
}
} else {
echo 'type="text" value="', $value, '"';
}
echo ' />';
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
<input type="submit" value="Submit changes" />
</form>
</body>
</html>
This too works... but it works for the time being only, and only for properties I'm aware to be of said type. It's an extremely fragile and brute force way of creating a UI that is more intuitive than plain text boxes. But there's no way for it to be otherwise without RouterOS sharing some type information. If it did, the above could be written something more like this (assuming I also rework the client to honor the type information, allow the user to choose to cast based on it, and get the type as given by that word):
<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/autoload.php';
header('Content-Type: text/html;charset=UTF-8');
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>TEST</title>
</head>
<body>
<form action="formProcessor.php" method="post">
<?php
$client = new Client('192.168.0.1', 'apifull', 'apifull');
$request = new Request('/queue/simple/print');
$request->setArgument('.proplist',
'.id,name,disabled,dst-address,target-addresses,priority'
);
echo '<table>';
foreach ($client->sendSync($request) as $index => $response) {
echo '<tr>';
foreach ($response->getAllArguments() as $name => $value) {
echo '<td>';
echo '<input type="hidden" name="oldValues[',
$index, '][', $name,
']" value="', $value, '" />';
echo '<input type="hidden" name="types[',
$index, '][', $name,
']" value="', $response->getArgumentType($name), '" />';
echo '<input name="data[', $index, '][', $name, ']" ';
switch (gettype($value)) {
case 'bool':
echo 'type="checkbox"';
if ($value) {
echo ' checked="checked"';
}
break;
case 'int':
case 'double':
/* Let's say for example that later we'd do JS
validation and/or add a numeric keypad for numbers,
and this class here is just the anchor.
*/
echo 'class="number" ';
default:
echo 'type="text" value="', $value, '"';
}
echo ' />';
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
<input type="submit" value="Submit changes" />
</form>
</body>
</html>
Not only does this last code work... it would work for any response. For example, I could switch to /ip/firewall/nat, and get a checkbox for "fragment" even though I never listed it.
You do not have the required permissions to view the files attached to this post.