Danh mục

Embedding Perl in HTML with Mason Chapter 8: Building a Mason Site-P4

Số trang: 54      Loại file: pdf      Dung lượng: 74.62 KB      Lượt xem: 8      Lượt tải: 0    
10.10.2023

Hỗ trợ phí lưu trữ khi tải xuống: 37,000 VND Tải xuống file đầy đủ (54 trang) 0

Báo xấu

Xem trước 6 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu embedding perl in html with mason chapter 8: building a mason site-p4, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Embedding Perl in HTML with Mason Chapter 8: Building a Mason Site-P4 Chapter 8: Building a Mason Site-P4. users/user_form.mas This form is used for both creating new users and editing existing ones. To prepopulate the form fields, it first looks at the %ARGS hash. If there are values for these fields here, it assumes that these have priority because the only way for %ARGS to have such values is if the form was submitted but then rejected for a data validation error, in which case the browser is redirected back to the submitting page. When that happens, we want to show the user the rejected values that were just entered into the form. If there is nothing in %ARGS, then we look at the $user object for these values. Unless the user for whom this page is being generated is an admin user, we dont bother showing the checkbox that allows them to turn on the admin flag for a user since that checkbox is respected only when a site administrator submits the form. The $submit_to variable is used to set the forms action attribute. This allows us to use this form for both creating new users and editing existing ones. The $return_to value is simply passed through the form to the component that handles the form submission, which will use it to determine where to send the browser if the form submission is successful. % foreach my $err (@errors) { % } % if ($user->user_id) { % } Username: Password: Confirm password: Real name: Email address: How available are you? % while (my $status = $user_statuses->next) { > status | h %> % } % if ($User->is_admin) { Site admin: % } $submit_to $return_to => / $user @errors => ( ) my $user_statuses = $Schema->UserStatus_t->all_rows ( order_by => $Schema->UserStatus_t->status_c ); my %form_vals; foreach my $field ( qw( username passwordreal_name email_address user_status_idis_admin ) ) { $form_vals{$field} = exists $ARGS{$field} ? $ARGS{$field}: $user->$field( ); } $ form_vals{password2} = exists $ARGS{password2} ?$ARGS{password2} : exists $ARGS{password} ? $ARGS{password}: $user->password; • /users/new_user_submit.html Because data validation is handled by our module code, this component doesnt have much to do. If the insert succeeds, we set the cookie used to indicate a successful login and redirect the client to whatever path is in the $return_to variable. Note that we will never set the is_admin flag to true unless the submitting user is a site administrator. One style point: this component calls a few other components, but it uses $m->comp() instead of tags to do so. This is partly just because it was convenient to call the components from within the section, but it also emphasizes the fact that those particular components dont generate any HTML output. $return_to # When inserting a new row, data validation checks are performed and an # exception is thrown if any of the checks fail. my $user = eval { $Schema->User_t->insert ( values => { ( map { $_ => $ARGS{$_} } qw( username passwordpassword2 real_nameemail_address user_status_id ) ), is_admin => $User->is_admin ? $ARGS{is_admin} : 0, } ); }; # One or more data validation checks failed $m->comp( /lib/redirect.mas, path => new_user.html, query =>{ %ARGS, errors => $@->errors } ) if $@ && UNIVERSAL::isa( $@,Apprentice::Exception::DataValidation ); # Some other unforeseen error happened die $@ if $@; $m->comp( /lib/set_login_cookie.mas, user => $user ); $m->comp( /lib/redirect.mas, path => $return_to ); inherit => /syshandler • /lib/redirect.mas With Masons built-in redirect() method, this component is trivially simple. We use the scomp() method to get a URL in the form of a string from the /lib/url.mas component, then pass that to the re ...

Tài liệu được xem nhiều: