Welcome Guest, Not a member yet? Register   Sign In
How to show validation errors with DMZ
#1

[eluser]The Mask[/eluser]
Hi,
I am in the process of implementing DMZ but how do I show validation errors in my views?

Beforehand, I was using:
<?php echo form_error( 'username' ); ?>

Also, do I need to change how the invalid value entered is shown?
ie <input name="username" value="<?php echo set_value( 'username' ); ?>" />

Thanks
#2

[eluser]BrianDHall[/eluser]
Here you go: http://www.overzealous.com/dmz/pages/val...r.Messages

For speed I use to just do this at the top of the form:

Code:
echo $object->error->string;

This prints out a string of all errors at the top, quick and easy. But you are able to do:

Code:
echo $object->error->fieldname;

...to make it so each field's error is right above the field itself, should you prefer.

For setting the value just use something like:

Code:
<input name=“username” value=”<?php echo $object->username; ?>” />

...if I recall correctly.

I use to do things this way as well but I found the HTMLForm extension to be incredible - using it has created the first time I've ever not dreaded having to create and handle forms. Ugh, I use to so hate forms, and with DMZ and the extension (and the array extension, of course), oh it is so much easier.

You don't have to use it right away, but I found putting in the extra time to learn it (it does have a little learning curve, but the manual on it is pretty small so just read carefully and experiment) was so worth it I wish I'd done it sooner.

http://www.overzealous.com/dmz/pages/extlist.html

The ability to do this in the controller is just so damn cool:

Code:
if ($_POST)
{
   $object->from_array($_POST);

   if ($object->save())
   {
      // yeah!
   }
   else
   {
      // boo!
   }
}

...which is the array extension. To show my form the view looks like:

Code:
<?= $org->render_form(array('fieldsyouwantshown', 'otherfield', 'etc'), 'urltopost/to/orleaveempty', array('save_button' => 'Save Button Text', 'reset_button' => 'Clear')); ?>

...and that's it. You just edit the template if you want the form to appear differently, so you either build the html of a form 0 times (use the existing template) or 1 time to work for every form on your site.

The only thing better than dealing with forms is dealing with forms less :>

DMZ r0x0rs juR s0x3rz
#3

[eluser]bonatoc[/eluser]
Hello,

I understand that validation is performed only when trying to save an $object.

I have a 3 steps entity creation (a franchise), so it's a 3 forms sequence (1.choose a country of competence, 2.create a company, 3. Create the main's company contact).

What I want to do is to store each step values into a session array, and then insert everything all at once in the DB, after the final step.

But, as I read, validation within htmlform is only performed when trying to save :

$company->from_array($_POST);

if ($company->save())
{
// yeah!
}
else
{
// boo!
}

The problem for me is I don't want to insert the company (step 2) yet. Because you never know if the user is going to cancel his action before step 3, and I don't want the DB to be polluted. I could still check the last id inserted, delete it, and proceed to step 3, but I'm looking for something cleaner.

Is there a way to simulate a save(), so the validation is performed without actually perform a DB insertion ?
#4

[eluser]bonatoc[/eluser]
I found the way, and it's pretty simple.

For those of you interested :

Code:
$company->from_array($_POST);
$company->validate();

if ($company->valid)
{
echo("yeah!<br>");
}
else
{
echo("boo!<br>");
}

...as posted here (2nd post) :
http://ellislab.com/forums/viewthread/162347/




Theme © iAndrew 2016 - Forum software by © MyBB