Raising errors without throwing exceptions

22 views
Skip to first unread message

john mossell

unread,
Dec 14, 2010, 1:37:17 AM12/14/10
to redbeanphp
Hi,

If a user fills in a form incorrectly, how can I tell him without
throwing an exception. I don't want to throw an exception because if
he fills the username and comment field in incorrectly, I want to tell
him that both are wrong, not just one. Also with exceptions, the code
is stopped and the input form (signup form etc) doesn't show-- only
the exception error message shows. I want to show a list of errors in
the form and also show the form.

Thanks...

zenith

unread,
Dec 14, 2010, 1:32:50 PM12/14/10
to redbeanphp
Hi,
what does it mean that the fields are wrong exactly?

You can check the input values in javascript, or by creating variables
in php that indicate the errors.

gabor de mooij

unread,
Dec 14, 2010, 2:38:50 PM12/14/10
to redbe...@googlegroups.com
create a function called $this->checkProperties(), put it in a class called

class Model_User extends RedBean_SimpleModel { ... }

now if a user registers (example) import the fields like you used to,
using R::import and do R::store($user).

Now create a method in your user class called update() :

public function update() {
$this->checkProperties(); //your bean is here: $this->bean
}

Throw an exception and attach a list of fields to the exception (using
the message), or store the list in another property of the class.
RedBean will find the Model_User automatically; this is Fuse.

Cheers,
Gabor

> --
> You received this message because you are subscribed to the Google Groups "redbeanphp" group.
> To post to this group, send email to redbe...@googlegroups.com.
> To unsubscribe from this group, send email to redbeanorm+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/redbeanorm?hl=en.
>
>

john mossell

unread,
Dec 14, 2010, 4:14:50 PM12/14/10
to redbeanphp
So after I've stored the user:


class Model_User extends RedBean_SimpleModel
{
public function update()
{
$this->checkProperties();
}

public function checkProperties()
{
if ( (strlen($this->username)) < 6 || (strlen($this-
>username)) > 12 )
{
throw new Exception('Username must be between 6 and 12
characters.');
}

if ( (!preg_match( "(^[a-zA-Z0-9_]*$)" ,$this->username)) )
{
throw new Exception('Username can only contain
alphanumeric characters (a-z, 0-9 and _)')
}
}

}

So, if a user enters a username that is less than 6 characters AND
also does not match the regex pattern, I want to show both exceptions.


There was a problem with your form submission:
-- Username must be between 6 and 12 characters.
-- Username can only contain alphanumeric characters.

// then show form here

john mossell

unread,
Dec 14, 2010, 5:25:36 PM12/14/10
to redbeanphp
Actually I've decided to change it a bit, here is the full code, I
want to do something like this

public function update
{
// for each bean (i.e. username, name and email):
if ( in_array($this->bean, $this->_errors))
{
return $this->bean->getError(); // so I can display each
error under each form field
}
}

My model is fuse is as follows:

class Model_User extends RedBean_SimpleModel
{
# internal variables
protected $_errors = array();

public function update()
{
$this->checkProperties();
}


public function checkProperties()
{
if ( (strlen($this->username)) < 6 || (strlen($this-
>username)) > 12 )
{
$this->_errors[$this->bean] = 'Username must be between 6
and 12 characters.';
}

if ( (!preg_match( "(^[a-zA-Z0-9_]*$)" ,$this->username)) )
{
$this->_errors[$this->bean] = 'Username can only contain
alphanumeric characters (a-z, 0-9 and _)';
}

if ( strlen($this->password) < 7 )
{
$this->_errors[$this->bean] = 'Password must be longer
than 7 characters';
}

}


}

gabor de mooij

unread,
Dec 15, 2010, 6:21:29 AM12/15/10
to redbe...@googlegroups.com
that is what I meant ;) this should work !

john mossell

unread,
Dec 15, 2010, 11:33:26 AM12/15/10
to redbeanphp
I don't know what to do with the getErrors function though... how can
I access it from the outside?

On Dec 15, 11:21 am, gabor de mooij <gabordemo...@gmail.com> wrote:
> that is what I meant ;) this should work !
>
> On Tue, Dec 14, 2010 at 11:25 PM, john mossell
>

gabor de mooij

unread,
Dec 15, 2010, 11:57:27 AM12/15/10
to redbe...@googlegroups.com
Well if getErrors() is part of your model and you can access the bean,
just fire the method on the bean:

$bean->getErrors();

Cheers ;)

john mossell

unread,
Dec 15, 2010, 2:48:32 PM12/15/10
to redbeanphp
Sorry, I meant to write I don't know what code to put into $this-
>getErrors(), to get the errors for each bean...

Alberto Ruffo

unread,
May 10, 2013, 8:13:15 PM5/10/13
to redbe...@googlegroups.com, johnm...@googlemail.com
Hello, i'd like to continue this post:

If i have this tables:

Customer
-----------------------------
id, name, surname

AND

Address
-----------------------------
id, address, number, city, customer_id


Customer has more addresses, so:

$address = R::dispense...
$address->import(...); // for instance, from $_POST['address']

$customer = R::dispense...
$customer->import(...); // for instance, from $_POST['customer']

$customer->ownAddress[] = $address;

try {
R::store($customer);
} catch (ModelException $e) {}

In this case, how do model validations work? 
I have to throw exception once and retrieve errors of both at the same time; but there are two model-validations which need two exceptions.
Reply all
Reply to author
Forward
0 new messages