/** * modify input value before validation * * @param array $input Assoc array of input fields and values * @param bool $allow_partial If false, all fields that have validation rules defined MUST be present in the input * @param array $name Any additional callables required to run all defined validation rules * @return boolean $temp_callables of validation * * @return boolean * * @access public * @since 1.0 * @version 1.0 * @author Dao Anh Minh */ public function run($input = null, $allow_partial = false, $temp_callables = array()) { if ($input == null) { $input = Input::post(); } return parent::run($input, $allow_partial, $temp_callables); }
public function action_register() { if (Auth\Auth::check()) { Fuel\Core\Session::set_flash('error', 'You cannot register as a user whilst you are logged in.'); Fuel\Core\Response::redirect('/'); } $val = Fuel\Core\Validation::forge('user_registration_validator'); // add validation rules $val->add('fullname', 'Your fullname')->add_rule('required')->add_rule('min_length', 3)->add_rule('max_length', 255); $val->add('password', 'Your password')->add_rule('required')->add_rule('min_length', 5); $val->add('confirm_password', 'Confirm password')->add_rule('required'); if (\Fuel\Core\Input::method() == 'POST') { // run validation on POST if ($val->run(\Fuel\Core\Input::post())) { try { $created = Auth\Auth::create_user(\Fuel\Core\Input::post('username'), \Fuel\Core\Input::post('password'), \Fuel\Core\Input::post('email'), 1, array('fullname' => \Fuel\Core\Input::post('fullname'))); if ($created) { Fuel\Core\Session::set_flash('success', 'New account created.'); Fuel\Core\Response::redirect('user'); } else { Fuel\Core\Session::set_flash('error', 'Registration error: Failed to create account.'); } } catch (\SimpleUserUpdateException $ex) { // duplicate email address if ($ex->getCode() == 2) { Fuel\Core\Session::set_flash('error', 'Email already exists.'); } elseif ($ex->getCode() == 3) { Fuel\Core\Session::set_flash('error', 'Username already exists.'); } else { Fuel\Core\Session::set_flash('error', $ex->getMessage()); } } } else { // validation failed Fuel\Core\Session::set_flash('registration_error', 'Registration error: Invalid user data.'); } } $this->template->user_is_admin = $this->check_user_is_admin(); $this->template->title = 'Register'; $this->template->content = Fuel\Core\View::forge('user/register', array('val' => $val), false); }
protected function __construct($fieldset) { parent::__construct($fieldset); }
/** * validationを実施してtrue or falseを返します。<br/> * 第1引数の$validationにadd_ruleなどした状態で指定してください。<br/> * <br/> * GET値のバリデーションの場合は、第2引数に配列でキーと値をセットしてください。<br/> * GET例:$get_parameter = array('hoge' => Input::get('hoge'),'fuga' => Input::get('fuga'));<br/> * <br/> * バリデーションエラー時はエラーログに入力値内容などを出力し、<br/> * プロパティのmessageにエラー内容を文字列で設定します。<br/> * 呼び出し元でエラーメッセージを利用したければself::$messageを利用してください。<br/> * self::$error_arrayでjson出力用エラーを設定します。<br/> * { * "status": 400, * "message": "validation error", * "error": [ * { * "field": "hoge", * "message": "The field hoge is required and must contain a value." * }, * { * "field": "fuga", * "message": "The field fuga is required and must contain a value." * } * ] * } * @param Fuel\Core\Validation $validation * @param array $get_parameter * @return boolean */ public static function validation(Fuel\Core\Validation $validation, array $get_parameter = null) { if ($validation->run($get_parameter)) { return true; } # 以下、validation_error発生処理 self::$message = "validation error"; $count = 0; $log = null; foreach ($validation->error() as $key => $value) { $log .= "{$key}=>{$value},"; self::$error_array[$count]['field'] = $key; self::$error_array[$count]['message'] = $value->get_message(); $count++; } $log = "[validation error]" . rtrim($log, ","); Log::error($log); return false; }