Tutorial - DVD Title

Handling The Title Formlets

The DVD title is not a key, but it must also have a value before we can commit. This is because we are using the title itself as a link on the list page. It's also a reasonable assumption that one wishes to have a title to any DVD on the database, so it's not too much of a restriction.

Here we want to check to see if there is a value being submitted. If there is, and it's valid, then we will want to commit the record. So we can add a simple error view also - this can be overwritten if needs be, but here that is not necessary.

Otherwise, we will retrieve the record from the database and display it. First of all the view. This is more or less the same as the ean-13 in this case, but often will look different in the real world! Because the title value is free text, we need to sql encode it to prevent sql injection!

Creating the DVD formlet_title.html file

<!DOCTYPE label PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<label xmlns="http://www.w3.org/1999/xhtml">
	<span>Title</span> 
	<input name="title" type="text" />
</label>

And creating the DVD formlet_title_err.html file

<!DOCTYPE div PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<div xmlns="http://www.w3.org/1999/xhtml" class="error" >
	Error! The title field must have a value! 
</div>

The following is quite complex! Here is a pseudocode version of what we are doing.

load view
if (sysparms 'ean13,title' exist) {
	if (sysparm title has a value) {
		commit title to db
		insert title to view
	} else {
		insert 'no value' error to parent
	}
} else {
	if (sysparm ean13 exists) {
		if (db record) {
			insert title to view from db
		}
	}
}
insert view into parent

And of course the DVD formlet_title.obyx file

<instruction xmlns="http://www.obyx.org">
	<input>
		<instruction note="load view">
			<output space="store" value="f_title"/>
			<input space="file" value="dvdformlet_title.html"/>
		</instruction>
		<comparison operation="existent" note="if (sysparms 'ean13,title' exist)">
			<comparate space="sysparm" value="ean13" />
			<comparate space="sysparm" value="title" />
			<ontrue>
				<comparison operation="significant" note="if (sysparm title has a value)">
					<comparate space="sysparm" value="title" />
					<ontrue>
						<instruction operation="query" note="commit title to db" >
							<input kind="text">
								<s value="replace into ean13item set ean13='" />
								<s space="sysparm" value="ean13" encoder="digits"  />
								<s value="', title='" />
								<s space="sysparm" value="title" encoder="sql" />
								<s value="'" />
							</input>
						</instruction>
						<instruction note="insert title to view">
							<output space="store" value="f_title#//h:input/@value" />
							<input space="sysparm" value="title" />
						</instruction>
					</ontrue>
					<onfalse>
						<instruction note="insert 'no value' error to parent">
							<output space="store" context="parm" value="errxp" />
							<input space="file" value="dvdformlet_title_err.html"/>
						</instruction>
					</onfalse>
				</comparison>
			</ontrue>
			<onfalse>
				<comparison operation="existent" note="if (sysparm ean13 exists)">
					<comparate space="sysparm" value="ean13" />
					<ontrue>
						<iteration operation="sql" note="if (db record)" >
							<control kind="text">
								<s value="select title from ean13item where ean13='" />
								<s space="sysparm" value="ean13" encoder="digits"  />
								<s value="'" />
							</control>
							<body>
								<instruction note="insert title to view from db">
									<output space="store" value="f_title#//h:input/@value" />
									<input space="field" value="title" />
								</instruction>
							</body>
						</iteration>
					</ontrue>
				</comparison>
			</onfalse>
		</comparison>
		<instruction note="insert view into parent">
			<output space="store" context="parm" value="xpath" />
			<input space="store" value="f_title" release="true"/>
		</instruction>
	</input>
</instruction>

Okay, so we have a basic application. I hope it gives you some idea of using Obyx.




Last Modified: Thu, 15 Jan 2015