Jump to content
php.lv forumi

Kļūdas izvadīšana


Kemito

Recommended Posts

Tad otrs jautājums, kā šinī sakarā noķert vairākus nederīgus ievadītos laukus?

 public function register() {
   $user = new User($_POST['user'], $_POST['mail'], $_POST['birthday'], ...);

   try { $user->save(); }
   catch(ValidationException $e) {
     $this->render('new', array('form_errors' => $e->get_reasons()));
   }
 }

..vienkārši tas ir diezgan kaitinoši, kad netiek izvadīti visi kļūdaini lauki uzreiz, bet gan pa vienam :)

Link to comment
Share on other sites

Starpcitu, arī jūsu pieminētie frameworki (Kohana, Fuel) lieto tos pašus Exception'us neveiksmīgas validācijas u.c. gadījumos.

Yii kodā es neko tādu neredzu, exception pie modeļa validācijas tiek izsaukts tikai ja pats validation rule ir uzrakstīts ar kļūdām.

Link to comment
Share on other sites

briedis: Kaut kaa taa vareetu

 

// Base Model klasee

public function run_validations() {
 $errors = array();
 foreach($this->validations as $validation) {
   try {
     $this->validate($validation);
   }
   catch(SomeValidationException $e) {
     $errors[] = $e->get_why_message();
   }
 }

 if(!empty($errors))
   throw new ValidationException($errors);
}

 

Eniivei - nezkaadeelj liekas ka te driiz saaksies religjiozie kari :D

Edited by rATRIJS
Link to comment
Share on other sites

Yii arī nav raķešu zinātne. Visur pieeja viena http://www.yiiframework.com/doc/guide/1.1/en/topics.error#raising-exceptions

Tip: Raising a CHttpException exception is a simple way of reporting errors caused by user misoperation. For example, if the user provides an invalid post ID in the URL, we can simply do the following to show a 404 error (page not found)
Link to comment
Share on other sites

Tas bija piemērs (par Kohanu), nevis automātiski nozīmē, ka pilnīgi visi frameworki dara tikai tā un ne savādāk. Nav ko cepties. Exception nav nekas slikts un nekur nav teikts, ka šī metode jāizmanto tikai akūtos gadījumos.

Edited by marcis
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...